mongodb複製集搭建
環境
主機 埠4444
[root@db11 mongo]# hostname
db11
db11
[root@db11 mongo]# pwd
/opt/mongo
[root@db11 mongo]# ls -l
total 124
drwxr-xr-x. 2 root root 4096 Mar 10 19:37 bin
drwxr-xr-x. 3 root root 4096 Mar 12 16:27 data
-rw-r--r--. 1 root root 34520 Feb 28 01:43 GNU-AGPL-3.0
-rw-r--r--. 1 root root 104 Mar 11 09:52 mongo.conf
-rw-r--r--. 1 root root 37896 Mar 16 14:52 mongo.log
-rw-------. 1 root root 1602 Mar 16 14:51 nohup.out
-rw-r--r--. 1 root root 1359 Feb 28 01:43 README
-rw-r--r--. 1 root root 22660 Feb 28 01:43 THIRD-PARTY-NOTICES
/opt/mongo
[root@db11 mongo]# ls -l
total 124
drwxr-xr-x. 2 root root 4096 Mar 10 19:37 bin
drwxr-xr-x. 3 root root 4096 Mar 12 16:27 data
-rw-r--r--. 1 root root 34520 Feb 28 01:43 GNU-AGPL-3.0
-rw-r--r--. 1 root root 104 Mar 11 09:52 mongo.conf
-rw-r--r--. 1 root root 37896 Mar 16 14:52 mongo.log
-rw-------. 1 root root 1602 Mar 16 14:51 nohup.out
-rw-r--r--. 1 root root 1359 Feb 28 01:43 README
-rw-r--r--. 1 root root 22660 Feb 28 01:43 THIRD-PARTY-NOTICES
備機 埠從4000開始
兩個複製節點,一個arbiter
[root@db10 mongo]# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:C2:46:86
inet addr:1.1.1.10 Bcast:1.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fec2:4686/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:117884 errors:0 dropped:0 overruns:0 frame:0
TX packets:50708 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:162996837 (155.4 MiB) TX bytes:3730840 (3.5 MiB)
eth0:1 Link encap:Ethernet HWaddr 08:00:27:C2:46:86
inet addr:1.1.1.100 Bcast:1.1.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:2 Link encap:Ethernet HWaddr 08:00:27:C2:46:86
inet addr:1.1.1.101 Bcast:1.1.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:3 Link encap:Ethernet HWaddr 08:00:27:C2:46:86
inet addr:1.1.1.102 Bcast:1.1.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0 Link encap:Ethernet HWaddr 08:00:27:C2:46:86
inet addr:1.1.1.10 Bcast:1.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fec2:4686/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:117884 errors:0 dropped:0 overruns:0 frame:0
TX packets:50708 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:162996837 (155.4 MiB) TX bytes:3730840 (3.5 MiB)
eth0:1 Link encap:Ethernet HWaddr 08:00:27:C2:46:86
inet addr:1.1.1.100 Bcast:1.1.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:2 Link encap:Ethernet HWaddr 08:00:27:C2:46:86
inet addr:1.1.1.101 Bcast:1.1.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:3 Link encap:Ethernet HWaddr 08:00:27:C2:46:86
inet addr:1.1.1.102 Bcast:1.1.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[root@db10 mongo]# hostname
db10
[root@db10 mongo]# ls -l
total 28
drwxr-xr-x. 2 root root 4096 Mar 16 13:50 bin
drwxr-xr-x. 2 root root 4096 Mar 16 14:07 data1
drwxr-xr-x. 2 root root 4096 Mar 16 14:07 data2
drwxr-xr-x. 2 root root 4096 Mar 16 13:50 dir
-rw-r--r--. 1 root root 104 Mar 16 13:50 mongo1.conf
-rw-r--r--. 1 root root 104 Mar 16 14:13 mongo2.conf
-rw-r--r--. 1 root root 104 Mar 16 14:14 mongo_arbiter.conf
db10
[root@db10 mongo]# ls -l
total 28
drwxr-xr-x. 2 root root 4096 Mar 16 13:50 bin
drwxr-xr-x. 2 root root 4096 Mar 16 14:07 data1
drwxr-xr-x. 2 root root 4096 Mar 16 14:07 data2
drwxr-xr-x. 2 root root 4096 Mar 16 13:50 dir
-rw-r--r--. 1 root root 104 Mar 16 13:50 mongo1.conf
-rw-r--r--. 1 root root 104 Mar 16 14:13 mongo2.conf
-rw-r--r--. 1 root root 104 Mar 16 14:14 mongo_arbiter.conf
步驟
1 在每個成員的配置檔案裡新增replica set name,並且啟動所有成員。
replSet = rs0
arbiter成員設定
replSet = rs0
journal = false
2 連線主庫,初始化複製集。
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "db11:4444",
"ok" : 1
}
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "db11:4444",
"ok" : 1
}
rs0:OTHER>
3 檢視當前replica配置。
執行後,提示符程式設計“rs0:PRIMARY>”
rs0:OTHER> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "db11:4444",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "db11:4444",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
rs0:PRIMARY>
4 新增兩個備機和一個arbiter
rs0:PRIMARY> rs.add('1.1.1.101:4001');
{ "ok" : 1 }
rs0:PRIMARY> rs.add('1.1.1.102:4002');
{ "ok" : 1 }
{ "ok" : 1 }
rs0:PRIMARY> rs.add('1.1.1.102:4002');
{ "ok" : 1 }
rs0:PRIMARY> rs.addArb('1.1.1.100:4000')
{ "ok" : 1 }
{ "ok" : 1 }
5 讓備庫就緒
(以後每次啟動備庫都需要執行)
rs0:SECONDARY> rs.slaveOk()
6 檢視配置結果
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 5,
"members" : [
{
"_id" : 0,
"host" : "db11:4444",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "1.1.1.101:4001",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 2,
"host" : "1.1.1.102:4002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 3,
"host" : "1.1.1.100:4000",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
{
"_id" : "rs0",
"version" : 5,
"members" : [
{
"_id" : 0,
"host" : "db11:4444",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "1.1.1.101:4001",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 2,
"host" : "1.1.1.102:4002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 3,
"host" : "1.1.1.100:4000",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
7 驗證
主庫
rs0:PRIMARY> use test
switched to db test
rs0:PRIMARY> db.createCollection('obj');
{ "ok" : 1 }
rs0:PRIMARY> db.obj.insert({name: "a", value: 100});
WriteResult({ "nInserted" : 1 })
switched to db test
rs0:PRIMARY> db.createCollection('obj');
{ "ok" : 1 }
rs0:PRIMARY> db.obj.insert({name: "a", value: 100});
WriteResult({ "nInserted" : 1 })
備庫
rs0:SECONDARY> use test
switched to db test
rs0:SECONDARY> db.obj.findOne();
{
"_id" : ObjectId("55068b6b391bb83a8c51b140"),
"name" : "a",
"value" : 100
}
switched to db test
rs0:SECONDARY> db.obj.findOne();
{
"_id" : ObjectId("55068b6b391bb83a8c51b140"),
"name" : "a",
"value" : 100
}
備庫整體是隻讀的,建其它資料庫也不行
rs0:SECONDARY> use ls
switched to db ls
rs0:SECONDARY> db.createCollection('xx')
{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
switched to db ls
rs0:SECONDARY> db.createCollection('xx')
{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26239116/viewspace-1485427/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MongoDB 搭建複製集MongoDB
- 【Mongodb】 可複製集搭建MongoDB
- MongoDB搭建Replica Set複製集MongoDB
- MongoDB學習4:MongoDB複製集機制和原理,搭建複製集MongoDB
- mongodb複製集(replica set)搭建及管理MongoDB
- mongodb6.0.13 搭建複製集PSAMongoDB
- mongodb複製集MongoDB
- MongoDB複製集原理MongoDB
- 【Mongodb】分片加複製集MongoDB
- MongoDB 搭建可複製群集MongoDB
- mongodb複製集(replica sets)+分片(sharding)環境搭建MongoDB
- 【Mongodb】往分片複製集新增複製成員MongoDB
- MongoDB學習之複製集MongoDB
- MongoDB 複製集模式Replica SetsMongoDB模式
- 【MongoDB】windows平臺搭建Mongo資料庫複製集(相似叢集)(一)MongoDBWindows資料庫
- MongoDB複製集資料同步流程MongoDB
- mongoDB研究筆記:複製集概述MongoDB筆記
- 利用Mongodb的複製集搭建高可用分片,Replica Sets + Sharding的搭建過程MongoDB
- Replica sets複製集的搭建
- 02 . MongoDB複製集,分片集,備份與恢復MongoDB
- MongoDB原理:複製集狀態同步機制MongoDB
- MongoDB日常運維-03主從複製搭建MongoDB運維
- 和麵試官這樣吹MongoDB 複製集!MongoDB
- Mongodb 配置複製集,並啟用安全校驗MongoDB
- 【Mongodb】分片複製集環境新增新的分片MongoDB
- mongodb複製+分片MongoDB
- Redis搭建主從複製、哨兵叢集Redis
- Mongodb主從複製/ 副本集/分片叢集介紹MongoDB
- mongoDB研究筆記:複製集故障轉移機制MongoDB筆記
- mongoDB研究筆記:複製集資料同步機制MongoDB筆記
- MongoDB 複製機制MongoDB
- MongoDB主從複製MongoDB
- 【mongodb】mongodb 實現主從複製MongoDB
- MongoDB 分片叢集搭建MongoDB
- Docker 搭建叢集 MongoDBDockerMongoDB
- 搭建MongoDB分片叢集MongoDB
- MongoDB系列-解決面試中可能遇到的MongoDB複製集(replica set)問題MongoDB面試
- Docker Compose搭建MySQL主從複製叢集DockerMySql