mongodb 3.0 replica set 配置
mongodb:mongodb 3.0.3
host:
ct6601 192.108.56.117
ct6602 192.108.56.119
ct6604 192.108.56.120
mongodb primary:192.108.56.117:27017
mongodb secondary:192.108.56.119:27017 , 192.108.56.120:27017
一.安裝mongodb
#在ct6601,ct6602,ct6604上執行以下
#mongodb.tgz檔案放在/root下
[root@ct6601 ~]# ll mongo*
-rwxr--r-- 1 root root 50328650 Jun 4 2015 mongodb-linux-x86_64-rhel62-3.0.3.tgz
#解壓mongodb.tgz
[root@ct6601 ~]# tar -xzvf mongodb-linux-x86_64-rhel62-3.0.3.tgz
#移動解壓後的檔案到/usr/local/mongodb
[root@ct6601 ~]# mv mongodb-linux-x86_64-rhel62-3.0.3 /usr/local/mongodb
#設定環境變數
[root@ct6601 ~]# echo "export PATH=\$PATH:/usr/local/mongodb/bin" >>.bash_profile
[root@ct6601 ~]# . /root/.bash_profile
#建立資料目錄
[root@ct6601 ~]# mkdir /usr/local/mongodb/data
二.配置replica set
#在ct6601上執行以下
[root@ct6601 ~]# mongo 192.108.56.117:27017
> conf = { _id:"db1", members:[
{_id:0,host:"192.108.56.117:27017"},
{_id:1,host:"192.108.56.119:27017"},
{_id:2,host:"192.108.56.120:27017"}
]
}
輸出:
{
"_id" : "db1",
"members" : [
{
"_id" : 0,
"host" : "192.108.56.117:27017"
},
{
"_id" : 1,
"host" : "192.108.56.119:27017"
},
{
"_id" : 2,
"host" : "192.108.56.120:27017"
}
]
}
> rs.initiate(conf)
輸出:
{ "ok" : 1 }
#檢視配置結果
db1:OTHER> rs.status()
輸出:
{
"set" : "db1",
"date" : ISODate("2015-12-16T04:05:20.195Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.108.56.117:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 233,
"optime" : Timestamp(1450238666, 1),
"optimeDate" : ISODate("2015-12-16T04:04:26Z"),
"electionTime" : Timestamp(1450238670, 1),
"electionDate" : ISODate("2015-12-16T04:04:30Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.108.56.119:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 53,
"optime" : Timestamp(1450238666, 1),
"optimeDate" : ISODate("2015-12-16T04:04:26Z"),
"lastHeartbeat" : ISODate("2015-12-16T04:05:18.995Z"),
"lastHeartbeatRecv" : ISODate("2015-12-16T04:05:18.994Z"),
"pingMs" : 6,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.108.56.120:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 53,
"optime" : Timestamp(1450238666, 1),
"optimeDate" : ISODate("2015-12-16T04:04:26Z"),
"lastHeartbeat" : ISODate("2015-12-16T04:05:18.975Z"),
"lastHeartbeatRecv" : ISODate("2015-12-16T04:05:18.973Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 1
}
],
"ok" : 1
}
三.測試
#測試replica set的replication功能
[root@ct6601 ~]# mongo --port 27017
db1:PRIMARY> use test
輸出:switched to db test
db1:PRIMARY> db.tb01.insert({"name":"selectshen"})
輸出:WriteResult({ "nInserted" : 1 })
db1:PRIMARY> db.tb01.find()
輸出:{ "_id" : ObjectId("5670e3e22d45179620bb7a97"), "name" : "selectshen" }
db1:PRIMARY> exit
[root@ct6601 ~]# mongo 192.108.56.119:27017
db1:SECONDARY> rs.slaveOk()
db1:SECONDARY> db.tb01.find()
輸出:{ "_id" : ObjectId("5670e3e22d45179620bb7a97"), "name" : "selectshen" }
db1:SECONDARY> exit
#測試replica set的failover功能
#關閉192.108.56.117:27017的mongodb
[root@ct6601 ~]# mongo --port 27017
db1:PRIMARY> use admin
db1:PRIMARY> db.shutdownServer()
> exit
#可以看到primary已經切換到192.108.56.120:27017
[root@ct6601 ~]# mongo 192.108.56.119:27017
db1:SECONDARY> rs.status()
輸出:
{
"set" : "db1",
"date" : ISODate("2015-12-16T04:15:28.079Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "192.108.56.117:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-12-16T04:15:27.614Z"),
"lastHeartbeatRecv" : ISODate("2015-12-16T04:13:35.281Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to 192.108.56.117:27017; couldn't connect to server 192.108.56.117:27017 (192.108.56.117), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.108.56.119:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 853,
"optime" : Timestamp(1450238947, 2),
"optimeDate" : ISODate("2015-12-16T04:09:07Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.108.56.120:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 677,
"optime" : Timestamp(1450238947, 2),
"optimeDate" : ISODate("2015-12-16T04:09:07Z"),
"lastHeartbeat" : ISODate("2015-12-16T04:15:27.472Z"),
"lastHeartbeatRecv" : ISODate("2015-12-16T04:15:27.471Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1450239217, 1),
"electionDate" : ISODate("2015-12-16T04:13:37Z"),
"configVersion" : 1
}
],
"ok" : 1
}
host:
ct6601 192.108.56.117
ct6602 192.108.56.119
ct6604 192.108.56.120
mongodb primary:192.108.56.117:27017
mongodb secondary:192.108.56.119:27017 , 192.108.56.120:27017
一.安裝mongodb
#在ct6601,ct6602,ct6604上執行以下
#mongodb.tgz檔案放在/root下
[root@ct6601 ~]# ll mongo*
-rwxr--r-- 1 root root 50328650 Jun 4 2015 mongodb-linux-x86_64-rhel62-3.0.3.tgz
#解壓mongodb.tgz
[root@ct6601 ~]# tar -xzvf mongodb-linux-x86_64-rhel62-3.0.3.tgz
#移動解壓後的檔案到/usr/local/mongodb
[root@ct6601 ~]# mv mongodb-linux-x86_64-rhel62-3.0.3 /usr/local/mongodb
#設定環境變數
[root@ct6601 ~]# echo "export PATH=\$PATH:/usr/local/mongodb/bin" >>.bash_profile
[root@ct6601 ~]# . /root/.bash_profile
#建立資料目錄
[root@ct6601 ~]# mkdir /usr/local/mongodb/data
二.配置replica set
#在ct6601上執行以下
[root@ct6601 ~]# mongo 192.108.56.117:27017
> conf = { _id:"db1", members:[
{_id:0,host:"192.108.56.117:27017"},
{_id:1,host:"192.108.56.119:27017"},
{_id:2,host:"192.108.56.120:27017"}
]
}
輸出:
{
"_id" : "db1",
"members" : [
{
"_id" : 0,
"host" : "192.108.56.117:27017"
},
{
"_id" : 1,
"host" : "192.108.56.119:27017"
},
{
"_id" : 2,
"host" : "192.108.56.120:27017"
}
]
}
> rs.initiate(conf)
輸出:
{ "ok" : 1 }
#檢視配置結果
db1:OTHER> rs.status()
輸出:
{
"set" : "db1",
"date" : ISODate("2015-12-16T04:05:20.195Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.108.56.117:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 233,
"optime" : Timestamp(1450238666, 1),
"optimeDate" : ISODate("2015-12-16T04:04:26Z"),
"electionTime" : Timestamp(1450238670, 1),
"electionDate" : ISODate("2015-12-16T04:04:30Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.108.56.119:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 53,
"optime" : Timestamp(1450238666, 1),
"optimeDate" : ISODate("2015-12-16T04:04:26Z"),
"lastHeartbeat" : ISODate("2015-12-16T04:05:18.995Z"),
"lastHeartbeatRecv" : ISODate("2015-12-16T04:05:18.994Z"),
"pingMs" : 6,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.108.56.120:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 53,
"optime" : Timestamp(1450238666, 1),
"optimeDate" : ISODate("2015-12-16T04:04:26Z"),
"lastHeartbeat" : ISODate("2015-12-16T04:05:18.975Z"),
"lastHeartbeatRecv" : ISODate("2015-12-16T04:05:18.973Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 1
}
],
"ok" : 1
}
三.測試
#測試replica set的replication功能
[root@ct6601 ~]# mongo --port 27017
db1:PRIMARY> use test
輸出:switched to db test
db1:PRIMARY> db.tb01.insert({"name":"selectshen"})
輸出:WriteResult({ "nInserted" : 1 })
db1:PRIMARY> db.tb01.find()
輸出:{ "_id" : ObjectId("5670e3e22d45179620bb7a97"), "name" : "selectshen" }
db1:PRIMARY> exit
[root@ct6601 ~]# mongo 192.108.56.119:27017
db1:SECONDARY> rs.slaveOk()
db1:SECONDARY> db.tb01.find()
輸出:{ "_id" : ObjectId("5670e3e22d45179620bb7a97"), "name" : "selectshen" }
db1:SECONDARY> exit
#測試replica set的failover功能
#關閉192.108.56.117:27017的mongodb
[root@ct6601 ~]# mongo --port 27017
db1:PRIMARY> use admin
db1:PRIMARY> db.shutdownServer()
> exit
#可以看到primary已經切換到192.108.56.120:27017
[root@ct6601 ~]# mongo 192.108.56.119:27017
db1:SECONDARY> rs.status()
輸出:
{
"set" : "db1",
"date" : ISODate("2015-12-16T04:15:28.079Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "192.108.56.117:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-12-16T04:15:27.614Z"),
"lastHeartbeatRecv" : ISODate("2015-12-16T04:13:35.281Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to 192.108.56.117:27017; couldn't connect to server 192.108.56.117:27017 (192.108.56.117), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.108.56.119:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 853,
"optime" : Timestamp(1450238947, 2),
"optimeDate" : ISODate("2015-12-16T04:09:07Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.108.56.120:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 677,
"optime" : Timestamp(1450238947, 2),
"optimeDate" : ISODate("2015-12-16T04:09:07Z"),
"lastHeartbeat" : ISODate("2015-12-16T04:15:27.472Z"),
"lastHeartbeatRecv" : ISODate("2015-12-16T04:15:27.471Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1450239217, 1),
"electionDate" : ISODate("2015-12-16T04:13:37Z"),
"configVersion" : 1
}
],
"ok" : 1
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28539951/viewspace-1873371/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Mongodb】如何建立mongodb的replica setMongoDB
- [MONGODB]: WHEN ARBITER REQUIRED FOR REPLICA SETMongoDBUI
- MongoDB Replica Set 副本集實踐MongoDB
- MongoDB搭建Replica Set複製集MongoDB
- mongodb replica set 和 nodejs中使用mongoose連線replicaMongoDBNodeJS
- docker 下部署mongodb Replica Set 叢集DockerMongoDB
- 【Mongodb】 Replica set 的讀寫分離MongoDB
- mongodb複製集(replica set)搭建及管理MongoDB
- 【MongoDB】高可用方案之副本集(Replica Set)MongoDB
- 【Mongodb】Replica Set 的選舉策略之三MongoDB
- 【Mongodb】 Replica set 的 選舉策略之二MongoDB
- 【Mongodb】 Replica set 的選舉策略之一MongoDB
- 【Mongodb】 replica set 新增和刪除節點。MongoDB
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- 小丸子學MongoDB系列之——部署Replica Set+Sharded ClusterMongoDB
- MongoDB系列二:Replica Sets安裝與配置MongoDB
- 【Mongodb】 replica set 兩種新增節點方法的日誌分析MongoDB
- MongoDB系列-解決面試中可能遇到的MongoDB複製集(replica set)問題MongoDB面試
- mongodb replica sets 測試MongoDB
- MongoDB 複製集模式Replica SetsMongoDB模式
- Simple Automated Backups for MongoDB Replica SetsMongoDB
- mongodb叢集shard_replica的搭建方法MongoDB
- MongoDB系列三:Replica Sets在生產環境中安裝配置的注意事項MongoDB
- mongodb複製集(replica sets)+分片(sharding)環境搭建MongoDB
- MongoDB 3.0新增特性一覽MongoDB
- MongoDB 3.0 使用者建立MongoDB
- MongoDB 3.0 正式版釋出!MongoDB
- mongoDB 3.0 安全許可權訪問MongoDB
- webpack3.0配置Web
- MongoDB安全配置MongoDB
- MongoDB 映象配置方法MongoDB
- Mongodb主從配置MongoDB
- 【mongodb安裝配置】MongoDB
- MongoDB3.0新版本幾點介紹MongoDB
- mongodb配置檔案常用配置項MongoDB
- 利用Mongodb的複製集搭建高可用分片,Replica Sets + Sharding的搭建過程MongoDB
- 搭建高可用MongoDB叢集(一):配置MongoDBMongoDB
- 【Django3.0】配置篇:全球化配置Django