1. 程式人生 > 其它 >mongo副本集

mongo副本集

replication sets

1.啟動3個例項,他們3個屬於同一個複製集,宣告屬於哪一個複製集
./mongod --dbpath=/mongodb/data1 --logpath=/mongodb/log/d1.log --fork --port 27017 --replSet rsjty
./mongod --dbpath=/mongodb/data2 --logpath=/mongodb/log/d2.log --fork --port 27018 --replSet rsjty
./mongod --dbpath=/mongodb/data3 --logpath=/mongodb/log/d3.log --fork --port 27019 --replSet rsjty

2.配置
var rsconfig={

_id:"rsjty",
members:[
{_id:0,
host:"127.0.0.1:27017"},
{_id:1,
host:"127.0.0.1:27018"},
{_id:2,
host:"127.0.0.1:27019"}

]
}

3.根據配置做replication set的初始化
rs.initiate(rsconfig)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1646197076, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1646197076, 1)

通過rs.stats()可以檢視狀態
配置好之後會27017為primary ,18、19為secondary狀態

4.刪除節點
rs.remove("127.0.0.1:27019")
rs.stats() 可以檢視節點數量

5.新增節點;
rs.add("127.0.0.1:27019")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1646197632, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1646197632, 1)
}

5.連線secondary檢視資料,slave預設不允許讀寫,可以執行rs.slaveok()或者rs.secondaryok()
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead

6.primary 角色shutdown ,執行db.shutdownServer()
rsjty:PRIMARY> db.shutdownServer()
server should be down...

27018 跟27019會自動選舉上來一個作為住,