MongoDB系列二:Replica Sets安裝與配置
Replica Sets一般歸入到高可用範疇內,提供服務的高度連續可用,降低服務的間斷時間;而且Replica Sets也可以用在容災、備份/恢復、負載均衡等方面。
1、Slave可以執行查詢,降低Master壓力;
2、在Slave上進行備份,避免備份中將Master鎖定,造成Master不可方面的情況;
3、Master出現故障,可以快速切換到Slave上,使得服務不間斷;
4、多臺Slave可以和負載均衡器組合,提供高效能的可訪問服務。
一、規劃:
按照上圖,安裝配置3節點的Replica Sets,使用的作業系統是RHEL6.4,具體安裝軟體請參見“MongoDB系列一”。
相應的儲存路徑和埠定義,如下表:
二、準備:
# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
如果不執行,啟動mongod,日誌中會顯示相應的警告。
三、建立相關目錄
# su - mongodb
$ mkdir /mongodb/data/r{0,1,2}
$ mkdir /mongodb/log
四、啟動每個節點的mongod
$ mongod --replSet rs1 --fork --port 28010 --dbpath /mongodb/data/r0/ --logpath /mongodb/log/r0.log --logappend --smallfiles --oplogSize 128
about to fork child process, waiting until server is ready for connections.
forked process: 11552
child process started successfully, parent exiting
$ mongod --replSet rs1 --fork --port 28011 --dbpath /mongodb/data/r1/ --logpath /mongodb/log/r1.log --logappend --smallfiles --oplogSize 128
$ mongod --replSet rs1 --fork --port 28012 --dbpath /mongodb/data/r2/ --logpath /mongodb/log/r2.log --logappend --smallfiles --oplogSize 128
說明:
--replSet Replica Sets的名稱
--fork 以建立子程式的方式執行
--port 服務使用的埠
--dbpath 資料檔案儲存的路徑
--logpath 日誌檔案儲存的路徑
--logappend 日誌寫入日誌問題的方式“追加”
--smallfiles 使用小檔案的預設大小
--oplogSize 複製日誌的空間大小(MB)
五、配置Replica Sets
連線R0,在R0做配置。
$ mongo --port 28010
MongoDB shell version: 3.2.6
connecting to: 127.0.0.1:28010/test
> rsconf = {
"_id" : "rs1",
"members" : [
{
"_id" : 0,
"host" : "localhost:28010"
}
]
}
> rs.initiate(rsconf);
{ "ok" : 1 }
rs1:OTHER> rs.conf();
{
"_id" : "rs1",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "localhost:28010",
"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("57c9095e7bf8b5d9980b2e9d")
}
}
rs1:PRIMARY> rs.add("localhost:28011");
{ "ok" : 1 }
rs1:PRIMARY> rs.add("localhost:28012");
{ "ok" : 1 }
rs1:PRIMARY> rs.conf();
{
"_id" : "rs1",
"version" : 3,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "localhost:28010",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:28011",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:28012",
"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("57c9095e7bf8b5d9980b2e9d")
}
}
配置完成。
六、檢視狀態,以及檢查Replica Sets部署是否成功
1)檢視狀態
rs1:PRIMARY> rs.status();
{
"set" : "rs1",
"date" : ISODate("2016-09-02T05:12:10.162Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "localhost:28010",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 450,
"optime" : {
"ts" : Timestamp(1472793094, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-09-02T05:11:34Z"),
"electionTime" : Timestamp(1472792926, 2),
"electionDate" : ISODate("2016-09-02T05:08:46Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "localhost:28011",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 39,
"optime" : {
"ts" : Timestamp(1472793094, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-09-02T05:11:34Z"),
"lastHeartbeat" : ISODate("2016-09-02T05:12:08.894Z"),
"lastHeartbeatRecv" : ISODate("2016-09-02T05:12:09.898Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "localhost:28010",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "localhost:28012",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 35,
"optime" : {
"ts" : Timestamp(1472793094, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-09-02T05:11:34Z"),
"lastHeartbeat" : ISODate("2016-09-02T05:12:08.894Z"),
"lastHeartbeatRecv" : ISODate("2016-09-02T05:12:06.897Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1
}
2)檢查
(a)插入記錄
rs1:PRIMARY> use test
switched to db test
rs1:PRIMARY> db.t1.insert({name:"john",age:10});
WriteResult({ "nInserted" : 1 })
rs1:PRIMARY> db.t1.find();
{ "_id" : ObjectId("57c923dc296d0a6382e17195"), "name" : "john", "age" : 10 }
(b)檢查同步日誌
rs1:PRIMARY> use local
switched to db local
rs1:PRIMARY> db.oplog.rs.find();
{ "ts" : Timestamp(1472792926, 1), "h" : NumberLong("-1551070289351464931"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } }
{ "ts" : Timestamp(1472792927, 1), "t" : NumberLong(1), "h" : NumberLong("-6644373458713625013"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "new primary" } }
{ "ts" : Timestamp(1472793091, 1), "t" : NumberLong(1), "h" : NumberLong("-3467321571589321913"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 2 } }
{ "ts" : Timestamp(1472793094, 1), "t" : NumberLong(1), "h" : NumberLong("5469898305152102871"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 3 } }
{ "ts" : Timestamp(1472799708, 1), "t" : NumberLong(1), "h" : NumberLong("-2309306902204899951"), "v" : 2, "op" : "c", "ns" : "test.$cmd", "o" : { "create" : "t1" } }
{ "ts" : Timestamp(1472799708, 2), "t" : NumberLong(1), "h" : NumberLong("-7169457093293246964"), "v" : 2, "op" : "i", "ns" : "test.t1", "o" : { "_id" : ObjectId("57c923dc296d0a6382e17195"), "name" : "john", "age" : 10 } }
注意:紅色部分即是插入的記錄資料,在此出現,說明在Master插入的記錄,已經進入同步日誌,進行同步,需要到Slave去驗證是否接收到該記錄。
(c)連線Slave,檢查該記錄是否可以查詢
$ mongo --port 28011
MongoDB shell version: 3.2.6
connecting to: 127.0.0.1:28011/test
rs1:SECONDARY> use test
switched to db test
rs1:SECONDARY> db.t1.find();
Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }
注:MongoDB預設Slave是不能讀的,所以需要進行如下配置。
rs1:SECONDARY> db.getMongo().setSlaveOk();
查詢:
rs1:SECONDARY> db.t1.find();
{ "_id" : ObjectId("57c923dc296d0a6382e17195"), "name" : "john", "age" : 10 }
至此,可以證明Replica Sets是安裝配置成功的。
1、Slave可以執行查詢,降低Master壓力;
2、在Slave上進行備份,避免備份中將Master鎖定,造成Master不可方面的情況;
3、Master出現故障,可以快速切換到Slave上,使得服務不間斷;
4、多臺Slave可以和負載均衡器組合,提供高效能的可訪問服務。
一、規劃:
按照上圖,安裝配置3節點的Replica Sets,使用的作業系統是RHEL6.4,具體安裝軟體請參見“MongoDB系列一”。
相應的儲存路徑和埠定義,如下表:
|
R0 | R1 | R2 |
資料儲存路徑 | /mongodb/data/r0 | /mongodb/data/r1 | /mongodb/data/r2 |
日誌儲存路徑 | /mongodb/log/r0.log | /mongodb/log/r1.log | /mongodb/log/r2.log |
Port | 28010 | 28011 | 28012 |
二、準備:
# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
如果不執行,啟動mongod,日誌中會顯示相應的警告。
三、建立相關目錄
# su - mongodb
$ mkdir /mongodb/data/r{0,1,2}
$ mkdir /mongodb/log
四、啟動每個節點的mongod
$ mongod --replSet rs1 --fork --port 28010 --dbpath /mongodb/data/r0/ --logpath /mongodb/log/r0.log --logappend --smallfiles --oplogSize 128
about to fork child process, waiting until server is ready for connections.
forked process: 11552
child process started successfully, parent exiting
$ mongod --replSet rs1 --fork --port 28011 --dbpath /mongodb/data/r1/ --logpath /mongodb/log/r1.log --logappend --smallfiles --oplogSize 128
$ mongod --replSet rs1 --fork --port 28012 --dbpath /mongodb/data/r2/ --logpath /mongodb/log/r2.log --logappend --smallfiles --oplogSize 128
說明:
--replSet Replica Sets的名稱
--fork 以建立子程式的方式執行
--port 服務使用的埠
--dbpath 資料檔案儲存的路徑
--logpath 日誌檔案儲存的路徑
--logappend 日誌寫入日誌問題的方式“追加”
--smallfiles 使用小檔案的預設大小
--oplogSize 複製日誌的空間大小(MB)
五、配置Replica Sets
連線R0,在R0做配置。
$ mongo --port 28010
MongoDB shell version: 3.2.6
connecting to: 127.0.0.1:28010/test
> rsconf = {
"_id" : "rs1",
"members" : [
{
"_id" : 0,
"host" : "localhost:28010"
}
]
}
> rs.initiate(rsconf);
{ "ok" : 1 }
rs1:OTHER> rs.conf();
{
"_id" : "rs1",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "localhost:28010",
"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("57c9095e7bf8b5d9980b2e9d")
}
}
rs1:PRIMARY> rs.add("localhost:28011");
{ "ok" : 1 }
rs1:PRIMARY> rs.add("localhost:28012");
{ "ok" : 1 }
rs1:PRIMARY> rs.conf();
{
"_id" : "rs1",
"version" : 3,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "localhost:28010",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:28011",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:28012",
"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("57c9095e7bf8b5d9980b2e9d")
}
}
配置完成。
六、檢視狀態,以及檢查Replica Sets部署是否成功
1)檢視狀態
rs1:PRIMARY> rs.status();
{
"set" : "rs1",
"date" : ISODate("2016-09-02T05:12:10.162Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "localhost:28010",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 450,
"optime" : {
"ts" : Timestamp(1472793094, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-09-02T05:11:34Z"),
"electionTime" : Timestamp(1472792926, 2),
"electionDate" : ISODate("2016-09-02T05:08:46Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "localhost:28011",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 39,
"optime" : {
"ts" : Timestamp(1472793094, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-09-02T05:11:34Z"),
"lastHeartbeat" : ISODate("2016-09-02T05:12:08.894Z"),
"lastHeartbeatRecv" : ISODate("2016-09-02T05:12:09.898Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "localhost:28010",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "localhost:28012",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 35,
"optime" : {
"ts" : Timestamp(1472793094, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-09-02T05:11:34Z"),
"lastHeartbeat" : ISODate("2016-09-02T05:12:08.894Z"),
"lastHeartbeatRecv" : ISODate("2016-09-02T05:12:06.897Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1
}
2)檢查
(a)插入記錄
rs1:PRIMARY> use test
switched to db test
rs1:PRIMARY> db.t1.insert({name:"john",age:10});
WriteResult({ "nInserted" : 1 })
rs1:PRIMARY> db.t1.find();
{ "_id" : ObjectId("57c923dc296d0a6382e17195"), "name" : "john", "age" : 10 }
(b)檢查同步日誌
rs1:PRIMARY> use local
switched to db local
rs1:PRIMARY> db.oplog.rs.find();
{ "ts" : Timestamp(1472792926, 1), "h" : NumberLong("-1551070289351464931"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } }
{ "ts" : Timestamp(1472792927, 1), "t" : NumberLong(1), "h" : NumberLong("-6644373458713625013"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "new primary" } }
{ "ts" : Timestamp(1472793091, 1), "t" : NumberLong(1), "h" : NumberLong("-3467321571589321913"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 2 } }
{ "ts" : Timestamp(1472793094, 1), "t" : NumberLong(1), "h" : NumberLong("5469898305152102871"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 3 } }
{ "ts" : Timestamp(1472799708, 1), "t" : NumberLong(1), "h" : NumberLong("-2309306902204899951"), "v" : 2, "op" : "c", "ns" : "test.$cmd", "o" : { "create" : "t1" } }
{ "ts" : Timestamp(1472799708, 2), "t" : NumberLong(1), "h" : NumberLong("-7169457093293246964"), "v" : 2, "op" : "i", "ns" : "test.t1", "o" : { "_id" : ObjectId("57c923dc296d0a6382e17195"), "name" : "john", "age" : 10 } }
注意:紅色部分即是插入的記錄資料,在此出現,說明在Master插入的記錄,已經進入同步日誌,進行同步,需要到Slave去驗證是否接收到該記錄。
(c)連線Slave,檢查該記錄是否可以查詢
$ mongo --port 28011
MongoDB shell version: 3.2.6
connecting to: 127.0.0.1:28011/test
rs1:SECONDARY> use test
switched to db test
rs1:SECONDARY> db.t1.find();
Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }
注:MongoDB預設Slave是不能讀的,所以需要進行如下配置。
rs1:SECONDARY> db.getMongo().setSlaveOk();
查詢:
rs1:SECONDARY> db.t1.find();
{ "_id" : ObjectId("57c923dc296d0a6382e17195"), "name" : "john", "age" : 10 }
至此,可以證明Replica Sets是安裝配置成功的。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26812308/viewspace-2124412/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MongoDB系列三:Replica Sets在生產環境中安裝配置的注意事項MongoDB
- mongodb replica sets 測試MongoDB
- MongoDB 複製集模式Replica SetsMongoDB模式
- Simple Automated Backups for MongoDB Replica SetsMongoDB
- 【MongoDB】安裝與配置MongoDB
- mongodb 3.0 replica set 配置MongoDB
- mongodb複製集(replica sets)+分片(sharding)環境搭建MongoDB
- Mac 安裝與配置mongodbMacMongoDB
- Linux mongodb 安裝與配置LinuxMongoDB
- Mongodb入門——安裝與配置MongoDB
- Replica sets複製集的搭建
- 學習MongoDB 一:MongoDB 入門(安裝與配置)MongoDB
- 【mongodb安裝配置】MongoDB
- MongoDB安裝配置教程MongoDB
- 【Mongodb】 Replica set 的 選舉策略之二MongoDB
- 小丸子學MongoDB系列之——安裝MongoDBMongoDB
- 利用Mongodb的複製集搭建高可用分片,Replica Sets + Sharding的搭建過程MongoDB
- MongoDB之安裝並配置MongoDB
- mongodb的安裝配置,使用MongoDB
- 【Mongodb】如何建立mongodb的replica setMongoDB
- Kubernetes Replica Sets 和 Kubernetes Replica Controller的區別Controller
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- MongoDB系列二MongoDB
- 小丸子學MongoDB系列之——部署Replica Set+Sharded ClusterMongoDB
- Varnish(二)Varnish4.0安裝與配置
- MongoDB下載與安裝MongoDB
- MongoDB 安裝與啟動MongoDB
- [Mongodb]安裝與啟動MongoDB
- 本地windows搭建spark環境,安裝與詳細配置(jdk安裝與配置,scala安裝與配置,hadoop安裝與配置,spark安裝與配置)WindowsSparkJDKHadoop
- MongoDB Windows環境安裝及配置MongoDBWindows
- Mongodb在Windows下安裝及配置MongoDBWindows
- MongoDB系列-解決面試中可能遇到的MongoDB複製集(replica set)問題MongoDB面試
- [MONGODB]: WHEN ARBITER REQUIRED FOR REPLICA SETMongoDBUI
- 手把手教你MongoDB的安裝與詳細使用(二)MongoDB
- Jenkins入門系列之——02第二章 Jenkins安裝與配置Jenkins
- Python第二課 -PyCharm安裝與配置PythonPyCharm
- mongodb replica set 和 nodejs中使用mongoose連線replicaMongoDBNodeJS
- Mongodb的下載與安裝MongoDB