小丸子學MongoDB系列之——部署MongoDB副本集
1.以副本集的方式啟動mongodb例項
1.1 建立副本集目錄
[mgousr01@vm1 ~]$ mkdir -p mongorep/{mg17/{bin,conf,data,logs,pid},mg27/{bin,conf,data,logs,pid},mg37/{bin,conf,data,logs,pid}}
[mgousr01@vm1 ~]$ ls -R mongorep/
mongorep/:
mg17 mg27 mg37
mongorep/mg17:
bin conf data logs pid
mongorep/mg27:
bin conf data logs pid
mongorep/mg37:
bin conf data logs pid
1.2 配置mongodb副本集(所有例項都需配置,以mg17為例)
[mgousr01@vm1 ~]$ vi /appbase/users/mgousr01/mongorep/mg17/conf/mg17.conf
dbpath=/appbase/users/mgousr01/mongorep/mg17/data/
logpath=/appbase/users/mgousr01/mongorep/mg17/logs/mg17.log
pidfilepath=/appbase/users/mgousr01/mongorep/mg17/pid/mg17.pid
directoryperdb=true
logappend=true
bind_ip=192.168.157.128
port=47017
oplogSize=10240
fork=true
replSet=rstl
1.3 啟動mongodb例項
[mgousr01@vm1 ~]$ vi /appbase/users/mgousr01/mongorep/mg17/bin/start_mongodb.sh
mongod -f /appbase/users/mgousr01/mongorep/mg17/conf/mg17.conf
[mgousr01@vm1 ~]$ chmod +x /appbase/users/mgousr01/mongorep/mg17/bin/start_mongodb.sh
[mgousr01@vm1 ~]$ /appbase/users/mgousr01/mongorep/mg17/bin/start_mongodb.sh
1.4 檢視mongodb例項的監聽埠
[mgousr01@vm1 ~]$ ps -ef|grep mongod|grep -v grep
mgousr01 3688 1 0 11:04 ? 00:00:01 mongod -f /appbase/users/mgousr01/mongorep/mg17/conf/mg17.conf
mgousr01 3707 1 0 11:05 ? 00:00:00 mongod -f /appbase/users/mgousr01/mongorep/mg27/conf/mg27.conf
mgousr01 3726 1 1 11:05 ? 00:00:00 mongod -f /appbase/users/mgousr01/mongorep/mg37/conf/mg37.conf
[mgousr01@vm1 ~]$ netstat -tnpl|grep mongod
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 192.168.157.128:47027 0.0.0.0:* LISTEN 3707/mongod
tcp 0 0 192.168.157.128:47037 0.0.0.0:* LISTEN 3726/mongod
tcp 0 0 192.168.157.128:47017 0.0.0.0:* LISTEN 3688/mongod
注:從輸出資訊可以判斷所有的mongodb副本整合員已經啟動,分別佔用了監聽埠47017,47027,47037
2.連線到其中一個副本整合員
[mgousr01@vm1 ~]$ mongo 192.168.157.128:47017
MongoDB shell version: 3.0.3
connecting to: 192.168.157.128:47017/test
>
3.初始化副本集(一主兩從架構)
> cfg=
{
"_id" : "rstl",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "192.168.157.128:47017"
},
{
"_id" : 1,
"host" : "192.168.157.128:47027"
},
{
"_id" : 2,
"host" : "192.168.157.128:47037"
}
]
}
> rs.initiate(cfg)
{ "ok" : 1 }
rstl:PRIMARY> rs.status()
{
"set" : "rstl",
"date" : ISODate("2015-12-03T09:14:07.046Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.157.128:47017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4447,
"optime" : Timestamp(1449131360, 1),
"optimeDate" : ISODate("2015-12-03T08:29:20Z"),
"electionTime" : Timestamp(1449131364, 1),
"electionDate" : ISODate("2015-12-03T08:29:24Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.157.128:47027",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2686,
"optime" : Timestamp(1449131360, 1),
"optimeDate" : ISODate("2015-12-03T08:29:20Z"),
"lastHeartbeat" : ISODate("2015-12-03T09:14:06.765Z"),
"lastHeartbeatRecv" : ISODate("2015-12-03T09:14:06.899Z"),
"pingMs" : 0,
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.157.128:47037",
"health" : 1,
"state" : 5,
"stateStr" : "STARTUP2",
"uptime" : 2686,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-12-03T09:14:06.723Z"),
"lastHeartbeatRecv" : ISODate("2015-12-03T09:14:06.852Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
注:觀察到第三個成員節點的狀態為"STARTUP2",證明副本集初始化沒有成功,出現這個原因是磁碟空間不夠導致的。
解決方法是將啟動引數的oplogSize設定小一點或者增加磁碟空間,正常成員狀態顯示如下:
rstl:PRIMARY> rs.status();
{
"set" : "rstl",
"date" : ISODate("2015-12-04T06:31:40.931Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.157.128:47017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 71,
"optime" : Timestamp(1449210685, 1),
"optimeDate" : ISODate("2015-12-04T06:31:25Z"),
"electionTime" : Timestamp(1449210687, 1),
"electionDate" : ISODate("2015-12-04T06:31:27Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.157.128:47027",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 14,
"optime" : Timestamp(1449210685, 1),
"optimeDate" : ISODate("2015-12-04T06:31:25Z"),
"lastHeartbeat" : ISODate("2015-12-04T06:31:39.964Z"),
"lastHeartbeatRecv" : ISODate("2015-12-04T06:31:39.973Z"),
"pingMs" : 0,
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.157.128:47037",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 14,
"optime" : Timestamp(1449210685, 1),
"optimeDate" : ISODate("2015-12-04T06:31:25Z"),
"lastHeartbeat" : ISODate("2015-12-04T06:31:39.964Z"),
"lastHeartbeatRecv" : ISODate("2015-12-04T06:31:39.973Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
4.驗證資料同步是否正常
rstl:PRIMARY> use soho
switched to db soho
rstl:PRIMARY> db.food.insert({name:"egg",price:38})
WriteResult({ "nInserted" : 1 })
rstl:PRIMARY> show collections
food
system.indexes
rstl:PRIMARY> db.food.find()
{ "_id" : ObjectId("5664fdfe1830846a3331ce02"), "name" : "egg", "price" : 38 }
[mgousr01@vm1 ~]$ mongo 192.168.157.128:47027
MongoDB shell version: 3.0.3
connecting to: 192.168.157.128:47027/test
rstl:SECONDARY> show dbs;
2015-12-07T11:34:05.753+0800 E QUERY Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
at Error ()
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
rstl:SECONDARY> rs.slaveOk()
rstl:SECONDARY> show dbs;
local 0.203GB
soho 0.078GB
rstl:SECONDARY> use soho
switched to db soho
rstl:SECONDARY> show collections
food
system.indexes
rstl:SECONDARY> db.food.find();
{ "_id" : ObjectId("5664fdfe1830846a3331ce02"), "name" : "egg", "price" : 38 }
至此mongodb副本集搭建成功,後續將會研究兩種常見的副本集架構(1主2從和1主1從1仲裁)在主節點掛了後的auto failover現象。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/20801486/viewspace-1853447/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 小丸子學MongoDB系列之——副本集Auto-FailoverMongoDBAI
- 小丸子學MongoDB系列之——安裝MongoDBMongoDB
- 小丸子學MongoDB系列之——部署Replica Set+Sharded ClusterMongoDB
- 小丸子學MongoDB系列之——副本集在容災環境中的故障演練MongoDB
- mongodb 4.0副本集搭建MongoDB
- MongoDB 6.0.3副本集搭建MongoDB
- MongoDB部署副本集MongoDB
- MongoDB之副本集MongoDB
- MongoDB日常運維-04副本集搭建MongoDB運維
- MongoDB日常運維-05副本集故障切換MongoDB運維
- MongoDB副本集MongoDB
- MongoDB 4.2副本集新增/刪除副本(一主一副一仲裁)MongoDB
- MongoDB 副本集搭建MongoDB
- MongoDB 副本集管理MongoDB
- 再看MongoDB副本集MongoDB
- 搭建MongoDB副本集MongoDB
- Mongodb3.0.5副本集搭建及spring和java連線副本集配置MongoDBSpringJava
- Mongodb副本集+分片叢集環境部署記錄MongoDB
- MongoDB 4.2副本集自動故障轉移(一主一副一仲裁)MongoDB
- 小丸子學Hadoop系列之——部署Hbase叢集Hadoop
- MongoDB - 副本集簡介MongoDB
- MongoDB副本集實踐MongoDB
- 如何配置 MongoDB 副本集MongoDB
- 【MongoDB】高可用方案之副本集(Replica Set)MongoDB
- MongoDB 3.2.7 for rhel6.4 副本集-分片叢集部署MongoDB
- 小丸子學Hadoop系列之——部署Hadoop叢集Hadoop
- 修改mongodb3.0副本集使用者密碼遇到的坑MongoDB密碼
- MongoDB 副本集原理及管理MongoDB
- MongoDB 副本集切換方法MongoDB
- 006.MongoDB副本集MongoDB
- MongoDB Replica Set 副本集實踐MongoDB
- mongodb簡單副本集實驗MongoDB
- 使用副本集搭建MongoDB叢集MongoDB
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- 2.MongoDB 4.2副本集環境基於時間點的恢復MongoDB
- 分散式文件儲存資料庫之MongoDB副本集分散式資料庫MongoDB
- MongoDB副本集學習(三):效能和優化相關MongoDB優化
- mongodb副本集新增刪除節點MongoDB