1、複製集概述;
2、複製集原理;
3、案例:搭建Mongodb實現應用複製集;linux
組成:
Mongodb複製集(副本集replica set)由一組Mongod實例(進程)組成,包含一個Primary節點和多個Secondary節點,Mongodb Driver(客戶端)的全部數據都寫入Primary,Secondary經過oplog來同步Primary的數據,保證主節點和從節點數據的一致性,複製集在完成主從複製的基礎上,經過心跳機制,一旦primary節點出現宕機,則觸發選舉一個新的主節點,剩下的secondary節點指向新的primary,時間應該在10-30s內完成感知primary節點故障,實現高可用數據庫集羣;
特色:
主是惟一的,但不是固定的;
由大多數據原則保證數據的一致性;
從庫沒法寫入(默認狀況下,不使用驅動鏈接時,也是不能查詢的);
相對於傳統的主從結構,複製集能夠自動容災;mongodb
角色(按是否存儲數據劃分):
Primary:主節點,由選舉產生,負責客戶端的寫操做,產生oplog日誌文件;
Secondary:從節點,負責客戶端的讀操做,提供數據的備份和故障的切換;
Arbiter:仲裁節點,只參與選舉的投票,不會成爲primary,也不向Primary同步數據,若部署了一個2個節點的複製集,1個Primary,1個Secondary,任意節點宕機,複製集將不能提供服務了(沒法選出Primary),這時能夠給複製集添加一個Arbiter節點,即便有節點宕機,仍能選出Primary;
角色(按類型區分):
Standard(標準):這種是常規節點,它存儲一份完整的數據副本,參與投票選舉,有可能成爲主節點;
Passive(被動):存儲完整的數據副本,參與投票,不能成爲活躍節點。
Arbiter(投票):仲裁節點只參與投票,不接收復制的數據,也不能成爲活躍節點。
注:每一個參與節點(非仲裁者)有個優先權(0-1000),優先權(priority)爲0則是被動的,不能成爲活躍節點,優先權不爲0的,按照由大到小選出活躍節點,優先值同樣的則看誰的數據比較新;
注:Mongodb 3.0裏,複製集成員最多50個,參與Primary選舉投票的成員最多7個;shell
選舉:
每一個節點經過優先級定義出節點的類型(標準、被動、投票);
標準節點經過對比自身數據進行選舉出peimary節點或者secondary節點;
影響選舉的因素:
1.心跳檢測:複製集內成員每隔兩秒向其餘成員發送心跳檢測信息,若10秒內無響應,則標記其爲不可用;
2.鏈接:在多個節點中,最少保證兩個節點爲活躍狀態,若是集羣中共三個節點,掛掉兩個節點,那麼剩餘的節點不管狀態是primary仍是處於選舉過程當中,都會直接被降權爲secondary;
觸發選舉的狀況:
1.初始化狀態 2.從節點們沒法與主節點進行通訊 3.主節點辭職
主節點辭職的狀況:
1.在接收到replSetStepDown命令後;
2.在現有的環境中,其餘secondary節點的數據落後於自己10s內,且擁有更高優先級;
3.當主節點沒法與羣集中多數節點通訊;
注:當主節點辭職後,主節點將關閉自身全部的鏈接,避免出現客戶端在從節點進行寫入操做;
數據庫
案例拓撲:
異常處理:
當Primary宕機時,若是有數據未同步到Secondary,當Primary從新加入時,若是新的Primary上已經發生了寫操做,則舊Primary須要回滾部分操做,以保證數據集與新的Primary一致。舊Primary將回滾的數據寫到單獨的rollback目錄下,數據庫管理員可根據須要使用mongorestore進行恢復。bash
案例環境:
系統 主機名 IP地址 所需軟件
Centos 7.4 mongodb.benet.com 192.168.100.101 mongodb-linux-x86_64-rhel70-3.6.3.tgzapp
實驗步驟:tcp
安裝mongodb; 下載mongodb軟件包; [root@mongodb ~]# tar zxvf mongodb-linux-x86_64-rhel70-3.6.3.tgz [root@mongodb ~]# mv mongodb-linux-x86_64-rhel70-3.6.3 /usr/local/mongodb [root@mongodb ~]# echo "export PATH=/usr/local/mongodb/bin:\$PATH" >>/etc/profile [root@mongodb ~]# source /etc/profile [root@mongodb ~]# ulimit -n 25000 [root@mongodb ~]# ulimit -u 25000 [root@mongodb ~]# echo 0 >/proc/sys/vm/zone_reclaim_mode [root@mongodb ~]# sysctl -w vm.zone_reclaim_mode=0 [root@mongodb ~]# echo never >/sys/kernel/mm/transparent_hugepage/enabled [root@mongodb ~]# echo never >/sys/kernel/mm/transparent_hugepage/defrag 建立並啓動四個實例; [root@mongodb ~]# cd /usr/local/mongodb/bin/ [root@mongodb bin]# mkdir {../mongodb1,../mongodb2,../mongodb3,../mongodb4} [root@mongodb bin]# mkdir ../logs [root@mongodb bin]# touch ../logs/mongodb{1..4}.log [root@mongodb bin]# chmod 777 ../logs/mongodb* [root@mongodb bin]# cat <<END >>/usr/local/mongodb/bin/mongodb1.conf bind_ip=192.168.100.101 port=27017 dbpath=/usr/local/mongodb/mongodb1/ logpath=/usr/local/mongodb/logs/mongodb1.log logappend=true fork=true maxConns=5000 replSet=haha #replication name END [root@mongodb bin]# cat <<END >>/usr/local/mongodb/bin/mongodb2.conf bind_ip=192.168.100.101 port=27018 dbpath=/usr/local/mongodb/mongodb2/ logpath=/usr/local/mongodb/logs/mongodb2.log logappend=true fork=true maxConns=5000 replSet=haha END [root@mongodb bin]# cat <<END >>/usr/local/mongodb/bin/mongodb3.conf bind_ip=192.168.100.101 port=27019 dbpath=/usr/local/mongodb/mongodb3/ logpath=/usr/local/mongodb/logs/mongodb3.log logappend=true fork=true maxConns=5000 replSet=haha END [root@mongodb bin]# cat <<END >>/usr/local/mongodb/bin/mongodb4.conf bind_ip=192.168.100.101 port=27020 dbpath=/usr/local/mongodb/mongodb4/ logpath=/usr/local/mongodb/logs/mongodb4.log logappend=true fork=true maxConns=5000 replSet=haha END [root@mongodb bin]# cd [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb1.conf [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb2.conf [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb3.conf [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb4.conf [root@mongodb ~]# netstat -utpln |grep mongod tcp 0 0 192.168.100.101:27019 0.0.0.0:* LISTEN 2271/mongod tcp 0 0 192.168.100.101:27020 0.0.0.0:* LISTEN 15260/mongod tcp 0 0 192.168.100.101:27017 0.0.0.0:* LISTEN 2440/mongod tcp 0 0 192.168.100.101:27018 0.0.0.0:* LISTEN 1412/mongod [root@mongodb ~]# echo -e "/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb1.conf \n/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb2.conf\n/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb3.conf\n/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb4.conf">>/etc/rc.local [root@mongodb ~]# chmod +x /etc/rc.local [root@mongodb ~]# cat <<END >>/etc/init.d/mongodb #!/bin/bash INSTANCE=\$1 ACTION=\$2 case "\$ACTION" in 'start') /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/"\$INSTANCE".conf;; 'stop') /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/"\$INSTANCE".conf --shutdown;; 'restart') /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/"\$INSTANCE".conf --shutdown /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/"\$INSTANCE".conf;; esac END [root@mongodb ~]# chmod +x /etc/init.d/mongodb [root@mongodb ~]# /etc/init.d/mongodb mongodb1 stop killing process with pid: 1301 [root@mongodb ~]# /etc/init.d/mongodb mongodb1 start about to fork child process, waiting until server is ready for connections. forked process: 1457 child process started successfully, parent exiting [root@mongodb ~]# mongo --port 27017 --host 192.168.100.101 配置實例; [root@mongodb ~]# mongo --port 27017 --host 192.168.100.101 > show dbs 2018-04-19T04:34:35.871+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk" } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1 shellHelper.show@src/mongo/shell/utils.js:816:19 shellHelper@src/mongo/shell/utils.js:706:15 @(shellhelp2):1:1 > rs.status() { "info" : "run rs.initiate(...) if not yet done for the set", "ok" : 0, "errmsg" : "no replset config has been received", "code" : 94, "codeName" : "NotYetInitialized" } > rs.isMaster() ##查看複製集節點 { "ismaster" : false, "secondary" : false, "info" : "Does not have a valid replica set config", "isreplicaset" : true, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : ISODate("2018-04-18T20:36:31.698Z"), "minWireVersion" : 0, "maxWireVersion" : 6, "readOnly" : false, "ok" : 1 } > exit [root@mongodb ~]# mongo --port 27018 --host 192.168.100.101 > show dbs 2018-04-19T04:34:56.884+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk" } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1 shellHelper.show@src/mongo/shell/utils.js:816:19 shellHelper@src/mongo/shell/utils.js:706:15 @(shellhelp2):1:1 > exit 建立複製集並添加節點; [root@mongodb ~]# mongo --port 27017 --host 192.168.100.101 >cfg={"_id":"haha","members":[{"_id":0,"host":"192.168.100.101:27017"},{"_id":1,"host":"192.168.100.101:27018"},{"_id":2,"host":"192.168.100.101:27019"}]} ##添加節點 { "_id" : "haha", "members" : [ { "_id" : 0, "host" : "192.168.100.101:27017" }, { "_id" : 1, "host" : "192.168.100.101:27018" }, { "_id" : 2, "host" : "192.168.100.101:27019" } ] } > rs.initiate(cfg) ##初始化節點 { "ok" : 1, "operationTime" : Timestamp(1524083843, 1), "$clusterTime" : { "clusterTime" : Timestamp(1524083843, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } haha:OTHER> haha:PRIMARY> haha:PRIMARY> rs.status() { "set" : "haha", "date" : ISODate("2018-04-18T20:37:54.095Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1524083855, 5), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1524083855, 5), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1524083855, 5), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1524083855, 5), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "192.168.100.101:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 329, "optime" : { "ts" : Timestamp(1524083855, 5), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-04-18T20:37:35Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1524083854, 1), "electionDate" : ISODate("2018-04-18T20:37:34Z"), "configVersion" : 1, "self" : true }, { "_id" : 1, "name" : "192.168.100.101:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 30, "optime" : { "ts" : Timestamp(1524083855, 5), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1524083855, 5), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-04-18T20:37:35Z"), "optimeDurableDate" : ISODate("2018-04-18T20:37:35Z"), "lastHeartbeat" : ISODate("2018-04-18T20:37:54.043Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T20:37:52.499Z"), "pingMs" : NumberLong(0), "syncingTo" : "192.168.100.101:27017", "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.100.101:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 30, "optime" : { "ts" : Timestamp(1524083855, 5), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1524083855, 5), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-04-18T20:37:35Z"), "optimeDurableDate" : ISODate("2018-04-18T20:37:35Z"), "lastHeartbeat" : ISODate("2018-04-18T20:37:54.043Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T20:37:52.500Z"), "pingMs" : NumberLong(0), "syncingTo" : "192.168.100.101:27017", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1524083855, 5), "$clusterTime" : { "clusterTime" : Timestamp(1524083855, 5), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } haha:PRIMARY> rs.addArb("192.168.100.101:27020") ##添加仲裁節點 { "ok" : 1, "operationTime" : Timestamp(1524083905, 1), "$clusterTime" : { "clusterTime" : Timestamp(1524083905, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } haha:PRIMARY> rs.status() { "set" : "haha", "date" : ISODate("2018-04-18T20:38:41.468Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1524083915, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1524083915, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1524083915, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1524083915, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "192.168.100.101:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 376, "optime" : { "ts" : Timestamp(1524083915, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-04-18T20:38:35Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1524083854, 1), "electionDate" : ISODate("2018-04-18T20:37:34Z"), "configVersion" : 2, "self" : true }, { "_id" : 1, "name" : "192.168.100.101:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 78, "optime" : { "ts" : Timestamp(1524083905, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1524083905, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-04-18T20:38:25Z"), "optimeDurableDate" : ISODate("2018-04-18T20:38:25Z"), "lastHeartbeat" : ISODate("2018-04-18T20:38:41.134Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T20:38:40.134Z"), "pingMs" : NumberLong(0), "configVersion" : 2 }, { "_id" : 2, "name" : "192.168.100.101:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 78, "optime" : { "ts" : Timestamp(1524083905, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1524083905, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-04-18T20:38:25Z"), "optimeDurableDate" : ISODate("2018-04-18T20:38:25Z"), "lastHeartbeat" : ISODate("2018-04-18T20:38:41.134Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T20:38:40.136Z"), "pingMs" : NumberLong(0), "configVersion" : 2 }, { "_id" : 3, "name" : "192.168.100.101:27020", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 16, "lastHeartbeat" : ISODate("2018-04-18T20:38:41.137Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T20:38:40.140Z"), "pingMs" : NumberLong(0), "configVersion" : 2 } ], "ok" : 1, "operationTime" : Timestamp(1524083915, 1), "$clusterTime" : { "clusterTime" : Timestamp(1524083915, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } 注:rs.add()和rs.remove()命令分別用於添加和刪除標準節點 haha:PRIMARY> show dbs admin 0.000GB config 0.000GB local 0.000GB haha:PRIMARY> use cloud switched to db cloud haha:PRIMARY> db.users.insert({"id":"1","name":"xiaoming"}) WriteResult({ "nInserted" : 1 }) haha:PRIMARY> db.users.find() { "_id" : ObjectId("5ad7b245f6308759d4605b5c"), "id" : "1", "name" : "xiaoming" } haha:PRIMARY> show dbs admin 0.000GB cloud 0.000GB config 0.000GB local 0.000GB haha:PRIMARY> exit [root@mongodb ~]# mongo --port 27018 --host 192.168.100.101 haha:SECONDARY> rs.status() { "set" : "haha", "date" : ISODate("2018-04-18T20:50:50.975Z"), "myState" : 2, "term" : NumberLong(1), "syncingTo" : "192.168.100.101:27017", "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1524084645, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1524084645, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1524084645, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1524084645, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "192.168.100.101:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 805, "optime" : { "ts" : Timestamp(1524084645, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1524084645, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-04-18T20:50:45Z"), "optimeDurableDate" : ISODate("2018-04-18T20:50:45Z"), "lastHeartbeat" : ISODate("2018-04-18T20:50:49.966Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T20:50:49.997Z"), "pingMs" : NumberLong(0), "electionTime" : Timestamp(1524083854, 1), "electionDate" : ISODate("2018-04-18T20:37:34Z"), "configVersion" : 2 }, { "_id" : 1, "name" : "192.168.100.101:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1497, "optime" : { "ts" : Timestamp(1524084645, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-04-18T20:50:45Z"), "syncingTo" : "192.168.100.101:27017", "configVersion" : 2, "self" : true }, { "_id" : 2, "name" : "192.168.100.101:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 805, "optime" : { "ts" : Timestamp(1524084645, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1524084645, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-04-18T20:50:45Z"), "optimeDurableDate" : ISODate("2018-04-18T20:50:45Z"), "lastHeartbeat" : ISODate("2018-04-18T20:50:49.811Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T20:50:49.965Z"), "pingMs" : NumberLong(0), "syncingTo" : "192.168.100.101:27017", "configVersion" : 2 }, { "_id" : 3, "name" : "192.168.100.101:27020", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 745, "lastHeartbeat" : ISODate("2018-04-18T20:50:49.966Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T20:50:50.378Z"), "pingMs" : NumberLong(0), "configVersion" : 2 } ], "ok" : 1, "operationTime" : Timestamp(1524084645, 1), "$clusterTime" : { "clusterTime" : Timestamp(1524084645, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } haha:SECONDARY> show dbs ##secondary節點默認沒法讀取,能夠經過如下方式或者驅動方式實現 2018-04-19T04:52:42.813+0800 E QUERY [thread1] Error: listDatabases failed:{ "operationTime" : Timestamp(1524084755, 1), "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk", "$clusterTime" : { "clusterTime" : Timestamp(1524084755, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1 shellHelper.show@src/mongo/shell/utils.js:816:19 shellHelper@src/mongo/shell/utils.js:706:15 @(shellhelp2):1:1 haha:SECONDARY> db.getMongo().setSlaveOk(); haha:SECONDARY> show dbs admin 0.000GB cloud 0.000GB config 0.000GB local 0.000GB haha:SECONDARY> use cloud switched to db cloud haha:SECONDARY> db.users.insert({"id":"2","name":"xiaohong"}) ##secondary節點沒法寫入 WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } }) haha:SECONDARY> db.users.find() { "_id" : ObjectId("5ad7b245f6308759d4605b5c"), "id" : "1", "name" : "xiaoming" } haha:SECONDARY> exit 模擬Primary節點出現故障,查看角色切換狀況; [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb1.conf --shutdown killing process with pid: 1457 [root@mongodb ~]# mongo --port 27018 --host 192.168.100.101 haha:SECONDARY> rs.status() { "set" : "haha", "date" : ISODate("2018-04-18T21:16:22.623Z"), "myState" : 2, "term" : NumberLong(2), "syncingTo" : "192.168.100.101:27019", "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1524086155, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1524086155, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1524086174, 1), "t" : NumberLong(2) }, "durableOpTime" : { "ts" : Timestamp(1524086174, 1), "t" : NumberLong(2) } }, "members" : [ { "_id" : 0, "name" : "192.168.100.101: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("2018-04-18T21:16:19.030Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T21:16:03.344Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "Connection refused", "configVersion" : -1 }, { "_id" : 1, "name" : "192.168.100.101:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 3029, "optime" : { "ts" : Timestamp(1524086174, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2018-04-18T21:16:14Z"), "syncingTo" : "192.168.100.101:27019", "infoMessage" : "syncing from: 192.168.100.101:27019", "configVersion" : 2, "self" : true }, { "_id" : 2, "name" : "192.168.100.101:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2337, "optime" : { "ts" : Timestamp(1524086174, 1), "t" : NumberLong(2) }, "optimeDurable" : { "ts" : Timestamp(1524086174, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2018-04-18T21:16:14Z"), "optimeDurableDate" : ISODate("2018-04-18T21:16:14Z"), "lastHeartbeat" : ISODate("2018-04-18T21:16:19.030Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T21:16:21.575Z"), "pingMs" : NumberLong(0), "electionTime" : Timestamp(1524086173, 1), "electionDate" : ISODate("2018-04-18T21:16:13Z"), "configVersion" : 2 }, { "_id" : 3, "name" : "192.168.100.101:27020", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 2277, "lastHeartbeat" : ISODate("2018-04-18T21:16:19.030Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T21:16:20.894Z"), "pingMs" : NumberLong(0), "configVersion" : 2 } ], "ok" : 1, "operationTime" : Timestamp(1524086174, 1), "$clusterTime" : { "clusterTime" : Timestamp(1524086174, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } haha:SECONDARY> exit [root@mongodb ~]# mongo --port 27019 --host 192.168.100.101 haha:PRIMARY> exit [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb1.conf 手動切換Primary角色; [root@mongodb ~]# mongo --port 27019 --host 192.168.100.101 haha:PRIMARY> rs.stepDown(60,30) haha:SECONDARY> exit [root@mongodb ~]# mongo --port 27018 --host 192.168.100.101 haha:PRIMARY> exit 指定節點的優先級,驗證角色重選狀況; [root@mongodb ~]# mongo --port 27018 --host 192.168.100.101 haha:PRIMARY> cfg={"_id":"haha","members":[{"_id":0,"host":"192.168.100.101:27017","priority":100},{"_id":1,"host":"192.168.100.101:27018","priority":100},{"_id":2,"host":"192.168.100.101:27019","priority":10}]} { "_id" : "haha", "members" : [ { "_id" : 0, "host" : "192.168.100.101:27017", "priority" : 100 }, { "_id" : 1, "host" : "192.168.100.101:27018", "priority" : 100 }, { "_id" : 2, "host" : "192.168.100.101:27019", "priority" : 10 } ] } haha:PRIMARY> rs.reconfig(cfg) { "ok" : 1, "operationTime" : Timestamp(1524086716, 1), "$clusterTime" : { "clusterTime" : Timestamp(1524086716, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } haha:PRIMARY> rs.isMaster() { "hosts" : [ "192.168.100.101:27017", "192.168.100.101:27018", "192.168.100.101:27019" ], "setName" : "haha", "setVersion" : 3, "ismaster" : true, "secondary" : false, "primary" : "192.168.100.101:27018", "me" : "192.168.100.101:27018", "electionId" : ObjectId("7fffffff0000000000000003"), "lastWrite" : { "opTime" : { "ts" : Timestamp(1524086733, 1), "t" : NumberLong(3) }, "lastWriteDate" : ISODate("2018-04-18T21:25:33Z"), "majorityOpTime" : { "ts" : Timestamp(1524086733, 1), "t" : NumberLong(3) }, "majorityWriteDate" : ISODate("2018-04-18T21:25:33Z") }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : ISODate("2018-04-18T21:25:42.122Z"), "logicalSessionTimeoutMinutes" : 30, "minWireVersion" : 0, "maxWireVersion" : 6, "readOnly" : false, "ok" : 1, "operationTime" : Timestamp(1524086733, 1), "$clusterTime" : { "clusterTime" : Timestamp(1524086733, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } haha:PRIMARY> exit [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb2.conf --shutdown [root@mongodb ~]# mongo --port 27017 --host 192.168.100.101 haha:PRIMARY> exit 將標準節點通通停掉,被動節點也不會成爲主節點; [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb1.conf --shutdown [root@mongodb ~]# mongo --port 27019 --host 192.168.100.101 haha:SECONDARY> rs.status() { "set" : "haha", "date" : ISODate("2018-04-18T21:29:42.973Z"), "myState" : 2, "term" : NumberLong(5), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1524086961, 1), "t" : NumberLong(5) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1524086961, 1), "t" : NumberLong(5) }, "appliedOpTime" : { "ts" : Timestamp(1524086961, 1), "t" : NumberLong(5) }, "durableOpTime" : { "ts" : Timestamp(1524086961, 1), "t" : NumberLong(5) } }, "members" : [ { "_id" : 0, "name" : "192.168.100.101: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("2018-04-18T21:29:38.566Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T21:29:26.484Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "Connection refused", "configVersion" : -1 }, { "_id" : 1, "name" : "192.168.100.101:27018", "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("2018-04-18T21:29:38.566Z"), "lastHeartbeatRecv" : ISODate("2018-04-18T21:26:42.249Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "Connection refused", "configVersion" : -1 }, { "_id" : 2, "name" : "192.168.100.101:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 3826, "optime" : { "ts" : Timestamp(1524086961, 1), "t" : NumberLong(5) }, "optimeDate" : ISODate("2018-04-18T21:29:21Z"), "infoMessage" : "could not find member to sync from", "configVersion" : 3, "self" : true } ], "ok" : 1, "operationTime" : Timestamp(1524086961, 1), "$clusterTime" : { "clusterTime" : Timestamp(1524086961, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } haha:SECONDARY> exit [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb1.conf [root@mongodb ~]# mongod -f /usr/local/mongodb/bin/mongodb2.conf [root@mongodb ~]# netstat -utpln |grep mongod tcp 0 0 192.168.100.101:27019 0.0.0.0:* LISTEN 1357/mongod tcp 0 0 192.168.100.101:27020 0.0.0.0:* LISTEN 1385/mongod tcp 0 0 192.168.100.101:27017 0.0.0.0:* LISTEN 14363/mongod tcp 0 0 192.168.100.101:27018 0.0.0.0:* LISTEN 14442/mongod 查詢複製集狀態以及查看oplog日誌文件的大小; [root@mongodb ~]# mongo --port 27018 --host 192.168.100.101 haha:PRIMARY> use local haha:PRIMARY> rs.printSlaveReplicationInfo() ##查看節點信息 source: 192.168.100.101:27017 syncedTo: Thu Apr 19 2018 05:34:37 GMT+0800 (CST) 0 secs (0 hrs) behind the primary source: 192.168.100.101:27019 syncedTo: Thu Apr 19 2018 05:34:37 GMT+0800 (CST) 0 secs (0 hrs) behind the primary haha:PRIMARY> rs.printReplicationInfo() configured oplog size: 990MB log length start to end: 3464secs (0.96hrs) oplog first event time: Thu Apr 19 2018 04:37:23 GMT+0800 (CST) oplog last event time: Thu Apr 19 2018 05:35:07 GMT+0800 (CST) now: Thu Apr 19 2018 05:35:16 GMT+0800 (CST) haha:PRIMARY> db.oplog.rs.stats() { "ns" : "local.oplog.rs", "size" : 50356, "count" : 450, "avgObjSize" : 111, "storageSize" : 61440, "capped" : true, "max" : -1, "maxSize" : 1038090240, ##單位是KB ... 部署用戶認證登陸(密鑰對)的複製集; [root@mongodb ~]# mongo --port 27018 --host 192.168.100.101 haha:PRIMARY> use admin switched to db admin haha:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]}) Successfully added user: { "user" : "root", "roles" : [ "root" ] } haha:PRIMARY> exit [root@mongodb ~]# echo -e "clusterAuthMode=keyFile\nkeyFile=/usr/local/mongodb/bin/cloudkey1" >>/usr/local/mongodb/bin/mongodb1.conf [root@mongodb ~]# echo -e "clusterAuthMode=keyFile\nkeyFile=/usr/local/mongodb/bin/cloudkey2" >>/usr/local/mongodb/bin/mongodb2.conf [root@mongodb ~]# echo -e "clusterAuthMode=keyFile\nkeyFile=/usr/local/mongodb/bin/cloudkey3" >>/usr/local/mongodb/bin/mongodb3.conf [root@mongodb ~]# echo "haha key" >/usr/local/mongodb/bin/cloudkey1 [root@mongodb ~]# echo "haha key" >/usr/local/mongodb/bin/cloudkey2 [root@mongodb ~]# echo "haha key" >/usr/local/mongodb/bin/cloudkey3 [root@mongodb ~]# chmod 600 /usr/local/mongodb/bin/cloudkey* [root@mongodb ~]# /etc/init.d/mongodb mongodb1 restart [root@mongodb ~]# /etc/init.d/mongodb mongodb2 restart [root@mongodb ~]# /etc/init.d/mongodb mongodb3 restart [root@mongodb ~]# mongo --port 27018 --host 192.168.100.101 haha:PRIMARY> show dbs 2018-04-19T12:02:57.720+0800 E QUERY [thread1] Error: listDatabases failed:{ "operationTime" : Timestamp(1524110569, 1), "ok" : 0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $clusterTime: { clusterTime: Timestamp(1524110569, 1), signature: { hash: BinData(0, 85A5B587BFFE4B2FB534789EA87032CB263D8121), keyId: 6545890313586606081 } }, $db: \"admin\" }", ... haha:PRIMARY> use admin switched to db admin haha:PRIMARY>db.auth("root","123") 1 haha:PRIMARY> show dbs admin 0.000GB cloud 0.000GB config 0.000GB local 0.000GB haha:PRIMARY> exit