標籤:讀取 proc 中斷 shell 修改 硬體 init code master
MongoDB 複本集配置
MongoDB複製是將資料同步在多個伺服器的過程。
複製提供了資料的冗餘備份,並在多個伺服器上儲存資料副本,提高了資料的可用性, 並可以保證資料的安全性。
複製還允許您從硬體故障和服務中斷中恢複資料。
棄置站台集時注意
- 版本: 各複本集伺服器的MongoDB版本一致或支援同樣的replSet功能
- 網路: 複本集內的每個成員都必須能夠串連到其他成員(包括自身),啟動時注意bind_ip --bind_ip_all.
配置複本集
mongodb.conf 1 # Start MongoDB as a daemon on port 27017 2 3 port = 27017 4 fork = true 5 replSet = test_replica_set 6 dbpath = /datatest/db 7 logpath = /datatest/db/test.log 8 logappend = true 9 mongodb2.conf 2 3 port = 27017 4 fork = true 5 replSet = test_replica_set 6 dbpath = /datatest/db 7 logpath = /datatest/db/test.log 8 logappend = true 9 mongodb3.conf 2 3 port = 27017 4 fork = true 5 replSet = test_replica_set 6 dbpath = /datatest/db 7 logpath = /datatest/db/test.log 8 logappend = true 9
mac-abeen:bin abeen$ sudo ./mongod -f mongodb.conf about to fork child process, waiting until server is ready for connections.forked process: 37181child process started successfully, parent exitingmac-abeen:bin abeen$ sudo ./mongod -f mongodb2.conf about to fork child process, waiting until server is ready for connections.forked process: 37185child process started successfully, parent exitingmac-abeen:bin abeen$ sudo ./mongod -f mongodb3.conf about to fork child process, waiting until server is ready for connections.forked process: 37189child process started successfully, parent exiting
mac-abeen:bin abeen$ ./mongo --nodbMongoDB shell version: 3.2.8> config = {}{ }> config = {"_id": "test_replica_set", "members": [ {"_id": 0, "host": "192.168.0.10:27017"}, {"_id": 1, "host": "192.168.0.20:27017"}, {"_id": 2, "host": "192.168.0.30:27017"}]}{ "_id" : "test_replica_set", "members" : [ { "_id" : 0, "host" : "192.168.0.10:27017" }, { "_id" : 1, "host" : "192.168.0.20:27017" }, { "_id" : 2, "host" : "192.168.0.30:27017" } ]}> db = (new Mongo("192.168.0.10:27017")).getDB("test")test> rs.initiate(config){ "ok" : 1 }test_replica_set:OTHER> test_replica_set:PRIMARY> rs.config(){ "_id" : "test_replica_set", "version" : 1, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "192.168.0.10:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "192.168.0.20:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "192.168.0.30: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("5850f445c8cacd70496883b0") }}
test_replica_set:PRIMARY> test_replica_set:PRIMARY> for (i=0; i < 100; i++){db.coll.insert({"count": i})}WriteResult({ "nInserted" : 1 })test_replica_set:PRIMARY> db.coll.count()100test_replica_set:PRIMARY> db.coll.find().limit(5){ "_id" : ObjectId("5850f5f35f1a7d82c0b45b51"), "count" : 0 }{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b52"), "count" : 1 }{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b53"), "count" : 2 }{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b54"), "count" : 3 }{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b55"), "count" : 4 }
mac-abeen:bin abeen$ ./mongo --port 27017 --host 192.168.0.20MongoDB shell version: 3.2.8connecting to: 192.168.0.20:27017/testtest_replica_set:SECONDARY> db.coll.find().limit(5)Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }設定串連可讀取資料test_replica_set:SECONDARY> db.setSlaveOk()副本中已有資料 test_replica_set:SECONDARY> db.coll.find().limit(5){ "_id" : ObjectId("5850f5f35f1a7d82c0b45b53"), "count" : 2 }{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b55"), "count" : 4 }{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b52"), "count" : 1 }{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b54"), "count" : 3 }{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b51"), "count" : 0 }
修改複本集
rs.add("server-4:27017") //添加rs.addArb("server-4:27017") //添加選舉仲裁者rs.add({"_id": 4, "host": "server-4:27017", "arbiterOnly": true) //添加選舉仲裁者rs.add({"_id": 5, "host": "server-5:27017", "priority": 0, "hidden": true) //添加隱藏成員rs.remove("server-4:27017") //移除
修改複本集,通過rs.reconfig
rs.reconfig修改複本集成員時限制
- 不能修改成員的_id 欄位
- 不能將接收rs.reconfig命令的成員(通常是主節點)的優先順序設為0
- 不能仲裁者成員變為非仲裁者成員,反之亦然.
- 不能將buildIndexes: false的成員修改為buildIndexes: true
可以修改其他,比如host
var config = rs.config()config.members[1].host = "newsserver:27017" //修改hostrs.reconfig(config)
[Database] MongoDB 複本集配置