发布网友 发布时间:2022-04-09 06:46
共1个回答
热心网友 时间:2022-04-09 08:15
1.停止单节点的mongod我们可以从mongoshell里运行db.shutdownServer()命令来停止mongod[javascript]>useadmin;switchedtodbadmin>db.shutdownServer()2.使用--replSet的选项加上新的复制集的名字来重新启动这个mongod[javascript]bash-3.2$mongod--dbpath/data/db--logpath/data/mongod.log--logappend--fork--port30000--replSetrs0abouttoforkchildprocess,waitinguntilserverisreadyforconnections.forkedprocess:3092alloutputgoingto:/data/mongod.logchildprocessstartedsuccessfully,parentexiting3.连接刚才启动的数据库[javascript]bash-3.2$mongo--port30000MongoDBshellversion:2.4.5connectingto:127.0.0.1:30000/testServerhasstartupwarnings:MonJul2911:28:02.291[initandlisten]MonJul2911:28:02.291[initandlisten]**WARNING:softrlimitstoolow.Numberoffilesis256,shouldbeatleast10004.在mongoshell里创建一个包含复制集配置信息的对象[javascript]rsconf={_id:"rs0",members:[{_id:0,host:"localhost:30000"}]}5.用rs.initiate()来初始化复制集[javascript]>rs.initiate(rsconf){"info":"Confignowsavedlocally.Shouldcomeonlineinaboutaminute.","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/db2bash-3.2$mongod--dbpath/data/db2--logpath/data/db2/mongod.log--logappend--fork--port30001--replSetrs0abouttoforkchildprocess,waitinguntilserverisreadyforconnections.forkedprocess:3118alloutputgoingto:/data/db2/mongod.logchildprocessstartedsuccessfully,parentexiting9.将新的mongod添加到复制集里[javascript]rs0:PRIMARY>rs.add("localhost:30001"){"ok":1}10.再启动一个新的mongod服务来作为仲裁节点[javascript]bash-3.2$mkdir-p/data/arbbash-3.2$mongod--dbpath/data/arb--logpath/data/arb/mongod.log--logappend--fork--port30002--replSetrs0abouttoforkchildprocess,waitinguntilserverisreadyforconnections.forkedprocess:3124alloutputgoingto:/data/arb/mongod.logchildprocessstartedsuccessfully,parentexiting11.将新启动的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}