【Mongodb】如何建立mongodb的replica set
Replica sets 在主從複製上做的擴充套件,增加了故障自動切換和自動修復成員節點。下面從技術上介紹如何搭建mongodb的replica set (個人覺得,搭建mongodb本身沒有多少乾貨,重要是如何災難規劃)
1 建立複製叢集節點的資料存放目錄
mkdir -p /opt/mongodata/r1
mkdir -p /opt/mongodata/r2
mkdir -p /opt/mongodata/r3
2 在三個視窗分別執行如下命令:
./mongod --dbpath /opt/mongodata/r1 --port 27018 --rest --replSet myset
./mongod --dbpath /opt/mongodata/r2 --port 27019 --rest --replSet myset
./mongod --dbpath /opt/mongodata/r3 --port 27020 --rest --replSet myset
3 在第四個視窗執行如下命令:
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27018 init.js
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27018/test
init.js 內容如下:
[mongodb@rac4 bin]$ cat init.js
rs.initiate({
_id : "myset",
members : [
{_id : 0, host : "10.250.7.220:27018"},
{_id : 1, host : "10.250.7.220:27019"},
{_id : 2, host : "10.250.7.220:27020"}
]
})
啟動3個服務節點,從log日誌可以看到,三個節點相互協商,選擇埠為27018的一個節點作為Primary,另外兩個自動作為Secondary節點。
Mon Oct 31 20:27:53 [conn2] replSet info saving a newer config version to local.system.replset
Mon Oct 31 20:27:53 [conn2] replSet saveConfigLocally done
Mon Oct 31 20:27:53 [conn2] replSet replSetInitiate config now saved locally. Should come online in about a minute.
Mon Oct 31 20:27:53 [conn2] command admin.$cmd command: { replSetInitiate: { _id: "myset", members: [ { _id: 0.0, host: "10.250.7.220:27018" }, { _id: 1.0, host: "10.250.7.220:27019" }, { _id: 2.0, host: "10.250.7.220:27020" } ] } } ntoreturn:1 reslen:112 12095ms
Mon Oct 31 20:27:53 [conn2] end connection 127.0.0.1:15252
Mon Oct 31 20:27:53 [rsStart] replSet STARTUP2
Mon Oct 31 20:27:53 [rsHealthPoll] replSet info 10.250.7.220:27019 is down (or slow to respond): still initializing
Mon Oct 31 20:27:53 [rsHealthPoll] replSet member 10.250.7.220:27019 is now in state DOWN
Mon Oct 31 20:27:53 [rsHealthPoll] replSet info 10.250.7.220:27020 is down (or slow to respond): still initializing
Mon Oct 31 20:27:53 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state DOWN
Mon Oct 31 20:27:53 [rsSync] replSet SECONDARY
Mon Oct 31 20:27:55 [initandlisten] connection accepted from 10.250.7.220:44134 #3
Mon Oct 31 20:27:59 [rsHealthPoll] replSet info member 10.250.7.220:27019 is up
Mon Oct 31 20:27:59 [rsHealthPoll] replSet member 10.250.7.220:27019 is now in state STARTUP2
Mon Oct 31 20:27:59 [rsMgr] not electing self, 10.250.7.220:27019 would veto
Mon Oct 31 20:28:01 [initandlisten] connection accepted from 10.250.7.220:44137 #4
Mon Oct 31 20:28:05 [rsMgr] replSet info electSelf 0
Mon Oct 31 20:28:05 [rsMgr] replSet PRIMARY
Mon Oct 31 20:28:07 [rsHealthPoll] replSet member 10.250.7.220:27019 is now in state RECOVERING
Mon Oct 31 20:28:10 [initandlisten] connection accepted from 10.250.7.220:44141 #5
Mon Oct 31 20:28:10 [initandlisten] connection accepted from 10.250.7.220:44142 #6
Mon Oct 31 20:28:11 [slaveTracking] build index local.slaves { _id: 1 }
Mon Oct 31 20:28:11 [slaveTracking] build index done 0 records 0.001 secs
Mon Oct 31 20:28:13 [rsHealthPoll] replSet info member 10.250.7.220:27020 is up
Mon Oct 31 20:28:13 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state STARTUP2
Mon Oct 31 20:28:14 [conn6] end connection 10.250.7.220:44142
Mon Oct 31 20:28:14 [conn5] end connection 10.250.7.220:44141
Mon Oct 31 20:28:15 [initandlisten] connection accepted from 10.250.7.220:44144 #7
Mon Oct 31 20:28:15 [rsHealthPoll] replSet member 10.250.7.220:27019 is now in state SECONDARY
Mon Oct 31 20:28:15 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state RECOVERING
Mon Oct 31 20:28:28 [initandlisten] connection accepted from 127.0.0.1:59232 #8
從客戶端進入資料庫:
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27018
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27018/test
PRIMARY> rs.status() --檢視replica set狀態的命令
{
"set" : "myset",
"date" : ISODate("2011-10-31T12:29:17Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "10.250.7.220:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"optime" : {
"t" : 1320064073000,
"i" : 1
},
"optimeDate" : ISODate("2011-10-31T12:27:53Z"),
"self" : true
},
{
"_id" : 1,
"name" : "10.250.7.220:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 78,
"optime" : {
"t" : 1320064073000,
"i" : 1
},
"optimeDate" : ISODate("2011-10-31T12:27:53Z"),
"lastHeartbeat" : ISODate("2011-10-31T12:29:16Z"),
"pingMs" : 0
},
{
"_id" : 2,
"name" : "10.250.7.220:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 64,
"optime" : {
"t" : 1320064073000,
"i" : 1
},
"optimeDate" : ISODate("2011-10-31T12:27:53Z"),
"lastHeartbeat" : ISODate("2011-10-31T12:29:16Z"),
"pingMs" : 1
}
],
"ok" : 1
}
狀態中關鍵資料位
state:1表示該host是當前可以進行讀寫,2:不能讀寫
health:1表示該host目前是正常的,0:異常
下面會進行從庫的讀測試,會出現error: { "$err" : "not master and slaveok=false", "code" : 13435 }
PRIMARY> rs.conf() --檢視replica set配置的命令
{
"_id" : "myset",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "10.250.7.220:27018"
},
{
"_id" : 1,
"host" : "10.250.7.220:27019"
},
{
"_id" : 2,
"host" : "10.250.7.220:27020"
}
]
}
PRIMARY> db.isMaster();--檢視是否是主庫的命令,當然可以從提示符中看出primary
{
"setName" : "myset",
"ismaster" : true, ##是primary
"secondary" : false,
"hosts" : [
"10.250.7.220:27018",
"10.250.7.220:27020",
"10.250.7.220:27019"
],
"primary" : "10.250.7.220:27018",
"me" : "10.250.7.220:27018",
"maxBsonObjectSize" : 16777216,
"ok" : 1
}
分別登入其他兩個mongodb 服務:
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27019
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27019/test
SECONDARY>
SECONDARY>
SECONDARY> db.isMaster();
{
"setName" : "myset",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"10.250.7.220:27019",
"10.250.7.220:27020",
"10.250.7.220:27018"
],
"primary" : "10.250.7.220:27018",
"me" : "10.250.7.220:27019",
"maxBsonObjectSize" : 16777216,
"ok" : 1
}
SECONDARY>
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27020
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27020/test
SECONDARY>
SECONDARY> db.isMaster();
{
"setName" : "myset",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"10.250.7.220:27020",
"10.250.7.220:27019",
"10.250.7.220:27018"
],
"primary" : "10.250.7.220:27018",
"me" : "10.250.7.220:27020",
"maxBsonObjectSize" : 16777216,
"ok" : 1
}
至此搭建成功。。
對主庫進行寫操作,從庫檢視:
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27020
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27020/test
PRIMARY> use test
switched to db test
PRIMARY>
PRIMARY> db.yql.insert({val:"this is a message on 27020 primary !"});
PRIMARY>
主庫日誌:
Mon Oct 31 21:03:46 [FileAllocator] allocating new datafile /opt/mongodata/r3/test.ns, filling with zeroes...
Mon Oct 31 21:03:46 [FileAllocator] done allocating datafile /opt/mongodata/r3/test.ns, size: 16MB, took 0.256 secs
Mon Oct 31 21:03:46 [FileAllocator] allocating new datafile /opt/mongodata/r3/test.0, filling with zeroes...
Mon Oct 31 21:03:48 [clientcursormon] mem (MB) res:35 virt:2726 mapped:1248
Mon Oct 31 21:03:50 [FileAllocator] done allocating datafile /opt/mongodata/r3/test.0, size: 64MB, took 4.488 secs
Mon Oct 31 21:03:50 [conn6] build index test.yql { _id: 1 }
Mon Oct 31 21:03:50 [conn6] build index done 0 records 0 secs
Mon Oct 31 21:03:50 [conn6] insert test.yql 4759ms
Mon Oct 31 21:03:50 [FileAllocator] allocating new datafile /opt/mongodata/r3/test.1, filling with zeroes...
Mon Oct 31 21:03:51 [conn8] getmore local.oplog.rs query: { ts: { $gte: new Date(5669632022159556609) } } cursorid:6257712144272734285 nreturned:1 reslen:146 5031ms
Mon Oct 31 21:03:51 [conn5] getmore local.oplog.rs query: { ts: { $gte: new Date(5669632022159556609) } } cursorid:423878080662643430 nreturned:1 reslen:146 5631ms
Mon Oct 31 21:03:54 [FileAllocator] done allocating datafile /opt/mongodata/r3/test.1, size: 128MB, took 3.818 secs
從庫日誌,可以看出 從庫從主庫應用日誌,複製資料檔案的過程。
Mon Oct 31 20:49:27 [clientcursormon] mem (MB) res:19 virt:2693 mapped:1232
Mon Oct 31 20:54:27 [clientcursormon] mem (MB) res:19 virt:2693 mapped:1232
Mon Oct 31 20:59:27 [clientcursormon] mem (MB) res:19 virt:2693 mapped:1232
Mon Oct 31 21:03:51 [FileAllocator] allocating new datafile /opt/mongodata/r2/test.ns, filling with zeroes...
Mon Oct 31 21:03:54 [FileAllocator] done allocating datafile /opt/mongodata/r2/test.ns, size: 16MB, took 3.396 secs
Mon Oct 31 21:03:54 [FileAllocator] allocating new datafile /opt/mongodata/r2/test.0, filling with zeroes...
Mon Oct 31 21:04:00 [FileAllocator] done allocating datafile /opt/mongodata/r2/test.0, size: 64MB, took 5.79 secs
Mon Oct 31 21:04:00 [rsSync] build index test.yql { _id: 1 }
Mon Oct 31 21:04:00 [rsSync] build index done 0 records 0 secs
Mon Oct 31 21:04:00 [FileAllocator] allocating new datafile /opt/mongodata/r2/test.1, filling with zeroes...
Mon Oct 31 21:04:03 [FileAllocator] done allocating datafile /opt/mongodata/r2/test.1, size: 128MB, took 2.965 secs
Mon Oct 31 21:04:37 [clientcursormon] mem (MB) res:17 virt:2853 mapped:1312
Mon Oct 31 21:04:41 [conn6] end connection 127.0.0.1:44672
如前面介紹rs.status()時所說,state:1表示該host是當前可以進行讀寫,2:不能讀寫
{
"_id" : 1,
"name" : "10.250.7.220:27019",
"health" : 1,
"state" : 2, -- 從庫的state為 2 ,此時是不可讀寫的。
"stateStr" : "SECONDARY",
"uptime" : 78,
"optime" : {
"t" : 1320064073000,
"i" : 1
},
在從庫進行讀操作,會報錯。
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27019
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27019/test
SECONDARY> use test
switched to db test
SECONDARY> db.yql.find();
error: { "$err" : "not master and slaveok=false", "code" : 13435 }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-710004/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mongodb 3.0 replica set 配置MongoDB
- [MONGODB]: WHEN ARBITER REQUIRED FOR REPLICA SETMongoDBUI
- 【Mongodb】 Replica set 的讀寫分離MongoDB
- MongoDB Replica Set 副本集實踐MongoDB
- MongoDB搭建Replica Set複製集MongoDB
- 【Mongodb】Replica Set 的選舉策略之三MongoDB
- 【Mongodb】 Replica set 的 選舉策略之二MongoDB
- 【Mongodb】 Replica set 的選舉策略之一MongoDB
- docker 下部署mongodb Replica Set 叢集DockerMongoDB
- mongodb replica set 和 nodejs中使用mongoose連線replicaMongoDBNodeJS
- mongodb複製集(replica set)搭建及管理MongoDB
- 【MongoDB】高可用方案之副本集(Replica Set)MongoDB
- 【Mongodb】 replica set 新增和刪除節點。MongoDB
- MongoDB系列-解決面試中可能遇到的MongoDB複製集(replica set)問題MongoDB面試
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- 【Mongodb】 replica set 兩種新增節點方法的日誌分析MongoDB
- 小丸子學MongoDB系列之——部署Replica Set+Sharded ClusterMongoDB
- mongodb replica sets 測試MongoDB
- MongoDB 複製集模式Replica SetsMongoDB模式
- mongodb叢集shard_replica的搭建方法MongoDB
- Simple Automated Backups for MongoDB Replica SetsMongoDB
- mongodb 如何建立資料庫MongoDB資料庫
- MongoDB系列二:Replica Sets安裝與配置MongoDB
- mongodb資料庫如何建立索引?MongoDB資料庫索引
- MongoDB如何在後臺建立索引MongoDB索引
- mongodb複製集(replica sets)+分片(sharding)環境搭建MongoDB
- MongoDB(3)——Index的建立流程1MongoDBIndex
- 【mongodb】mongodb的安裝MongoDB
- MongoDB 3.0 使用者建立MongoDB
- MongoDB系列一(索引及C#如何操作MongoDB)MongoDB索引C#
- mongodb 分片叢集建立分片集合MongoDB
- MongoDB系列三:Replica Sets在生產環境中安裝配置的注意事項MongoDB
- 利用Mongodb的複製集搭建高可用分片,Replica Sets + Sharding的搭建過程MongoDB
- BIRT 如何連線 MongoDBMongoDB
- mongodb 如何檢視索引MongoDB索引
- mongodb如何更改埠號MongoDB
- mongodb如何做主從?MongoDB
- 如何配置 MongoDB 副本集MongoDB