时间:2021-07-01 10:21:17 帮助过:3人阅读
然后启动所有的实例:[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"
}
]
}
可以看到当前的members只有一个,即_id为0的,即我们刚刚初始化的这个节点.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,