[root@mongodb1 ~]# cat /etc/mongod.conf
port=27017
dbpath=/data/db
logpath=/data/log/mongod.log
fork = true
oplogSize=500
replSet = rs0
[root@mongodb1 ~]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2722
child process started successfully, parent exiting
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "mongodb1:27017",
"ok" : 1
}
rs0:OTHER>
当然你也可以先配置一个配置文件,然后使用rs.initiate(rsconf)来初始化,例如:
rsconf = {
_id: "rs0",
members: [
{
_id: 0,
host: "<hostname>:27017"
}
]
}
rs0:OTHER> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("575647b35e9005faa0e8d690")
}
}
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2016-06-07T04:16:20.542Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "mongodb1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1184,
"optime" : {
"ts" : Timestamp(1465272244, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-06-07T04:04:04Z"),
"electionTime" : Timestamp(1465272243, 2),
"electionDate" : ISODate("2016-06-07T04:04:03Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
rs0:PRIMARY> rs.add("mongodb2:27017")
{ "ok" : 1 }
rs0:PRIMARY> rs.add("mongodb3:27017")
{ "ok" : 1 }
再次查看复制集配置,可以看到members有三个成员了.
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 3,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodb2:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodb3:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("575647b35e9005faa0e8d690")
}
}
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2016-06-07T04:28:12.721Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "mongodb1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1896,
"optime" : {
"ts" : Timestamp(1465273627, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-06-07T04:27:07Z"),
"electionTime" : Timestamp(1465272243, 2),
"electionDate" : ISODate("2016-06-07T04:04:03Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "mongodb2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 73,
"optime" : {
"ts" : Timestamp(1465273627, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-06-07T04:27:07Z"),
"lastHeartbeat" : ISODate("2016-06-07T04:28:11.197Z"),
"lastHeartbeatRecv" : ISODate("2016-06-07T04:28:12.206Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mongodb1:27017",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "mongodb3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 65,
"optime" : {
"ts" : Timestamp(1465273627, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-06-07T04:27:07Z"),
"lastHeartbeat" : ISODate("2016-06-07T04:28:11.198Z"),
"lastHeartbeatRecv" : ISODate("2016-06-07T04:28:09.224Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1
}
2016-06-07T12:26:57.846+0800 I REPL [ReplicationExecutor] This node is mongodb2:27017 in the config
2016-06-07T12:26:57.846+0800 I REPL [ReplicationExecutor] transition to STARTUP2
2016-06-07T12:26:57.846+0800 I REPL [rsSync] ******
2016-06-07T12:26:57.846+0800 I REPL [rsSync] creating replication oplog of size: 500MB...
2016-06-07T12:26:57.846+0800 I REPL [ReplicationExecutor] Member mongodb1:27017 is now in state PRIMARY
2016-06-07T12:26:57.858+0800 I STORAGE [rsSync] Starting WiredTigerRecordStoreThread local.oplog.rs
2016-06-07T12:26:57.858+0800 I STORAGE [rsSync] The size storer reports that the oplog contains 0 records totaling to 0 bytes
2016-06-07T12:26:57.858+0800 I STORAGE [rsSync] Scanning the oplog to determine where to place markers for truncation
2016-06-07T12:26:57.922+0800 I REPL [rsSync] ******
2016-06-07T12:26:57.922+0800 I REPL [rsSync] initial sync pending
2016-06-07T12:26:57.940+0800 I REPL [ReplicationExecutor] syncing from: mongodb1:27017
2016-06-07T12:26:57.943+0800 I REPL [rsSync] initial sync drop all databases
2016-06-07T12:26:57.943+0800 I STORAGE [rsSync] dropAllDatabasesExceptLocal 2
2016-06-07T12:26:57.966+0800 I REPL [rsSync] initial sync clone all databases
2016-06-07T12:26:57.967+0800 I REPL [rsSync] initial sync cloning db: suq
...
...
...
2016-06-07T12:27:00.234+0800 I REPL [rsSync] oplog sync 3 of 3
2016-06-07T12:27:00.235+0800 I REPL [rsSync] initial sync finishing up
2016-06-07T12:27:00.235+0800 I REPL [rsSync] set minValid=(term: 1, timestamp: Jun 7 12:26:58:1)
2016-06-07T12:27:00.240+0800 I REPL [rsSync] initial sync done
2016-06-07T12:27:00.241+0800 I REPL [ReplicationExecutor] transition to RECOVERING
2016-06-07T12:27:00.242+0800 I REPL [ReplicationExecutor] transition to SECONDARY
rs0:SECONDARY> use suq
switched to db suq
rs0:SECONDARY> rs.slaveOk()
rs0:SECONDARY> show collections
t1
test2
test3
test4
test5
test6
test7
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 3,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodb2:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodb3:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("575647b35e9005faa0e8d690")
}
}
rs0:PRIMARY> rsconfig=rs.conf()
rs0:PRIMARY> rsconfig.members[2].priority = 0.5
0.5
rs0:PRIMARY> rs.reconfig(rsconfig)
{ "ok" : 1 }
{
"_id" : 2,
"host" : "mongodb3:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0.5,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
rs0:SECONDARY> rs.help()
rs.status() { replSetGetStatus : 1 } checks repl set status
rs.initiate() { replSetInitiate : null } initiates set with default settings
rs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg
rs.conf() get the current configuration object from local.system.replset
rs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)
rs.add(hostportstr) add a new member to the set with default attributes (disconnects)
rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)
rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)
rs.stepDown([stepdownSecs, catchUpSecs]) step down as primary (disconnects)
rs.syncFrom(hostportstr) make a secondary sync from the given member
rs.freeze(secs) make a node ineligible to become primary for the time specified
rs.remove(hostportstr) remove a host from the replica set (disconnects)
rs.slaveOk() allow queries on secondary nodes
rs.printReplicationInfo() check oplog size and time range
rs.printSlaveReplicationInfo() check replica set members and replication lag
db.isMaster() check who is primary
reconfiguration helpers disconnect from the database so the shell will display
an error, even if the command succeeds.
rs0:SECONDARY> rs.isMaster()
{
"hosts" : [
"mongodb1:27017",
"mongodb2:27017",
"mongodb3:27017"
],
"setName" : "rs0",
"setVersion" : 4,
"ismaster" : false,
"secondary" : true,
"primary" : "mongodb1:27017",
"me" : "mongodb3:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2016-06-07T12:08:44.914Z"),
"maxWireVersion" : 4,
"minWireVersion" : 0,
"ok" : 1
}
rs.add('mongodb3.example.net:27017')
rs.add({ host: "mongodb3:27017", priority: 0 })
rs.add("mongodb3:27010", true)
rs0:PRIMARY> rs.add({"host":"mongodb3:27017","priority":0,"hidden":true})
{ "ok" : 1 }
rs0:PRIMARY> rs.remove("mongodb3:27017")
{ "ok" : 1 }
rs0:PRIMARY> rs.addArb("mongodb3:27017")
{ "ok" : 1 }
rs0:PRIMARY> rs.printReplicationInfo()
configured oplog size: 500MB
log length start to end: 30418secs (8.45hrs)
oplog first event time: Tue Jun 07 2016 12:04:03 GMT+0800 (CST)
oplog last event time: Tue Jun 07 2016 20:31:01 GMT+0800 (CST)
now: Tue Jun 07 2016 20:39:09 GMT+0800 (CST)
rs0:SECONDARY> rs.printSlaveReplicationInfo()
source: mongodb2:27017
syncedTo: Tue Jun 07 2016 20:31:01 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
rs0:PRIMARY> rs.stepDown()
2016-06-07T20:58:53.657+0800 E QUERY [thread1] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:27017' :
DB.prototype.runCommand@src/mongo/shell/db.js:135:1
DB.prototype.adminCommand@src/mongo/shell/db.js:153:16
rs.stepDown@src/mongo/shell/utils.js:1181:12
@(shell):1:1
2016-06-07T20:58:53.660+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-06-07T20:58:53.662+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) ok
rs0:SECONDARY>
rs0:SECONDARY>
{
"_id" : 2,
"name" : "mongodb3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 33316,
"optime" : {
"ts" : Timestamp(1465304797, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2016-06-07T13:06:37Z"),
"syncingTo" : "mongodb2:27017",
"configVersion" : 14,
"self" : true
}
rs0:SECONDARY> rs.syncFrom("mongodb1:27017")
{
"syncFromRequested" : "mongodb1:27017",
"prevSyncTarget" : "mongodb2:27017",
"ok" : 1
}
2016-06-07T21:10:55.416+0800 I REPL [ReplicationExecutor] syncing from: mongodb1:27017 by request
2016-06-07T21:10:55.418+0800 I REPL [SyncSourceFeedback] setting syncSourceFeedback to mongodb1:27017
2016-06-07T21:10:55.419+0800 I ASIO [NetworkInterfaceASIO-BGSync-0] Successfully connected to mongodb1:27017 2016-06-07T21:11:17.124+0800 I ASIO [NetworkInterfaceASIO-BGSync-0] Successfully connected to mongodb1:27017
rs0:SECONDARY> db.runCommand({"resync":1})
{ "ok" : 1 }