在自己的虚拟机环境中,对MongoDB复制集进行了测试,整个过程如下:
MongoDB复制集简介
# #/usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r0 --fork --port 28010 --dbpath /data02/mongors/data/r0 --logpath=/data02/mongors/log/r0.log --logappend about to fork child process, waiting until server is ready for connections. forked process: 10984 child process started successfully, parent exiting # #/usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend about to fork child process, waiting until server is ready for connections. forked process: 11030 child process started successfully, parent exiting # #/usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend about to fork child process, waiting until server is ready for connections. forked process: 11076 child process started successfully, parent exiting #
#ps -ef |grep mongo root 5715 1 0 Apr09 ? 00:11:50 mongod --dbpath=/data02/mongodb/db/ --logpath=/data02/mongodb/logs/mongodb.log --fork root 10984 1 0 14:23 ? 00:00:00 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r0 --fork --port 28010 --dbpath /data02/mongors/data/r0 --logpath=/data02/mongors/log/r0.log --logappend root 11030 1 0 14:23 ? 00:00:00 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend root 11076 1 0 14:23 ? 00:00:00 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend root 11121 10769 0 14:23 pts/1 00:00:00 grep mongo # # #netstat -tunlp |grep 28010 tcp 0 0 0.0.0.0:28010 0.0.0.0:* LISTEN 10984/mongod # #netstat -tunlp | grep 28011 tcp 0 0 0.0.0.0:28011 0.0.0.0:* LISTEN 11030/mongod # #netstat -tunlp | grep 28012 tcp 0 0 0.0.0.0:28012 0.0.0.0:* LISTEN 11076/mongod #
#/usr/local/mongodb/bin/mongo -port 28010 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28010/test > > show databases; admin (empty) local 0.078GB > > show collections; 2015-04-13T14:26:59.980+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131 > > config = {_id: 'rs1', members: [ ... {_id: 0, host: 'localhost:28010',priority:1}, ... {_id: 1, host: 'localhost:28011'}, ... {_id: 2, host: 'localhost:28012'}] ... } { "_id" : "rs1", "members" : [ { "_id" : 0, "host" : "localhost:28010", "priority" : 1 }, { "_id" : 1, "host" : "localhost:28011" }, { "_id" : 2, "host" : "localhost:28012" } ] } > > rs.initiate(config); { "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } > rs1:PRIMARY> rs1:PRIMARY>复制集配置并初始化完毕后,20810的前段自动产生了 rs1:PRIMARY 的标志。
rs1:PRIMARY> rs1:PRIMARY> rs.status() { "set" : "rs1", "date" : ISODate("2015-04-13T06:40:12Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "localhost:28010", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1014, "optime" : Timestamp(1428907084, 1), "optimeDate" : ISODate("2015-04-13T06:38:04Z"), "electionTime" : Timestamp(1428907094, 1), "electionDate" : ISODate("2015-04-13T06:38:14Z"), "self" : true }, { "_id" : 1, "name" : "localhost:28011", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 126, "optime" : Timestamp(1428907084, 1), "optimeDate" : ISODate("2015-04-13T06:38:04Z"), "lastHeartbeat" : ISODate("2015-04-13T06:40:10Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T06:40:11Z"), "pingMs" : 0, "syncingTo" : "localhost:28010" }, { "_id" : 2, "name" : "localhost:28012", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 126, "optime" : Timestamp(1428907084, 1), "optimeDate" : ISODate("2015-04-13T06:38:04Z"), "lastHeartbeat" : ISODate("2015-04-13T06:40:10Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T06:40:11Z"), "pingMs" : 0, "syncingTo" : "localhost:28010" } ], "ok" : 1 } rs1:PRIMARY> rs1:PRIMARY> rs1:PRIMARY> rs.isMaster() { "setName" : "rs1", "setVersion" : 1, "ismaster" : true, "secondary" : false, "hosts" : [ "localhost:28010", "localhost:28012", "localhost:28011" ], "primary" : "localhost:28010", "me" : "localhost:28010", "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-04-13T06:40:52.024Z"), "maxWireVersion" : 2, "minWireVersion" : 0, "ok" : 1 } rs1:PRIMARY> rs1:PRIMARY>
rs1:SECONDARY> show collections; 2015-04-13T14:42:58.431+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131 rs1:SECONDARY>
场景二:查看主从操作日志oplog
#/usr/local/mongodb/bin/mongo -port 28010 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28010/test rs1:PRIMARY> rs1:PRIMARY> show databases; admin (empty) local 1.078GB test (empty) rs1:PRIMARY> rs1:PRIMARY> use local switched to db local rs1:PRIMARY> rs1:PRIMARY> show databases; admin (empty) local 1.078GB test (empty) rs1:PRIMARY> rs1:PRIMARY> show collections; me oplog.rs slaves startup_log system.indexes system.replset rs1:PRIMARY> rs1:PRIMARY> db.oplog.rs.find(); { "ts" : Timestamp(1428907084, 1), "h" : NumberLong(0), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } } rs1:PRIMARY> rs1:PRIMARY> db.printReplicationInfo(); configured oplog size: 990MB log length start to end: 0secs (0hrs) oplog first event time: Mon Apr 13 2015 14:38:04 GMT+0800 (CST) oplog last event time: Mon Apr 13 2015 14:38:04 GMT+0800 (CST) now: Mon Apr 13 2015 14:46:44 GMT+0800 (CST) rs1:PRIMARY> rs1:PRIMARY> db.printSlaveReplicationInfo(); source: localhost:28011 syncedTo: Mon Apr 13 2015 14:38:04 GMT+0800 (CST) 0 secs (0 hrs) behind the primary source: localhost:28012 syncedTo: Mon Apr 13 2015 14:38:04 GMT+0800 (CST) 0 secs (0 hrs) behind the primary rs1:PRIMARY> rs1:PRIMARY>
场景三:主从配置信息查看与确认
rs1:PRIMARY> db local rs1:PRIMARY> rs1:PRIMARY> show collections; me oplog.rs slaves startup_log system.indexes system.replset rs1:PRIMARY> rs1:PRIMARY> db.system.replset.find(); { "_id" : "rs1", "version" : 1, "members" : [ { "_id" : 0, "host" : "localhost:28010" }, { "_id" : 1, "host" : "localhost:28011" }, { "_id" : 2, "host" : "localhost:28012" } ] } rs1:PRIMARY> rs1:PRIMARY> rs.conf(); { "_id" : "rs1", "version" : 1, "members" : [ { "_id" : 0, "host" : "localhost:28010" }, { "_id" : 1, "host" : "localhost:28011" }, { "_id" : 2, "host" : "localhost:28012" } ] } rs1:PRIMARY> #/usr/local/mongodb/bin/mongo -port 28011 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28011/test rs1:SECONDARY> rs1:SECONDARY> rs1:SECONDARY> db test rs1:SECONDARY> show collections; 2015-04-13T14:51:32.097+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131 rs1:SECONDARY> rs1:SECONDARY> rs.conf() { "_id" : "rs1", "version" : 1, "members" : [ { "_id" : 0, "host" : "localhost:28010" }, { "_id" : 1, "host" : "localhost:28011" }, { "_id" : 2, "host" : "localhost:28012" } ] } rs1:SECONDARY> rs1:SECONDARY> db.printSlaveReplicationInfo() source: localhost:28011 syncedTo: Mon Apr 13 2015 14:50:54 GMT+0800 (CST) 0 secs (0 hrs) behind the primary source: localhost:28012 syncedTo: Mon Apr 13 2015 14:50:54 GMT+0800 (CST) 0 secs (0 hrs) behind the primary rs1:SECONDARY>
场景四:管理维护ReplicaSets实现读写分离和故障转移
#/usr/local/mongodb/bin/mongo -port 28010 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28010/test rs1:PRIMARY> rs1:PRIMARY> db test rs1:PRIMARY> show collections; rs1:PRIMARY> rs1:PRIMARY> db.c1.insert({age:30}); WriteResult({ "nInserted" : 1 }) rs1:PRIMARY> rs1:PRIMARY> show collections; c1 system.indexes rs1:PRIMARY> rs1:PRIMARY> db.c1.find(); { "_id" : ObjectId("552b674ee7b1c3fc735c8316"), "age" : 30 } rs1:PRIMARY> rs1:PRIMARY> quit(); [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongo -port 28011 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28011/test rs1:SECONDARY> rs1:SECONDARY> rs1:SECONDARY> db test rs1:SECONDARY> show collections; 2015-04-13T14:51:32.097+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131 rs1:SECONDARY> rs1:SECONDARY> rs.conf() { "_id" : "rs1", "version" : 1, "members" : [ { "_id" : 0, "host" : "localhost:28010" }, { "_id" : 1, "host" : "localhost:28011" }, { "_id" : 2, "host" : "localhost:28012" } ] } rs1:SECONDARY> rs1:SECONDARY> db.printSlaveReplicationInfo() source: localhost:28011 syncedTo: Mon Apr 13 2015 14:50:54 GMT+0800 (CST) 0 secs (0 hrs) behind the primary source: localhost:28012 syncedTo: Mon Apr 13 2015 14:50:54 GMT+0800 (CST) 0 secs (0 hrs) behind the primary rs1:SECONDARY> rs1:SECONDARY> db.getMongo().setSlaveOk(); rs1:SECONDARY> rs1:SECONDARY> show collections; c1 system.indexes rs1:SECONDARY> rs1:SECONDARY> db.c1.find(); { "_id" : ObjectId("552b674ee7b1c3fc735c8316"), "age" : 30 } rs1:SECONDARY> rs1:SECONDARY> quit(); #
故障转移演示:
#echo "kill primary to Fail over" kill primary to Fail over [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]#ps -ef|grep mongo root 5715 1 0 Apr09 ? 00:11:55 mongod --dbpath=/data02/mongodb/db/ --logpath=/data02/mongodb/logs/mongodb.log --fork root 10984 1 0 14:23 ? 00:00:06 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r0 --fork --port 28010 --dbpath /data02/mongors/data/r0 --logpath=/data02/mongors/log/r0.log --logappend root 11030 1 0 14:23 ? 00:00:05 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend root 11076 1 0 14:23 ? 00:00:05 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend root 11731 1 0 14:58 ? 00:00:01 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dbpath /data02/mongors/data/r3 --logpath=/data02/mongors/log/r3.log --logappend root 12186 10769 0 15:05 pts/1 00:00:00 grep mongo [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongo -port 28010 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28010/test rs1:PRIMARY> rs1:PRIMARY> rs.isMaster(); { "setName" : "rs1", "setVersion" : 2, "ismaster" : true, "secondary" : false, "hosts" : [ "localhost:28010", "localhost:28013", "localhost:28012", "localhost:28011" ], "primary" : "localhost:28010", "me" : "localhost:28010", "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-04-13T07:06:06.185Z"), "maxWireVersion" : 2, "minWireVersion" : 0, "ok" : 1 } rs1:PRIMARY> rs1:PRIMARY> quit(); [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]#ps -ef|grep mongod root 5715 1 0 Apr09 ? 00:11:55 mongod --dbpath=/data02/mongodb/db/ --logpath=/data02/mongodb/logs/mongodb.log --fork root 10984 1 0 14:23 ? 00:00:06 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r0 --fork --port 28010 --dbpath /data02/mongors/data/r0 --logpath=/data02/mongors/log/r0.log --logappend root 11030 1 0 14:23 ? 00:00:06 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend root 11076 1 0 14:23 ? 00:00:06 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend root 11731 1 0 14:58 ? 00:00:01 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dbpath /data02/mongors/data/r3 --logpath=/data02/mongors/log/r3.log --logappend root 12237 10769 0 15:06 pts/1 00:00:00 grep mongod [root@MySQL193 /data02]# [root@MySQL193 /data02]#ps aux |grep mongod root 5715 0.2 2.1 1660908 41588 ? Sl Apr09 11:55 mongod --dbpath=/data02/mongodb/db/ --logpath=/data02/mongodb/logs/mongodb.log --fork root 10984 0.2 2.1 2703968 41512 ? Sl 14:23 0:06 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r0 --fork --port 28010 --dbpath /data02/mongors/data/r0 --logpath=/data02/mongors/log/r0.log --logappend root 11030 0.2 1.9 2683292 36708 ? Sl 14:23 0:06 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend root 11076 0.2 1.9 2683288 36668 ? Sl 14:23 0:06 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend root 11731 0.2 1.9 2684316 37188 ? Sl 14:58 0:01 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dbpath /data02/mongors/data/r3 --logpath=/data02/mongors/log/r3.log --logappend root 12245 0.0 0.0 103252 832 pts/1 S+ 15:06 0:00 grep mongod [root@MySQL193 /data02]# [root@MySQL193 /data02]#kill -9 10984 [root@MySQL193 /data02]# [root@MySQL193 /data02]#ps -ef|grep mongod root 5715 1 0 Apr09 ? 00:11:55 mongod --dbpath=/data02/mongodb/db/ --logpath=/data02/mongodb/logs/mongodb.log --fork root 11030 1 0 14:23 ? 00:00:06 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend root 11076 1 0 14:23 ? 00:00:06 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend root 11731 1 0 14:58 ? 00:00:01 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dbpath /data02/mongors/data/r3 --logpath=/data02/mongors/log/r3.log --logappend root 12289 10769 0 15:07 pts/1 00:00:00 grep mongod [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongo -port 28013 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28013/test rs1:SECONDARY> rs1:SECONDARY> rs.status(); { "set" : "rs1", "date" : ISODate("2015-04-13T07:07:41Z"), "myState" : 2, "syncingTo" : "localhost:28011", "members" : [ { "_id" : 0, "name" : "localhost:28010", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(1428908577, 1), "optimeDate" : ISODate("2015-04-13T07:02:57Z"), "lastHeartbeat" : ISODate("2015-04-13T07:07:40Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T07:07:12Z"), "pingMs" : 0 }, { "_id" : 1, "name" : "localhost:28011", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 462, "optime" : Timestamp(1428908577, 1), "optimeDate" : ISODate("2015-04-13T07:02:57Z"), "lastHeartbeat" : ISODate("2015-04-13T07:07:39Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T07:07:39Z"), "pingMs" : 0, "electionTime" : Timestamp(1428908838, 1), "electionDate" : ISODate("2015-04-13T07:07:18Z") }, { "_id" : 2, "name" : "localhost:28012", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 462, "optime" : Timestamp(1428908577, 1), "optimeDate" : ISODate("2015-04-13T07:02:57Z"), "lastHeartbeat" : ISODate("2015-04-13T07:07:39Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T07:07:39Z"), "pingMs" : 0, "lastHeartbeatMessage" : "syncing to: localhost:28011", "syncingTo" : "localhost:28011" }, { "_id" : 3, "name" : "localhost:28013", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 578, "optime" : Timestamp(1428908577, 1), "optimeDate" : ISODate("2015-04-13T07:02:57Z"), "infoMessage" : "syncing to: localhost:28011", "self" : true } ], "ok" : 1 } rs1:SECONDARY> rs1:SECONDARY> rs1:SECONDARY> quit(); [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongo -port 28011 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28011/test rs1:PRIMARY> rs1:PRIMARY> db test rs1:PRIMARY> rs1:PRIMARY> show collections; c1 system.indexes rs1:PRIMARY> rs1:PRIMARY> db.c1.find(); { "_id" : ObjectId("552b674ee7b1c3fc735c8316"), "age" : 30 } { "_id" : ObjectId("552b6a21572fc5a02732f62c"), "age" : 40 } rs1:PRIMARY> rs1:PRIMARY> db.c1.insert({age:50}); WriteResult({ "nInserted" : 1 }) rs1:PRIMARY> rs1:PRIMARY> db.c1.find(); { "_id" : ObjectId("552b674ee7b1c3fc735c8316"), "age" : 30 } { "_id" : ObjectId("552b6a21572fc5a02732f62c"), "age" : 40 } { "_id" : ObjectId("552b6ba3cb6fc10cc6fc9085"), "age" : 50 } rs1:PRIMARY> rs1:PRIMARY> rs.conf(); { "_id" : "rs1", "version" : 2, "members" : [ { "_id" : 0, "host" : "localhost:28010" }, { "_id" : 1, "host" : "localhost:28011" }, { "_id" : 2, "host" : "localhost:28012" }, { "_id" : 3, "host" : "localhost:28013" } ] } rs1:PRIMARY> rs1:PRIMARY> db.printSlaveReplicationInfo() source: localhost:28010 syncedTo: Mon Apr 13 2015 15:02:57 GMT+0800 (CST) 386 secs (0.11 hrs) behind the primary source: localhost:28012 syncedTo: Mon Apr 13 2015 15:09:23 GMT+0800 (CST) 0 secs (0 hrs) behind the primary source: localhost:28013 syncedTo: Mon Apr 13 2015 15:09:23 GMT+0800 (CST) 0 secs (0 hrs) behind the primary rs1:PRIMARY> rs1:PRIMARY> quit() [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongo -port 28013 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28013/test rs1:SECONDARY> rs1:SECONDARY> rs1:SECONDARY> db test rs1:SECONDARY> rs1:SECONDARY> show collections; 2015-04-13T15:11:12.092+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131 rs1:SECONDARY> rs1:SECONDARY> db.getMongo().setSlaveOk(); rs1:SECONDARY> rs1:SECONDARY> show collections; c1 system.indexes rs1:SECONDARY> rs1:SECONDARY> db.c1.find(); { "_id" : ObjectId("552b674ee7b1c3fc735c8316"), "age" : 30 } { "_id" : ObjectId("552b6a21572fc5a02732f62c"), "age" : 40 } { "_id" : ObjectId("552b6ba3cb6fc10cc6fc9085"), "age" : 50 } rs1:SECONDARY> rs1:SECONDARY> rs1:SECONDARY> quit() [root@MySQL193 /data02]#
场景五:增加和减少复制集中的节点
增加节点步骤
#echo " add a new host into rs1" add a new host into rs1 [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]#pwd /data02 [root@MySQL193 /data02]#ll total 12 drwxr-xr-x 4 root root 4096 Apr 9 14:09 mongodb drwxr-xr-x 5 root root 4096 Apr 13 14:20 mongors drwxr-xr-x 3 mysql mysql 4096 Apr 9 17:45 mysqldata [root@MySQL193 /data02]# [root@MySQL193 /data02]#mkdir -p /data02/mongors/data/r3 [root@MySQL193 /data02]# [root@MySQL193 /data02]#echo " this is rs1 super secret key " > /data02/mongors/key/r3 [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]#chmod 600 /data02/mongors/key/r3 [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dpath /data02/mongors/data/r3 --logpath=/data02/mongors/log/r3.log --logappend -bash: /usr/local/bin/mongod: No such file or directory [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dpath /data02/mongors/data/r3 --logpath=/data02/mongors/log/r3.log --logappend Error parsing command line: unknown option dpath try '/usr/local/mongodb/bin/mongod --help' for more information [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dbpath /data02/mongors/data/r3 --logpath=/data02/mongors/log/r3.log --logappend about to fork child process, waiting until server is ready for connections. forked process: 11731 child process started successfully, parent exiting [root@MySQL193 /data02]# [root@MySQL193 /data02]#ps -ef|grep mongo root 5715 1 0 Apr09 ? 00:11:54 mongod --dbpath=/data02/mongodb/db/ --logpath=/data02/mongodb/logs/mongodb.log --fork root 10984 1 0 14:23 ? 00:00:05 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r0 --fork --port 28010 --dbpath /data02/mongors/data/r0 --logpath=/data02/mongors/log/r0.log --logappend root 11030 1 0 14:23 ? 00:00:04 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend root 11076 1 0 14:23 ? 00:00:04 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend root 11731 1 0 14:58 ? 00:00:00 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dbpath /data02/mongors/data/r3 --logpath=/data02/mongors/log/r3.log --logappend root 11782 10769 0 14:58 pts/1 00:00:00 grep mongo [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dbpath /data02/mongors/data/r3 --logpata02/mongors/key/r3 --fork --port 28013 --dpath /data02/mongors/data/r3 --logpa[root@MySQL193 /data02]#/usr/local/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r3 --fork --port 28013 --dpath /data02/mongors/data/r3 --logpath=/data[root@MySQL193 /data02]#chmod 600 /data02/mongors/key/r3 [root@MySQL193 /data02]#echo " this is rs1 super secret key " > /data02/mongors/[root@MySQL193 /data02]#mkdir -p /data02/mongors/data/r3 [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongo -port 28013 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28013/test > > > rs.status(); { "startupStatus" : 3, "info" : "run rs.initiate(...) if not yet done for the set", "ok" : 0, "errmsg" : "can't get local.system.replset config from self or any seed (EMPTYCONFIG)" } > > rs.add("localhost:28013"); assert failed : no config object retrievable from local.system.replset Error: assert failed : no config object retrievable from local.system.replset at Error (<anonymous>) at doassert (src/mongo/shell/assert.js:11:14) at assert (src/mongo/shell/assert.js:20:5) at Function.rs.add (src/mongo/shell/utils.js:979:5) at (shell):1:4 2015-04-13T14:59:13.271+0800 Error: assert failed : no config object retrievable from local.system.replset at src/mongo/shell/assert.js:13 > > quit(); [root@MySQL193 /data02]# [root@MySQL193 /data02]# [root@MySQL193 /data02]#/usr/local/mongodb/bin/mongo -port 28010 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28010/test rs1:PRIMARY> rs1:PRIMARY> rs1:PRIMARY> rs.status(); { "set" : "rs1", "date" : ISODate("2015-04-13T06:59:34Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "localhost:28010", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2176, "optime" : Timestamp(1428907854, 1), "optimeDate" : ISODate("2015-04-13T06:50:54Z"), "electionTime" : Timestamp(1428907094, 1), "electionDate" : ISODate("2015-04-13T06:38:14Z"), "self" : true }, { "_id" : 1, "name" : "localhost:28011", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1288, "optime" : Timestamp(1428907854, 1), "optimeDate" : ISODate("2015-04-13T06:50:54Z"), "lastHeartbeat" : ISODate("2015-04-13T06:59:33Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T06:59:33Z"), "pingMs" : 0, "syncingTo" : "localhost:28010" }, { "_id" : 2, "name" : "localhost:28012", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1288, "optime" : Timestamp(1428907854, 1), "optimeDate" : ISODate("2015-04-13T06:50:54Z"), "lastHeartbeat" : ISODate("2015-04-13T06:59:33Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T06:59:33Z"), "pingMs" : 0, "syncingTo" : "localhost:28010" } ], "ok" : 1 } rs1:PRIMARY> rs1:PRIMARY> rs.add("localhost:28013"); { "ok" : 1 } rs1:PRIMARY> rs1:PRIMARY> rs.status(); { "set" : "rs1", "date" : ISODate("2015-04-13T06:59:59Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "localhost:28010", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2201, "optime" : Timestamp(1428908396, 1), "optimeDate" : ISODate("2015-04-13T06:59:56Z"), "electionTime" : Timestamp(1428907094, 1), "electionDate" : ISODate("2015-04-13T06:38:14Z"), "self" : true }, { "_id" : 1, "name" : "localhost:28011", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1313, "optime" : Timestamp(1428908396, 1), "optimeDate" : ISODate("2015-04-13T06:59:56Z"), "lastHeartbeat" : ISODate("2015-04-13T06:59:59Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T06:59:57Z"), "pingMs" : 0, "syncingTo" : "localhost:28010" }, { "_id" : 2, "name" : "localhost:28012", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1313, "optime" : Timestamp(1428908396, 1), "optimeDate" : ISODate("2015-04-13T06:59:56Z"), "lastHeartbeat" : ISODate("2015-04-13T06:59:59Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T06:59:59Z"), "pingMs" : 0, "syncingTo" : "localhost:28010" }, { "_id" : 3, "name" : "localhost:28013", "health" : 1, "state" : 5, "stateStr" : "STARTUP2", "uptime" : 3, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2015-04-13T06:59:58Z"), "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"), "pingMs" : 0, "lastHeartbeatMessage" : "initial sync need a member to be primary or secondary to do our initial sync" } ], "ok" : 1 } rs1:PRIMARY>
删除节点步骤
#/usr/local/mongodb/bin/mongo -port 28011 MongoDB shell version: 2.6.5 connecting to: 127.0.0.1:28011/test rs1:PRIMARY> rs1:PRIMARY> rs.conf() { "_id" : "rs1", "version" : 2, "members" : [ { "_id" : 0, "host" : "localhost:28010" }, { "_id" : 1, "host" : "localhost:28011" }, { "_id" : 2, "host" : "localhost:28012" }, { "_id" : 3, "host" : "localhost:28013" } ] } rs1:PRIMARY> rs1:PRIMARY> rs.remove("localhost:28013"); 2015-04-13T15:24:52.181+0800 DBClientCursor::init call() failed 2015-04-13T15:24:52.182+0800 Error: error doing query: failed at src/mongo/shell/query.js:81 2015-04-13T15:24:52.183+0800 trying reconnect to 127.0.0.1:28011 (127.0.0.1) failed 2015-04-13T15:24:52.184+0800 reconnect 127.0.0.1:28011 (127.0.0.1) ok rs1:PRIMARY> rs1:PRIMARY> rs.conf() { "_id" : "rs1", "version" : 3, "members" : [ { "_id" : 0, "host" : "localhost:28010" }, { "_id" : 1, "host" : "localhost:28011" }, { "_id" : 2, "host" : "localhost:28012" } ] } rs1:PRIMARY> rs1:PRIMARY> rs.status() { "set" : "rs1", "date" : ISODate("2015-04-13T07:25:15Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "localhost:28010", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 23, "optime" : Timestamp(1428909892, 1), "optimeDate" : ISODate("2015-04-13T07:24:52Z"), "lastHeartbeat" : ISODate("2015-04-13T07:25:14Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T07:25:14Z"), "pingMs" : 0, "lastHeartbeatMessage" : "syncing to: localhost:28011", "syncingTo" : "localhost:28011" }, { "_id" : 1, "name" : "localhost:28011", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 3699, "optime" : Timestamp(1428909892, 1), "optimeDate" : ISODate("2015-04-13T07:24:52Z"), "electionTime" : Timestamp(1428908838, 1), "electionDate" : ISODate("2015-04-13T07:07:18Z"), "self" : true }, { "_id" : 2, "name" : "localhost:28012", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 23, "optime" : Timestamp(1428909892, 1), "optimeDate" : ISODate("2015-04-13T07:24:52Z"), "lastHeartbeat" : ISODate("2015-04-13T07:25:14Z"), "lastHeartbeatRecv" : ISODate("2015-04-13T07:25:15Z"), "pingMs" : 0, "lastHeartbeatMessage" : "syncing to: localhost:28011", "syncingTo" : "localhost:28011" } ], "ok" : 1 } rs1:PRIMARY> rs1:PRIMARY> rs1:PRIMARY> rs1:PRIMARY> quit()