设置network
docker network create my-mongo-cluster
docker network ls
构建副本集
docker run \
-p 30001:27017 \
--name mongo1 \
--net my-mongo-cluster \
daocloud.io/library/mongo mongod --replSet my-mongo-set
docker run \
-p 30002:27017 \
--name mongo2 \
--net my-mongo-cluster \
daocloud.io/library/mongo mongod --replSet my-mongo-set
docker run \
-p 30003:27017 \
--name mongo3 \
--net my-mongo-cluster \
daocloud.io/library/mongo mongod --replSet my-mongo-set
初始化配置
docker exec -it mongo1 mongo
db = (new Mongo('localhost:27017')).getDB('test')
config = {
"_id" : "my-mongo-set",
"members" : [
{
"_id" : 0,
"host" : "mongo1:27017"
},
{
"_id" : 1,
"host" : "mongo2:27017"
},
{
"_id" : 2,
"host" : "mongo3:27017"
}
]
}
rs.initiate(config)
查看副本集状态
rs.status()
输出
my-mongo-set:PRIMARY> rs.status()
{
"set" : "my-mongo-set",
"date" : ISODate("2017-03-31T17:09:24.368Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1490980160, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1490980160, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1490980160, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "mongo1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 437,
"optime" : {
"ts" : Timestamp(1490980160, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-03-31T17:09:20Z"),
"electionTime" : Timestamp(1490979879, 1),
"electionDate" : ISODate("2017-03-31T17:04:39Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "mongo2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 296,
"optime" : {
"ts" : Timestamp(1490980160, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1490980160, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-03-31T17:09:20Z"),
"optimeDurableDate" : ISODate("2017-03-31T17:09:20Z"),
"lastHeartbeat" : ISODate("2017-03-31T17:09:23.936Z"),
"lastHeartbeatRecv" : ISODate("2017-03-31T17:09:23.625Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mongo1:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "mongo3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 296,
"optime" : {
"ts" : Timestamp(1490980160, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1490980160, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-03-31T17:09:20Z"),
"optimeDurableDate" : ISODate("2017-03-31T17:09:20Z"),
"lastHeartbeat" : ISODate("2017-03-31T17:09:23.936Z"),
"lastHeartbeatRecv" : ISODate("2017-03-31T17:09:23.624Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mongo1:27017",
"configVersion" : 1
}
],
"ok" : 1
}
查看同步延迟
rs.printSlaveReplicationInfo()
备份
mongodump --dbpath /var/lib/mongodb --out /var/lib/mongodb.backup
mongorestore --dbpath /var/lib/tokumx /var/lib/mongodb.backup
关闭replica set
use admin
db.shutdownServer({timeoutSecs : 15})
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。