MongoDB复制集——如何将单节点转换成复制集
MongoDB复制集——如何将单节点转换成复制集
具体步骤如下:
1. 停止单节点的mongod
我们可以从mongo shell里运行db.shutdownServer()命令来停止mongod
[javascript] > use admin; switched to db admin > db.shutdownServer()
2. 使用--replSet的选项加上新的复制集的名字来重新启动这个mongod
[javascript] bash-3.2$ mongod --dbpath /data/db --logpath /data/mongod.log --logappend --fork --port 30000 --replSet rs0 about to fork child process, waiting until server is ready for connections. forked process: 3092 all output going to: /data/mongod.log child process started successfully, parent exiting
3. 连接刚才启动的数据库
[javascript] bash-3.2$ mongo --port 30000 MongoDB shell version: 2.4.5 connecting to: 127.0.0.1:30000/test Server has startup warnings: Mon Jul 29 11:28:02.291 [initandlisten] Mon Jul 29 11:28:02.291 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
4. 在mongo shell里创建一个包含复制集配置信息的对象
[javascript] rsconf = { _id: "rs0", members: [ { _id: 0, host: "localhost:30000" } ] }
5. 用rs.initiate()来初始化复制集
[javascript] > rs.initiate( rsconf ) { "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 }
6. 初始化完毕后,我们可以用rs.conf()来查看复制集的配置
[javascript] > rs.conf() { "_id" : "rs0", "version" : 1, "members" : [ { "_id" : 0, "host" : "localhost:30000" } ] }
7. 我们还可以用rs.status()来查看复制集的运行状态
[javascript] rs0:PRIMARY> rs.status() { "set" : "rs0", "date" : ISODate("2013-07-29T02:15:07Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "localhost:30000", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2185, "optime" : Timestamp(1375062044, 1), "optimeDate" : ISODate("2013-07-29T01:40:44Z"), "self" : true } ], "ok" : 1 }
8. 启动一个新的mongod服务
[javascript] bash-3.2$ mkdir -p /data/db2 bash-3.2$ mongod --dbpath /data/db2 --logpath /data/db2/mongod.log --logappend --fork --port 30001 --replSet rs0 about to fork child process, waiting until server is ready for connections. forked process: 3118 all output going to: /data/db2/mongod.log child process started successfully, parent exiting
9. 将新的mongod添加到复制集里
[javascript] rs0:PRIMARY> rs.add("localhost:30001") { "ok" : 1 }
10. 再启动一个新的mongod服务来作为仲裁节点
[javascript] bash-3.2$ mkdir -p /data/arb bash-3.2$ mongod --dbpath /data/arb --logpath /data/arb/mongod.log --logappend --fork --port 30002 --replSet rs0 about to fork child process, waiting until server is ready for connections. forked process: 3124 all output going to: /data/arb/mongod.log child process started successfully, parent exiting
11. 将新启动的mongod服务作为仲裁节点添加到复制集中
[javascript] rs0:PRIMARY> rs.addArb("localhost:30002") { "ok" : 1 }
12. 查看复制集的状态
[javascript] rs0:PRIMARY> rs.status() { "set" : "rs0", "date" : ISODate("2013-07-29T02:18:20Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "localhost:30000", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2378, "optime" : Timestamp(1375064276, 1), "optimeDate" : ISODate("2013-07-29T02:17:56Z"), "self" : true }, { "_id" : 1, "name" : "localhost:30001", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 34, "optime" : Timestamp(1375064276, 1), "optimeDate" : ISODate("2013-07-29T02:17:56Z"), "lastHeartbeat" : ISODate("2013-07-29T02:18:18Z"), "lastHeartbeatRecv" : ISODate("2013-07-29T02:18:18Z"), "pingMs" : 0, "syncingTo" : "localhost:30000" }, { "_id" : 2, "name" : "localhost:30002", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 24, "lastHeartbeat" : ISODate("2013-07-29T02:18:18Z"), "lastHeartbeatRecv" : ISODate("2013-07-29T02:18:19Z"), "pingMs" : 0 } ], "ok" : 1 }
从上面的步骤我们可以看到,只需要先停止单节点的mongod,再用复制集的参数来启动mongod,然后初始化复制集,就可以把一个单节点的mongod转换成含有一个节点的复制集,随后就可以往这个复制集里添加从节点和仲裁节点。有了这么一个复制集的环境,我们就可以开始学习如何管理操作复制集了。