MongoDB之Replica Set(复制集复制)
MongoDB支持两种复制模式:
- 主从复制(Master/Slave)
- 复制集复制(Replica Set)
下面主要记录我在centos虚拟机上安装replica set,主要参考:http://docs.mongodb.org/manual/tutorial/deploy-replica-set/
首先准备好3份配置文件:
1 port = 27017 2 3 dbpath = /home/bells/install/data1 4 logpath = /home/bells/install/log1/1.log 5 6 fork = true 7 replSet = rs0
1 port = 27016 2 3 dbpath = /home/bells/install/data2 4 logpath = /home/bells/install/log2/2.log 5 6 fork = true 7 replSet = rs0
1 port = 27015 2 3 dbpath = /home/bells/install/data3 4 logpath = /home/bells/install/log3/3.log 5 6 fork = true 7 replSet = rs0
然后再启动这3个mongodb服务
sudo ./mongodb1/bin/mongod --smallfiles --config 1.conf sudo ./mongodb2/bin/mongod --smallfiles --config 2.conf sudo ./mongodb3/bin/mongod --smallfiles --config 3.conf
其实smallfiles也应该写进*.conf文件中的。
最后配置:
主要是通过下面的命令
rs.initiate() 使用默认的配置初始化replica set在当前的服务器
rs.status() 显示当前replica set的状态
rs.add(serverInfo) 增加新的成员到replica set 中
rs.addArb(serverInfo) 增加一个arbiter到replica set中
1 rs.initiate() 2 { 3 "info2" : "no configuration explicitly specified -- making one", 4 "me" : "localhost.localdomain:27017", 5 "info" : "Config now saved locally. Should come online in about a minute.", 6 "ok" : 1 7 } 8 > rs.conf() 9 { 10 "_id" : "rs0", 11 "version" : 1, 12 "members" : [ 13 { 14 "_id" : 0, 15 "host" : "localhost.localdomain:27017" 16 } 17 ] 18 } 19 rs0:PRIMARY> rs.add("localhost.localdomain:27016") 20 { "ok" : 1 } 21 rs0:PRIMARY> rs.add("localhost.localdomain:27015") 22 { "ok" : 1 } 23 rs0:PRIMARY> rs.status() 24 { 25 "set" : "rs0", 26 "date" : ISODate("2014-02-23T01:43:16Z"), 27 "myState" : 1, 28 "members" : [ 29 { 30 "_id" : 0, 31 "name" : "localhost.localdomain:27017", 32 "health" : 1, 33 "state" : 1, 34 "stateStr" : "PRIMARY", 35 "uptime" : 602, 36 "optime" : Timestamp(1393119782, 1), 37 "optimeDate" : ISODate("2014-02-23T01:43:02Z"), 38 "self" : true 39 }, 40 { 41 "_id" : 1, 42 "name" : "localhost.localdomain:27016", 43 "health" : 1, 44 "state" : 5, 45 "stateStr" : "STARTUP2", 46 "uptime" : 17, 47 "optime" : Timestamp(0, 0), 48 "optimeDate" : ISODate("1970-01-01T00:00:00Z"), 49 "lastHeartbeat" : ISODate("2014-02-23T01:43:15Z"), 50 "lastHeartbeatRecv" : ISODate("2014-02-23T01:43:15Z"), 51 "pingMs" : 0, 52 "lastHeartbeatMessage" : "initial sync need a member to be primary or secondary to do our initial sync" 53 }, 54 { 55 "_id" : 2, 56 "name" : "localhost.localdomain:27015", 57 "health" : 1, 58 "state" : 5, 59 "stateStr" : "STARTUP2", 60 "uptime" : 14, 61 "optime" : Timestamp(0, 0), 62 "optimeDate" : ISODate("1970-01-01T00:00:00Z"), 63 "lastHeartbeat" : ISODate("2014-02-23T01:43:16Z"), 64 "lastHeartbeatRecv" : ISODate("2014-02-23T01:43:16Z"), 65 "pingMs" : 1, 66 "lastHeartbeatMessage" : "syncThread: 12520 new file allocation failure" 67 } 68 ], 69 "ok" : 1 70 } 71 rs0:PRIMARY> db.person.insert({"name":"jack", "age":20}) 72 rs0:PRIMARY> db.person.find() 73 { "_id" : ObjectId("5309529869d3a7216c59ef2a"), "name" : "jack", "age" : 20 }
1 [bells@localhost install]$ ./mongodb2/bin/mongo --port 27016 2 MongoDB shell version: 2.4.5 3 connecting to: 127.0.0.1:27016/test 4 rs0:SECONDARY> db.person.find() 5 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } 6 rs0:SECONDARY> rs.slaveOk(); 7 rs0:SECONDARY> db.person.find() 8 { "_id" : ObjectId("5309529869d3a7216c59ef2a"), "name" : "jack", "age" : 20 }
安装部署过程中,遇到两个问题:
1. 启动mongodb时,错误日志:
1 Sun Feb 23 09:47:57.930 [initandlisten] ERROR: Insufficient free space for journal files 2 Sun Feb 23 09:47:57.930 [initandlisten] Please make at least 3379MB available in /home/bells/install/data1/journal or use --smallfiles 3 Sun Feb 23 09:47:57.930 [initandlisten] 4 Sun Feb 23 09:47:57.953 [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
解决办法就是,启动的时候,加个 --smallfiles : 这个参数默认值是false,如果设为true,则会是每个文件最大的大小限定为512M而不是默认的2G
2. 在secondary中,查询时:
1 rs0:SECONDARY> db.person.find() 2 error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
这个是正常的,因为SECONDARY默认是不允许读写操作的,可以:
rs0:SECONDARY> db.slaveOk();
来解决!