MongoDB Replica-set设置
MongoDB Replica-set设置本文用三个MongoDB server来架设replica-set。首先分别准备三个UBuntu vm, 里面安装了MongoDB instance.然后每个vm的/etc/hosts里面都包含了另外两个vm的ip和主机名。这样确保每个vm都能通过主机名连接到其他两个vm.我的vm分别是:vm1, vm3, vm4. IP分别是192.168.1.51, 192.168.1.52和192.168.1.53依次登录所有的vm,然后编辑/etc/mongodb.conf文件,取消注释,并设置replSet[plain]replSet = rs1 // 必须设置为一个名字,千万别不同添加[plain]fork=true重新启动mongodb[plain]service mongodb restart运行下面的命令:登录到vm3,登录到mongodb, 然后执行rs.initiate创建一个replicaset,并用config检查配置。[plain]> rs.initiate(){"info2" : "no configuration explicitly specified -- 易做图 one","me" : "vm3:27017","info" : "Config now saved locally. Should come online in about a minute.","ok" : 1}> rs.config(){"_id" : "rs1","version" : 1,"members" : [{"_id" : 0,"host" : "vm3:27017"}]}注意,3个vm中只能选中一个运行rs.initiate命令,如果在其他的vm中不小心也运行了。需要用下面的方法清除之:[plain]use localdb.dropDatabase()exitservice mongodb restart然后在vm3的mongodb上继续执行rs.add命令:[plain]root@vm3:~# mongoMongoDB shell version: 2.4.3connecting to: testrs1:PRIMARY> rs.add("vm1"){"errmsg" : "exception: set name does not match the set name host vm1:27017 expects","code" : 13145,"ok" : 0}rs1:PRIMARY> rs.add("vm4"){"errmsg" : "exception: set name does not match the set name host vm4:27017 expects","code" : 13145,"ok" : 0}报错的原因很奇怪。查了半天,难道是刚才修改了/etc/hosts没有重新启动的原因么?把三个vm都重新启动,再试![plain]root@vm3:~# mongoMongoDB shell version: 2.4.3connecting to: testrs1:PRIMARY> rs.add("vm4"){ "ok" : 1 }rs1:PRIMARY> rs.add("vm1"){ "ok" : 1 }成功了。不容易啊!再看一下配置:[plain]rs1:PRIMARY> rs.config(){"_id" : "rs1","version" : 3,"members" : [{"_id" : 0,"host" : "vm3:27017"},{"_id" : 1,"host" : "vm4:27017"},{"_id" : 2,"host" : "vm1:27017"}]}来看看状态:[plain]s1:PRIMARY> rs.status(){"set" : "rs1","date" : ISODate("2013-05-04T12:36:28Z"),"myState" : 1,"members" : [{"_id" : 0,"name" : "vm3:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 380,"optime" : {"t" : 1367670744,"i" : 1},"optimeDate" : ISODate("2013-05-04T12:32:24Z"),"self" : true},{"_id" : 1,"name" : "vm4:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 248,"optime" : {"t" : 1367670744,"i" : 1},"optimeDate" : ISODate("2013-05-04T12:32:24Z"),"lastHeartbeat" : ISODate("2013-05-04T12:36:28Z"),"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),"pingMs" : 0,"syncingTo" : "vm3:27017"},&上一个:redis 2.6.9安装报错解决
下一个:查询数据库中所有表的语句
- 更多mongodb疑问解答:
- 【急】MongoDB写入错误~~~~
- Mongodb NOSql 数据库问题,是否可以插入带接口的类
- java操作mongodb
- Spring data MongoDB 更新整个内嵌文档时报错???????
- node.js连接mongodb更新
- MongoDB Java驱动 WriteConcern.SAFE非常浪费资源
- 求科普,hibernate怎样操作mongodb?
- 问一下mongodb怎么用hibernate整合
- mongodb查询的数据过多
- 使用JAVA创建MongoDB的问题
- Mongodb事务管理问题?
- mongodb利用java进行模糊查询
- spring 抽象类 注入值为空(spring3+mongodb+morphia)
- 【急】MongoDB写入错误~~~~
- Mongodb NOSql 数据库问题,是否可以插入带接口的类