搭建副本集是爲了實現mongodb高可用。java
Mongodb(M)表示主節點,Mongodb(S)表示備節點,Mongodb(A)表示仲裁節點。主備節點存儲數據,仲裁節點不存儲數據。客戶端同時鏈接主節點與備節點,不鏈接仲裁節點。node
仲裁節點是一種特殊的節點,它自己並不存儲數據,主要的做用是決定哪個備節點在主節點掛掉以後提高爲主節點,因此客戶端不須要鏈接此節點。mongodb
在MongoDB副本集中,主節點負責處理客戶端的讀寫請求,備份節點則負責映射主節點的數據。shell
備份節點的工做原理過程能夠大體描述爲,備份節點按期輪詢主節點上的數據操做,而後對本身的數據副本進行這些操做,從而保證跟主節點的數據同步。至於主節點上的全部 數據庫狀態改變 的操做,都會存放在一張特定的系統表中。備份節點則是根據這些數據進行本身的數據更新。數據庫
上面提到的 數據庫狀態改變 的操做,稱爲oplog(operation log,主節點操做記錄)。oplog存儲在local數據庫的"oplog.rs"表中。副本集中備份節點異步的從主節點同步oplog,而後從新執行它記錄的操做,以此達到了數據同步的做用。vim
Oplog注意點:bash
初始化:這個過程發生在當副本集中建立一個新的數據庫或其中某個節點剛從宕機中恢復,或者向副本集中添加新的成員的時候,默認的,副本集中的節點會從離它最近的節點複製oplog來同步數據,這個最近的節點能夠是primary也能夠是擁有最新oplog副本的secondary節點。服務器
1.進入到/usr/java中,新建mongodbRepliSet文件夾,而後在 mongodbRepliSet 文件夾中新建 3個節點,先 mkdir node1。app
2.進入到 node1中,新建 data 和 log文件夾,即 mkdir data log,而後進入到 data中,mkdir db。異步
3.拷貝mongodb的配置文件到 node1中,
cp /usr/java/mongoNode/mongodb.conf /usr/java/mongodbRepliSet/node1/mongodb.conf
4.修改配置文件,vim mongodb.conf
dbpath=/usr/java/mongodbRepliSet/node1/data//db
logpath=/usr/java/mongodbRepliSet/node1/log/mongodb.log
logappend=true
fork=true
bind_ip=192.168.80.128
port=27017
replSet=JoeSet # 3個節點的這個配置要同樣,表示在一個副本集中
複製代碼
5.拷貝 node1 整個文件夾,名爲 node2 和 node3
cp -r node1 node2
cp -r node1 node3
複製代碼
6.修改 node2 和 node3 文件夾中的mongodb.conf,修改 dbpath、logpath 和 port(2701八、27019)。
7.配置 臨時的環境變量,export PATH=/usr/java/mongodb/bin:$PATH
8.查看臨時的環境變量是否配置成功:echo $PATH
9.啓動3個節點,分別進入 node1 node2和node3中,以配置文件方式啓動:mongod --config mongodb.conf
10.客戶端鏈接node1,mongo --host 192.168.80.128 --port 27017
11.上述配置 沒有指定哪個是master、slave、仲裁節點,因此須要執行下 副本集的初始化,執行:
rs.initiate({"_id":"JoeSet",members:[{"_id":1,"host":"192.168.80.128:27017",priority:3},{"_id":2,"host":"192.168.80.128:27018", priority:9},{"_id":3,"host":"192.168.80.128:27019",arbiterOnly:true}]})。
執行完 初始化命令後,會變爲上圖所示。
注意,可能出現的錯誤:
> rs.initiate({"_id":"JoeSet",members:[{"_id":1,"host":"192.168.80.128:27017",priority:3},{"_id":2,"host":"192.168.80.128:27018", priority:9},{"_id":4,"host":"192.168.80.128:27019",arbiterOnly:true}]})
{
"ok" : 0,
"errmsg" : "This node, 192.168.80.128:27019, with _id 4 is not electable under the new configuration version 1 for replica set JoeSet",
"code" : 93
}
複製代碼
錯誤緣由,是由於客戶端鏈接了 27019,而27019 又是仲裁節點,因此出現了這個錯誤。解決方法,客戶端鏈接 其餘節點,不鏈接 設置爲仲裁的節點。
12.執行:rs.status(),查看狀態。
JoeSet:OTHER> rs.status()
{
"set" : "JoeSet",
"date" : ISODate("2017-07-26T22:09:44.940Z"),
"myState" : 2,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",        # SECONDARY 表示從節點
"uptime" : 38,
"optime" : Timestamp(1501106974, 1),
"optimeDate" : ISODate("2017-07-26T22:09:34Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.80.128:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",        # PRIMARY 表示主節點
"uptime" : 10,
"optime" : Timestamp(1501106974, 1),
"optimeDate" : ISODate("2017-07-26T22:09:34Z"),
"lastHeartbeat" : ISODate("2017-07-26T22:09:44.560Z"),
"lastHeartbeatRecv" : ISODate("2017-07-26T22:09:44.579Z"),
"pingMs" : 1,
"electionTime" : Timestamp(1501106981, 1),
"electionDate" : ISODate("2017-07-26T22:09:41Z"),
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.80.128:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",        # ARBITER 表示仲裁節點
"uptime" : 10,
"lastHeartbeat" : ISODate("2017-07-26T22:09:44.559Z"),
"lastHeartbeatRecv" : ISODate("2017-07-26T22:09:44.585Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
複製代碼
13.爲了驗證副本集搭建成功,咱們 在node2(主節點) 中插入幾條數據,而後在 node1 (從節點)中查看,由於 上面配置了 node2 爲主節點, node1爲從節點,node3爲仲裁節點。
JoeSet:PRIMARY> show dbs
local 0.078GB
JoeSet:PRIMARY> use testdb
switched to db testdb
JoeSet:PRIMARY> db.createCollection("testCon")
{ "ok" : 1 }
JoeSet:PRIMARY> show collections
system.indexes
testCon
JoeSet:PRIMARY> for(var i=0; i<3;i++) db.testCon.insert({name:"Joe",index:i})
WriteResult({ "nInserted" : 1 })
JoeSet:PRIMARY> db.testCon.find()
{ "_id" : ObjectId("597a553d7db09ad9f77f1353"), "name" : "Joe", "index" : 0 }
{ "_id" : ObjectId("597a553d7db09ad9f77f1354"), "name" : "Joe", "index" : 1 }
{ "_id" : ObjectId("597a553d7db09ad9f77f1355"), "name" : "Joe", "index" : 2 }
複製代碼
JoeSet:SECONDARY> show dbs
2017-07-27T14:06:14.672-0700 E QUERY Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
at Error (<anonymous>)
at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
at shellHelper.show (src/mongo/shell/utils.js:630:33)
at shellHelper (src/mongo/shell/utils.js:524:36)
at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
JoeSet:SECONDARY> rs.slaveOk()
JoeSet:SECONDARY> show dbs
local 0.078GB
testdb 0.078GB
JoeSet:SECONDARY> use testdb
switched to db testdb
JoeSet:SECONDARY> show collections
system.indexes
testCon
JoeSet:SECONDARY> db.testCon.find()
{ "_id" : ObjectId("597a553d7db09ad9f77f1353"), "name" : "Joe", "index" : 0 }
{ "_id" : ObjectId("597a553d7db09ad9f77f1354"), "name" : "Joe", "index" : 1 }
{ "_id" : ObjectId("597a553d7db09ad9f77f1355"), "name" : "Joe", "index" : 2 }
複製代碼
在node1(從節點)中 能夠查看到 node2(主節點)插入的數據,說明 副本集搭建成功。
注意:從節點中是不容許執行寫操做的。
模擬node2(主節點)掛了,kiil 掉node2(主節點)的進程
客戶端鏈接 node1(從節點):mongo --host 192.168.80.128 --port 27017
JoeSet:PRIMARY> rs.status()
{
"set" : "JoeSet",
"date" : ISODate("2017-07-27T21:20:45.629Z"),
"myState" : 1,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4209,
"optime" : Timestamp(1501189437, 10),
"optimeDate" : ISODate("2017-07-27T21:03:57Z"),
"electionTime" : Timestamp(1501190187, 1),
"electionDate" : ISODate("2017-07-27T21:16:27Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.80.128:27018",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-07-27T21:20:43.773Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T21:16:24.292Z"),
"pingMs" : 1,
"lastHeartbeatMessage" : "Failed attempt to connect to 192.168.80.128:27018; couldn't connect to server 192.168.80.128:27018 (192.168.80.128), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 3,
"name" : "192.168.80.128:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 4191,
"lastHeartbeat" : ISODate("2017-07-27T21:20:45.475Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T21:20:44.876Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
複製代碼
發現原來的從節點(node1)變爲了 主節點,而原來的主節點顯示的狀態是 不可達、不健康的。這對於整個副本集的使用是沒有影響的。
從新啓動node2,即 原來的主節點。若是啓動node2失敗,就刪除掉 db文件夾下的mongod.lock文件。
客戶端鏈接node1,mongo --host 192.168.80.128 --port 27017,發現node2 從新啓動後,node1 又變爲 從節點,而 node2又變爲 原來的主節點,這是由於 仲裁節點 和 設置的優先級的緣由。
JoeSet:SECONDARY> rs.status()
{
"set" : "JoeSet",
"date" : ISODate("2017-07-27T21:31:06.197Z"),
"myState" : 2,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 4830,
"optime" : Timestamp(1501189437, 10),
"optimeDate" : ISODate("2017-07-27T21:03:57Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.80.128:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 87,
"optime" : Timestamp(1501189437, 10),
"optimeDate" : ISODate("2017-07-27T21:03:57Z"),
"lastHeartbeat" : ISODate("2017-07-27T21:31:05.209Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T21:31:05.506Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1501190981, 1),
"electionDate" : ISODate("2017-07-27T21:29:41Z"),
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.80.128:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 4811,
"lastHeartbeat" : ISODate("2017-07-27T21:31:06.085Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T21:31:05.633Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
複製代碼
當仲裁節點掛掉後,達不到高可用了,即當 主節點掛了後,從節點在沒有仲裁節點的狀況下,不會切換爲 主節點了。因此,推薦使用沒有仲裁節點的副本集,以下。
1.複製node1 爲 node六、node七、node8:
cp -r node1 node6
cp -r node1 node7
cp -r node1 node8
複製代碼
2.清空node六、node七、node8的db文件夾 和 log 文件夾
rm -rf data/db/*
rm -rf log/*
複製代碼
3.修改node六、node七、node8 的datapath、logpath和port、replSet
node6 的port 爲 27021,node7 的port 爲 27022,node8 的port 爲 27023,replSet 爲 XbqSet
4.啓動三個節點,分別進入 對應的node文件夾中:
mongod --config mongodb.conf
5.鏈接node6節點(27021):mongo --host 192.168.80.128 --port 27021
6.執行初始化命令:rs.initiate({"_id":"XbqSet",members:[{"_id":1,"host":"192.168.80.128:27021"},{"_id":2,"host":"192.168.80.128:27022"},{"_id":3,"host":"192.168.80.128:27023" }]})
7.查看狀態,發現有一個主節點,兩個從節點。
XbqSet:PRIMARY> rs.status()
{
"set" : "XbqSet",
"date" : ISODate("2017-07-27T22:02:28.188Z"),
"myState" : 1,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27021",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 483,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"electionTime" : Timestamp(1501192876, 1),
"electionDate" : ISODate("2017-07-27T22:01:16Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.80.128:27022",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 75,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:02:26.973Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:02:26.973Z"),
"pingMs" : 0,
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.80.128:27023",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 75,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:02:26.973Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:02:26.973Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
複製代碼
8.kill 掉node6,即端口 27021
9.而後鏈接到node7上:mongo --host 192.168.80.128 --port 27022,查看狀態,發現node7爲從節點,node8爲主節點了,原來的node6 不可達、不健康的。
XbqSet:SECONDARY> rs.status()
{
"set" : "XbqSet",
"date" : ISODate("2017-07-27T22:08:24.894Z"),
"myState" : 2,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27021",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:08:23.748Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:07:43.464Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to 192.168.80.128:27021; couldn't connect to server 192.168.80.128:27021 (192.168.80.128), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 2,
"name" : "192.168.80.128:27022",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 833,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 3,
"name" : "192.168.80.128:27023",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 431,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:08:23.409Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:08:23.521Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1501193266, 1),
"electionDate" : ISODate("2017-07-27T22:07:46Z"),
"configVersion" : 1
}
],
"ok" : 1
}
複製代碼
10.從新啓動node6:mongod --config mongodb.conf
11.繼續鏈接node7:mongo --host 192.168.80.128 --port 27022,查看狀態,原來的node6 變爲了 從節點。
XbqSet:SECONDARY> rs.status()
{
"set" : "XbqSet",
"date" : ISODate("2017-07-27T22:12:41.275Z"),
"myState" : 2,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27021",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 54,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:12:40.382Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:12:41.213Z"),
"pingMs" : 0,
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.80.128:27022",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1090,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 3,
"name" : "192.168.80.128:27023",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 688,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:12:39.673Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:12:39.820Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1501193266, 1),
"electionDate" : ISODate("2017-07-27T22:07:46Z"),
"configVersion" : 1
}
],
"ok" : 1
}
複製代碼
1.cp -r node8 node10,修改node10的datapath、logpath的port,並將port 改成 27024,啓動 node10。
2.進入到主節點中,mongo --host 192.168.80.128 --port 27023,必定要在主節點中進行操做。
3.增長節點:rs.add(「192.168.80.128:27024」)
XbqSet:PRIMARY> rs.add("192.168.80.128:27024")
{ "ok" : 1 }
複製代碼
4.查看狀態,rs.status(),發現新增長的節點 爲從節點,即 SECONDARY。
XbqSet:PRIMARY> rs.status()
{
"set" : "XbqSet",
"date" : ISODate("2017-07-29T14:31:44.872Z"),
"myState" : 1,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27021",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 630,
"optime" : Timestamp(1501338697, 1),
"optimeDate" : ISODate("2017-07-29T14:31:37Z"),
"lastHeartbeat" : ISODate("2017-07-29T14:31:43.555Z"),
"lastHeartbeatRecv" : ISODate("2017-07-29T14:31:43.457Z"),
"pingMs" : 0,
"configVersion" : 2
},
{
"_id" : 2,
"name" : "192.168.80.128:27022",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1854,
"optime" : Timestamp(1501338697, 1),
"optimeDate" : ISODate("2017-07-29T14:31:37Z"),
"lastHeartbeat" : ISODate("2017-07-29T14:31:43.555Z"),
"lastHeartbeatRecv" : ISODate("2017-07-29T14:31:43.186Z"),
"pingMs" : 0,
"configVersion" : 2
},
{
"_id" : 3,
"name" : "192.168.80.128:27023",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1953,
"optime" : Timestamp(1501338697, 1),
"optimeDate" : ISODate("2017-07-29T14:31:37Z"),
"electionTime" : Timestamp(1501338022, 1),
"electionDate" : ISODate("2017-07-29T14:20:22Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 4,
"name" : "192.168.80.128:27024",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 7,
"optime" : Timestamp(1501338697, 1),
"optimeDate" : ISODate("2017-07-29T14:31:37Z"),
"lastHeartbeat" : ISODate("2017-07-29T14:31:43.566Z"),
"lastHeartbeatRecv" : ISODate("2017-07-29T14:31:43.579Z"),
"pingMs" : 6,
"configVersion" : 2
}
],
"ok" : 1
}
複製代碼
5.刪除節點:rs.remove(hostportstr)
XbqSet:PRIMARY> rs.remove("192.168.80.128:27024")
{ "ok" : 1 }
複製代碼
查看狀態,發現 端口爲27024的節點 沒有了。