MongoDB之副本集
1、環境
2、分別在3個節點安裝配置和啟動MongoDB
[root@D2-POMS15 ~]# rpm -qa | grep openssl
openssl098e-0.9.8e-17.el6_2.2.x86_64
openssl-1.0.1e-15.el6.x86_64
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb
[root@D2-POMS15 ~]# tar -xvzf mongodb-linux-x86_64-rhel62-3.4.7.tgz
[root@D2-POMS15 ~]# mv mongodb-linux-x86_64-rhel62-3.4.7/* /usr/local/mongodb/
[root@D2-POMS15 ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH=$PATH:/usr/local/mongodb/bin/
export PATH
[root@D2-POMS15 ~]# . .bash_profile
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/db
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/log
[root@D2-POMS15 ~]# touch /usr/local/mongodb/log/mongodb.log
[root@D2-POMS15 ~]# vim /usr/local/mongodb/mongodb.conf
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
noauth=true
port=27017
fork=true
replSet=stoners
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11580
child process started successfully, parent exiting
[root@D2-POMS15 ~]# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Welcome to the MongoDB shell.
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS16 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS17 ~]# mongod -f /usr/local/mongodb/mongodb.conf
3、在任一節點進行副本集配置
初始化副本集配置。
語法:
{
_id : <setname>,
members : [
{_id : 0, host : <host0>},
{_id : 1, host : <host1>},
{_id : 2, host : <host2>},
]
}
配置其中一個節點為仲裁的語法:
{
_id : "my_replica_set",
members : [
{_id : 0, host : "rs1.example.net:27017"},
{_id : 1, host : "rs2.example.net:27017"},
{_id : 2, host : "rs3.example.net", arbiterOnly: true},
]
}
> rs.initiate({_id:"stoners",members:[{_id:0,host:"10.163.97.15:27017"}]})
{ "ok" : 1 }
stoners:SECONDARY> rs.conf()
{
"_id" : "stoners",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "10.163.97.15:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 60000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5996b49a33625594149428f2")
}
}
新增節點:
stoners:PRIMARY> rs.add("10.163.97.16")
{ "ok" : 1 }
stoners:PRIMARY> rs.add("10.163.97.17")
{ "ok" : 1 }
檢視狀態:
stoners:PRIMARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T09:49:18.307Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 144,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1503049643, 2),
"electionDate" : ISODate("2017-08-18T09:47:23Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 86,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
"lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.774Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.15:27017",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 52,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
"lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.883Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.15:27017",
"configVersion" : 3
}
],
"ok" : 1
}
4、測試資料寫入
主節點寫入資料:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":1})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
直接查詢副本節點報錯:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
Error: error: {
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
}
設定副本節點可讀:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
另一個副本節點:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
5、測試故障切換
先關閉主節點:
stoners:PRIMARY> use admin
switched to db admin
stoners:PRIMARY> db.shutdownServer();
server should be down...
其中一個副本節點變為了主節點:
stoners:SECONDARY>
stoners:PRIMARY>
stoners:PRIMARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T10:03:49.050Z"),
"myState" : 1,
"term" : NumberLong(2),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:03:47.314Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:01:03.792Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 929,
"optime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:03:44Z"),
"electionTime" : Timestamp(1503050473, 1),
"electionDate" : ISODate("2017-08-18T10:01:13Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 845,
"optime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:03:44Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:03:44Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:03:47.252Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:03:47.998Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.16:27017",
"configVersion" : 3
}
],
"ok" : 1
}
在主節點插入資料:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":2})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
在另一個副本節點檢視:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
現在啟動關閉的節點:
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
啟動後自動成為副本節點:
stoners:SECONDARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T10:08:25.401Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "10.163.97.17:27017",
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 49,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"syncingTo" : "10.163.97.17:27017",
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.753Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1503050473, 1),
"electionDate" : ISODate("2017-08-18T10:01:13Z"),
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.520Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.16:27017",
"configVersion" : 3
}
],
"ok" : 1
}
資料也同步過來了。
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
2、分別在3個節點安裝配置和啟動MongoDB
[root@D2-POMS15 ~]# rpm -qa | grep openssl
openssl098e-0.9.8e-17.el6_2.2.x86_64
openssl-1.0.1e-15.el6.x86_64
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb
[root@D2-POMS15 ~]# tar -xvzf mongodb-linux-x86_64-rhel62-3.4.7.tgz
[root@D2-POMS15 ~]# mv mongodb-linux-x86_64-rhel62-3.4.7/* /usr/local/mongodb/
[root@D2-POMS15 ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH=$PATH:/usr/local/mongodb/bin/
export PATH
[root@D2-POMS15 ~]# . .bash_profile
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/db
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/log
[root@D2-POMS15 ~]# touch /usr/local/mongodb/log/mongodb.log
[root@D2-POMS15 ~]# vim /usr/local/mongodb/mongodb.conf
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
noauth=true
port=27017
fork=true
replSet=stoners
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11580
child process started successfully, parent exiting
[root@D2-POMS15 ~]# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Welcome to the MongoDB shell.
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS16 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS17 ~]# mongod -f /usr/local/mongodb/mongodb.conf
3、在任一節點進行副本集配置
初始化副本集配置。
語法:
{
_id : <setname>,
members : [
{_id : 0, host : <host0>},
{_id : 1, host : <host1>},
{_id : 2, host : <host2>},
]
}
配置其中一個節點為仲裁的語法:
{
_id : "my_replica_set",
members : [
{_id : 0, host : "rs1.example.net:27017"},
{_id : 1, host : "rs2.example.net:27017"},
{_id : 2, host : "rs3.example.net", arbiterOnly: true},
]
}
> rs.initiate({_id:"stoners",members:[{_id:0,host:"10.163.97.15:27017"}]})
{ "ok" : 1 }
stoners:SECONDARY> rs.conf()
{
"_id" : "stoners",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "10.163.97.15:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 60000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5996b49a33625594149428f2")
}
}
新增節點:
stoners:PRIMARY> rs.add("10.163.97.16")
{ "ok" : 1 }
stoners:PRIMARY> rs.add("10.163.97.17")
{ "ok" : 1 }
檢視狀態:
stoners:PRIMARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T09:49:18.307Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 144,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1503049643, 2),
"electionDate" : ISODate("2017-08-18T09:47:23Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 86,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
"lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.774Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.15:27017",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 52,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
"lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.883Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.15:27017",
"configVersion" : 3
}
],
"ok" : 1
}
4、測試資料寫入
主節點寫入資料:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":1})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
直接查詢副本節點報錯:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
Error: error: {
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
}
設定副本節點可讀:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
另一個副本節點:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
5、測試故障切換
先關閉主節點:
stoners:PRIMARY> use admin
switched to db admin
stoners:PRIMARY> db.shutdownServer();
server should be down...
其中一個副本節點變為了主節點:
stoners:SECONDARY>
stoners:PRIMARY>
stoners:PRIMARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T10:03:49.050Z"),
"myState" : 1,
"term" : NumberLong(2),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:03:47.314Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:01:03.792Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 929,
"optime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:03:44Z"),
"electionTime" : Timestamp(1503050473, 1),
"electionDate" : ISODate("2017-08-18T10:01:13Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 845,
"optime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:03:44Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:03:44Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:03:47.252Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:03:47.998Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.16:27017",
"configVersion" : 3
}
],
"ok" : 1
}
在主節點插入資料:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":2})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
在另一個副本節點檢視:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
現在啟動關閉的節點:
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
啟動後自動成為副本節點:
stoners:SECONDARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T10:08:25.401Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "10.163.97.17:27017",
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 49,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"syncingTo" : "10.163.97.17:27017",
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.753Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1503050473, 1),
"electionDate" : ISODate("2017-08-18T10:01:13Z"),
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.520Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.16:27017",
"configVersion" : 3
}
],
"ok" : 1
}
資料也同步過來了。
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
節點 | IP | 作業系統 | MongoDB |
資料節點1 | 10.163.91.15 | RHLE6.5_x64 | mongodb-linux-x86_64-rhel62-3.4.7.tgz |
資料節點2 | 10.163.91.16 | RHLE6.5_x64 | mongodb-linux-x86_64-rhel62-3.4.7.tgz |
資料節點3 | 10.163.91.17 | RHLE6.5_x64 | mongodb-linux-x86_64-rhel62-3.4.7.tgz |
2、分別在3個節點安裝配置和啟動MongoDB
[root@D2-POMS15 ~]# rpm -qa | grep openssl
openssl098e-0.9.8e-17.el6_2.2.x86_64
openssl-1.0.1e-15.el6.x86_64
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb
[root@D2-POMS15 ~]# tar -xvzf mongodb-linux-x86_64-rhel62-3.4.7.tgz
[root@D2-POMS15 ~]# mv mongodb-linux-x86_64-rhel62-3.4.7/* /usr/local/mongodb/
[root@D2-POMS15 ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH=$PATH:/usr/local/mongodb/bin/
export PATH
[root@D2-POMS15 ~]# . .bash_profile
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/db
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/log
[root@D2-POMS15 ~]# touch /usr/local/mongodb/log/mongodb.log
[root@D2-POMS15 ~]# vim /usr/local/mongodb/mongodb.conf
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
noauth=true
port=27017
fork=true
replSet=stoners
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11580
child process started successfully, parent exiting
[root@D2-POMS15 ~]# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Welcome to the MongoDB shell.
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS16 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS17 ~]# mongod -f /usr/local/mongodb/mongodb.conf
3、在任一節點進行副本集配置
初始化副本集配置。
語法:
{
_id : <setname>,
members : [
{_id : 0, host : <host0>},
{_id : 1, host : <host1>},
{_id : 2, host : <host2>},
]
}
配置其中一個節點為仲裁的語法:
{
_id : "my_replica_set",
members : [
{_id : 0, host : "rs1.example.net:27017"},
{_id : 1, host : "rs2.example.net:27017"},
{_id : 2, host : "rs3.example.net", arbiterOnly: true},
]
}
> rs.initiate({_id:"stoners",members:[{_id:0,host:"10.163.97.15:27017"}]})
{ "ok" : 1 }
stoners:SECONDARY> rs.conf()
{
"_id" : "stoners",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "10.163.97.15:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 60000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5996b49a33625594149428f2")
}
}
新增節點:
stoners:PRIMARY> rs.add("10.163.97.16")
{ "ok" : 1 }
stoners:PRIMARY> rs.add("10.163.97.17")
{ "ok" : 1 }
檢視狀態:
stoners:PRIMARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T09:49:18.307Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 144,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1503049643, 2),
"electionDate" : ISODate("2017-08-18T09:47:23Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 86,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
"lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.774Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.15:27017",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 52,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
"lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.883Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.15:27017",
"configVersion" : 3
}
],
"ok" : 1
}
4、測試資料寫入
主節點寫入資料:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":1})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
直接查詢副本節點報錯:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
Error: error: {
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
}
設定副本節點可讀:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
另一個副本節點:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
5、測試故障切換
先關閉主節點:
stoners:PRIMARY> use admin
switched to db admin
stoners:PRIMARY> db.shutdownServer();
server should be down...
其中一個副本節點變為了主節點:
stoners:SECONDARY>
stoners:PRIMARY>
stoners:PRIMARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T10:03:49.050Z"),
"myState" : 1,
"term" : NumberLong(2),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:03:47.314Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:01:03.792Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 929,
"optime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:03:44Z"),
"electionTime" : Timestamp(1503050473, 1),
"electionDate" : ISODate("2017-08-18T10:01:13Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 845,
"optime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:03:44Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:03:44Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:03:47.252Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:03:47.998Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.16:27017",
"configVersion" : 3
}
],
"ok" : 1
}
在主節點插入資料:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":2})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
在另一個副本節點檢視:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
現在啟動關閉的節點:
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
啟動後自動成為副本節點:
stoners:SECONDARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T10:08:25.401Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "10.163.97.17:27017",
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 49,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"syncingTo" : "10.163.97.17:27017",
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.753Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1503050473, 1),
"electionDate" : ISODate("2017-08-18T10:01:13Z"),
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.520Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.16:27017",
"configVersion" : 3
}
],
"ok" : 1
}
資料也同步過來了。
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
2、分別在3個節點安裝配置和啟動MongoDB
[root@D2-POMS15 ~]# rpm -qa | grep openssl
openssl098e-0.9.8e-17.el6_2.2.x86_64
openssl-1.0.1e-15.el6.x86_64
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb
[root@D2-POMS15 ~]# tar -xvzf mongodb-linux-x86_64-rhel62-3.4.7.tgz
[root@D2-POMS15 ~]# mv mongodb-linux-x86_64-rhel62-3.4.7/* /usr/local/mongodb/
[root@D2-POMS15 ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH=$PATH:/usr/local/mongodb/bin/
export PATH
[root@D2-POMS15 ~]# . .bash_profile
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/db
[root@D2-POMS15 ~]# mkdir /usr/local/mongodb/log
[root@D2-POMS15 ~]# touch /usr/local/mongodb/log/mongodb.log
[root@D2-POMS15 ~]# vim /usr/local/mongodb/mongodb.conf
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
noauth=true
port=27017
fork=true
replSet=stoners
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11580
child process started successfully, parent exiting
[root@D2-POMS15 ~]# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Welcome to the MongoDB shell.
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS16 ~]# mongod -f /usr/local/mongodb/mongodb.conf
[root@D2-POMS17 ~]# mongod -f /usr/local/mongodb/mongodb.conf
3、在任一節點進行副本集配置
初始化副本集配置。
語法:
{
_id : <setname>,
members : [
{_id : 0, host : <host0>},
{_id : 1, host : <host1>},
{_id : 2, host : <host2>},
]
}
配置其中一個節點為仲裁的語法:
{
_id : "my_replica_set",
members : [
{_id : 0, host : "rs1.example.net:27017"},
{_id : 1, host : "rs2.example.net:27017"},
{_id : 2, host : "rs3.example.net", arbiterOnly: true},
]
}
> rs.initiate({_id:"stoners",members:[{_id:0,host:"10.163.97.15:27017"}]})
{ "ok" : 1 }
stoners:SECONDARY> rs.conf()
{
"_id" : "stoners",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "10.163.97.15:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 60000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5996b49a33625594149428f2")
}
}
新增節點:
stoners:PRIMARY> rs.add("10.163.97.16")
{ "ok" : 1 }
stoners:PRIMARY> rs.add("10.163.97.17")
{ "ok" : 1 }
檢視狀態:
stoners:PRIMARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T09:49:18.307Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 144,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1503049643, 2),
"electionDate" : ISODate("2017-08-18T09:47:23Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 86,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
"lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.774Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.15:27017",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 52,
"optime" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1503049755, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-18T09:49:15Z"),
"optimeDurableDate" : ISODate("2017-08-18T09:49:15Z"),
"lastHeartbeat" : ISODate("2017-08-18T09:49:17.787Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T09:49:16.883Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.15:27017",
"configVersion" : 3
}
],
"ok" : 1
}
4、測試資料寫入
主節點寫入資料:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":1})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
直接查詢副本節點報錯:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
Error: error: {
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
}
設定副本節點可讀:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
另一個副本節點:
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
5、測試故障切換
先關閉主節點:
stoners:PRIMARY> use admin
switched to db admin
stoners:PRIMARY> db.shutdownServer();
server should be down...
其中一個副本節點變為了主節點:
stoners:SECONDARY>
stoners:PRIMARY>
stoners:PRIMARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T10:03:49.050Z"),
"myState" : 1,
"term" : NumberLong(2),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:03:47.314Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:01:03.792Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 929,
"optime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:03:44Z"),
"electionTime" : Timestamp(1503050473, 1),
"electionDate" : ISODate("2017-08-18T10:01:13Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 845,
"optime" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050624, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:03:44Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:03:44Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:03:47.252Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:03:47.998Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.16:27017",
"configVersion" : 3
}
],
"ok" : 1
}
在主節點插入資料:
stoners:PRIMARY> use hr
switched to db hr
stoners:PRIMARY> db.emp.insert({"num":2})
WriteResult({ "nInserted" : 1 })
stoners:PRIMARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
在另一個副本節點檢視:
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
現在啟動關閉的節點:
[root@D2-POMS15 ~]# mongod -f /usr/local/mongodb/mongodb.conf
啟動後自動成為副本節點:
stoners:SECONDARY> rs.status()
{
"set" : "stoners",
"date" : ISODate("2017-08-18T10:08:25.401Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "10.163.97.17:27017",
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.163.97.15:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 49,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"syncingTo" : "10.163.97.17:27017",
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.163.97.16:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.753Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1503050473, 1),
"electionDate" : ISODate("2017-08-18T10:01:13Z"),
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.163.97.17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1503050974, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-08-18T10:09:34Z"),
"optimeDurableDate" : ISODate("2017-08-18T10:09:34Z"),
"lastHeartbeat" : ISODate("2017-08-18T10:08:24.973Z"),
"lastHeartbeatRecv" : ISODate("2017-08-18T10:08:23.520Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.163.97.16:27017",
"configVersion" : 3
}
],
"ok" : 1
}
資料也同步過來了。
stoners:SECONDARY> db.getMongo().setSlaveOk();
stoners:SECONDARY> use hr
switched to db hr
stoners:SECONDARY> db.emp.find();
{ "_id" : ObjectId("5996b96ca4ca3f4911a83ae8"), "num" : 1 }
{ "_id" : ObjectId("5996bbcb5945237d11f3052d"), "num" : 2 }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2144112/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mongodb 4.0副本集搭建MongoDB
- MongoDB 6.0.3副本集搭建MongoDB
- MongoDB日常運維-04副本集搭建MongoDB運維
- MongoDB日常運維-05副本集故障切換MongoDB運維
- MongoDB 4.2副本集新增/刪除副本(一主一副一仲裁)MongoDB
- Mongodb3.0.5副本集搭建及spring和java連線副本集配置MongoDBSpringJava
- MongoDB 4.2副本集自動故障轉移(一主一副一仲裁)MongoDB
- 小丸子學MongoDB系列之——部署MongoDB副本集MongoDB
- MongoDB副本集MongoDB
- 修改mongodb3.0副本集使用者密碼遇到的坑MongoDB密碼
- 【MongoDB】高可用方案之副本集(Replica Set)MongoDB
- MongoDB 副本集搭建MongoDB
- MongoDB部署副本集MongoDB
- MongoDB 副本集管理MongoDB
- 再看MongoDB副本集MongoDB
- 搭建MongoDB副本集MongoDB
- 2.MongoDB 4.2副本集環境基於時間點的恢復MongoDB
- MongoDB - 副本集簡介MongoDB
- MongoDB副本集實踐MongoDB
- 如何配置 MongoDB 副本集MongoDB
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- MongoDB 副本集原理及管理MongoDB
- MongoDB 副本集切換方法MongoDB
- 006.MongoDB副本集MongoDB
- 分散式文件儲存資料庫之MongoDB副本集分散式資料庫MongoDB
- 小丸子學MongoDB系列之——副本集Auto-FailoverMongoDBAI
- MongoDB Replica Set 副本集實踐MongoDB
- mongodb簡單副本集實驗MongoDB
- 使用副本集搭建MongoDB叢集MongoDB
- MongoDB 刪除,新增副本集,並修改副本集IP等資訊MongoDB
- mongodb副本集新增刪除節點MongoDB
- 手把手教你搭建mongodb副本集MongoDB
- Mongodb分散式叢集副本集+分片MongoDB分散式
- MongoDB副本集管理方法介紹MongoDB
- MongoDB 副本集的原理、搭建、應用MongoDB
- MongoDB副本集心跳和同步機制MongoDB
- MongoDB--副本集基本資訊【面試必備】MongoDB面試
- MongoDB4.2 副本集掃盲說明MongoDB