您好,登錄后才能下訂單哦!
MongoDB復制是將數據同步在多個服務器的過程。
復制提供了數據的冗余備份,并在多個服務器上存儲數據副本,提高了數據的可用性, 并可以保證數據的安全性。
mongodb的復制至少需要兩個節點。其中一個是主節點,負責處理客戶端請求,其余的都是從節點,負責復制主節點上的數據。
mongodb各個節點常見的搭配方式為:一主一從、一主多從。
主節點記錄在其上的所有操作oplog,從節點定期輪詢主節點獲取這些操作,然后對自己的數據副本執行這些操作,從而保證從節點的數據與主節點一致。
以上結構圖中,客戶端從主節點讀取數據,在客戶端寫入數據到主節點時, 主節點與從節點進行數據交互保障數據的一致性。
tar -zxvf mongodb-linux-x86_64-3.2.1.tgz -C /usr/local/
cd /usr/local/
mv mongodb-linux-x86_64-3.2.1/ mongodb #重命名
mkdir -p /data/mongodb/mongodb{1,2,3,4} #創建數據目錄
mkdir -p /data/logs
touch /data/logs/mongodb{1,2,3,4}.log #創建日志文件
cd /data/logs/
chmod 777 *.log #賦予權限
cd /usr/local/mongodb/bin
vim mongodb1.conf
port=27017
dbpath=/data/mongodb/mongodb1
logpath=/data/logs/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
replSet=test #復制集名稱
------------------------------------------------------------------------------------
#下面是yum安裝時修改、添加的
replication: #去注釋
replSetName: test #添加復制集名稱
ln -s /usr/local/mongodb/bin/mongod /usr/bin/ #把mongodb常用的命令添加到系統命令中
ln -s /usr/local/mongodb/bin/mongo /usr/bin/
[root@localhost bin]# mongo
> cfg={"_id":"test","members":[{"_id":0,"host":"192.168.217.129:27017"},{"_id":1,"host":"192.168.217.129:27018"},{"_id":2,"host":"192.168.217.129:27019"}]}
#配置復制集 ,注意復制集的名稱要一致
{
"_id" : "test",
"members" : [
{
"_id" : 0,
"host" : "192.168.217.129:27017"
},
{
"_id" : 1,
"host" : "192.168.217.129:27018"
},
{
"_id" : 2,
"host" : "192.168.217.129:27019"
}
]
}
> rs.initiate(cfg) #初始化配置時保證從節點沒有數據
{ "ok" : 1 }
test:PRIMARY> rs.status() #查看復制集的完整狀態
{
"set" : "test",
.......
{
"_id" : 0,
"name" : "192.168.217.129:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", #27017 端口作為主節點
"uptime" : 1234,
"optime" : {
"ts" : Timestamp(1531961046, 1),
"t" : NumberLong(1)
},
......
},
{
"_id" : 1,
"name" : "192.168.217.129:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #從節點
"uptime" : 49,
"optime" : {
"ts" : Timestamp(1531961046, 1),
"t" : NumberLong(1)
},
......
},
{
"_id" : 2,
"name" : "192.168.217.129:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #從節點
"uptime" : 49,
"optime" : {
"ts" : Timestamp(1531961046, 1),
"t" : NumberLong(1)
},
.......
}
],
"ok" : 1
}
test:PRIMARY> rs.add("192.168.217.129:27020") #添加節點
{ "ok" : 1 }
test:PRIMARY> rs.status()
........
"_id" : 3,
"name" : "192.168.217.129:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
........
test:PRIMARY> rs.remove("192.168.217.129:27020") #刪除節點
{ "ok" : 1 }
test:PRIMARY> rs.status()
.........
[root@localhost bin]# mongod -f mongodb1.conf --shutdown #關閉主節點端口
killing process with pid: 3552
[root@localhost bin]# mongo --port 27018
test:SECONDARY> rs.status()
{
"set" : "test",
.......
{
"_id" : 0,
"name" : "192.168.217.129:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
.......
{
"_id" : 1,
"name" : "192.168.217.129:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1811,
.......
{
"_id" : 2,
"name" : "192.168.217.129:27019", #自動切換主節點
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 712,
......
}
],
"ok" : 1
}
[root@localhost bin]# mongo --port 27019
test:PRIMARY> rs.freeze(30) #暫停30s不參與選舉
{ "ok" : 1 }
test:PRIMARY> rs.stepDown(60,30)
#交出主節點位置,維持從節點狀態不少于60秒,等待30秒使主節點和從節點日志同步
test:SECONDARY> rs.status()
......
test:SECONDARY> show dbs #
2018-07-19T09:04:34.898+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:53:1
shellHelper.show@src/mongo/shell/utils.js:700:19
shellHelper@src/mongo/shell/utils.js:594:15
@(shellhelp2):1:1
test:SECONDARY> rs.slaveOk() #允許默認從節點讀取數據
test:SECONDARY> show dbs
local 1.078GB
test:PRIMARY> use local
switched to db local
test:PRIMARY> rs.printReplicationInfo() #查看日志文件能夠使用的大小 默認oplog大小會占用64位實例5%的可用磁盤空間
configured oplog size: 95.37109375MB
log length start to end: 1103secs (0.31hrs)
oplog first event time: Thu Jul 19 2018 08:43:55 GMT+0800 (CST)
oplog last event time: Thu Jul 19 2018 09:02:18 GMT+0800 (CST)
now: Thu Jul 19 2018 09:20:08 GMT+0800 (CST)
test:PRIMARY> db.runCommand({"convertToCapped":"oplog.rs","size":10000000000}) #修改 單位:B
{ "ok" : 1 }
test:PRIMARY> rs.printReplicationInfo()
configured oplog size: 9536.746032714844MB
log length start to end: 1103secs (0.31hrs)
oplog first event time: Thu Jul 19 2018 08:43:55 GMT+0800 (CST)
oplog last event time: Thu Jul 19 2018 09:02:18 GMT+0800 (CST)
now: Thu Jul 19 2018 09:20:24 GMT+0800 (CST)
test:PRIMARY>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。