root@test:/usr/local/mongodb#mkdir replicaset
root@test:/usr/local/mongodb/replicaset#ls
root@test:/usr/local/mongodb#cd replicaset/
root@test:/usr/local/mongodb/replicaset#ls
root@test:/usr/local/mongodb/replicaset#echo $PATH
root@test:/usr/local/mongodb/replicaset#source /etc/profile
root@test:/usr/local/mongodb/replicaset#echo $PATH
root@test:/usr/local/mongodb/replicaset#ls
root@test:/usr/local/mongodb/replicaset#mkdir 2222
root@test:/usr/local/mongodb/replicaset#mkdir 3333
root@test:/usr/local/mongodb/replicaset#mkdir 4444
既然我们要建立集群,就得取个集群名字,这里就取我们的公司名shopex, --replSet表示让服务器知道shopex下还有其他数据库,
这里就把D盘里面的mongodb程序打开,端口为2222。指定端口为3333是shopex集群下的另一个数据库服务器。
root@test:/usr/local/mongodb/replicaset#mongod --dbpath=/usr/local/mongodb/replicaset/2222/ --port 2222 --replSet shopex/192.168.99.232:3333 --logpath=/usr/local/mongodb/replicaset/2222/2222.log --fork
root@test:/usr/local/mongodb/replicaset#netstat -ntlp|grep mongo
既然上面说3333是另一个数据库服务器,不要急,现在就来开
root@test:/usr/local/mongodb/replicaset#mongod --dbpath=/usr/local/mongodb/replicaset/3333/ --port 3333 --replSet shopex/192.168.99.232:2222 --logpath=/usr/local/mongodb/replicaset/3333/3333.log --fork
root@test:/usr/local/mongodb/replicaset#netstat -ntlp|grep mongo
似乎我们还没有做完,是的,log信息告诉我们要初始化一下“副本集“,既然日志这么说,那我也就
这么做,随便连接一下哪个服务器都行,不过一定要进入admin集合。
root@test:/usr/local/mongodb/replicaset#mongo --port 2222
> use admin
> db.runCommand({"replSetInitiate":{ "_id":"shopex", "members":[ { "_id":1, "host":"192.168.99.232:2222" }, { "_id":2, "host":"192.168.99.232:3333" } ]}})
我们知道sql server里面有一个叫做仲裁服务器,那么mongodb中也是有的,跟sql server一样,仲裁只参与投票选举,这里我们
把F盘的mongodb作为仲裁服务器,然后指定shopex集群中的任一个服务器端口,这里就指定2222。
root@test:/usr/local/mongodb/replicaset#mongod --dbpath=/usr/local/mongodb/replicaset/4444/ --port 4444 --replSet shopex/192.168.99.232:2222 --logpath=/usr/local/mongodb/replicaset/4444/4444.log --fork
netstat -ntlp|grep mongo
然后我们在admin集合中使用rs.addArb()追加即可。
root@test:/usr/local/mongodb/replicaset#mongo --port 2222
> use admin
> rs.addArb("192.168.99.232:4444")
> rs.status()
root@test:/usr/local/mongodb/replicaset#mongod --dbpath=/usr/local/mongodb/replicaset/2222/ --port 2222 --replSet shopex/192.168.99.232:3333 --logpath=/usr/local/mongodb/replicaset/2222/2222.log --fork
root@test:/usr/local/mongodb/replicaset#netstat -ntlp|grep mongo
root@test:/usr/local/mongodb/replicaset#kill -2 10649
root@test:/usr/local/mongodb/replicaset#netstat -ntlp|grep mongo
root@test:/usr/local/mongodb/replicaset#mongo --port 2222
>rs.status()
root@test:/usr/local/mongodb/replicaset#
shopex:PRIMARY> show dbs
local 0.09375GB
shopex:PRIMARY> use test
switched to db test
shopex:PRIMARY> for(i=1;i<100000;i++){
... db.userinfo.insert({"username":"user_"+i});
... }
shopex:PRIMARY> show tables
system.indexes
userinfo
shopex:PRIMARY> db.userinfo.count()
99999
shopex:PRIMARY> exit
bye
root@test:/usr/local/mongodb/replicaset# mongo --port 3333
MongoDB shell version: 2.4.9-pre-
connecting to: 127.0.0.1:3333/test
Server has startup warnings:
Tue Nov 19 10:17:37.540 [initandlisten]
Tue Nov 19 10:17:37.540 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Tue Nov 19 10:17:37.540 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Tue Nov 19 10:17:37.540 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Tue Nov 19 10:17:37.540 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Tue Nov 19 10:17:37.540 [initandlisten]
shopex:SECONDARY> use test
switched to db test
shopex:SECONDARY> show tables
Tue Nov 19 10:43:19.476 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:128
shopex:SECONDARY> rs.slaveOk()
shopex:SECONDARY> show tables
system.indexes
userinfo
shopex:SECONDARY> db.userinfo.count()
99999
shopex:SECONDARY>