docker——mongodb副本集

docker mongodb 副本集

 

 

docker network create net-mongoset

docker network ls

 

docker run -d -p27001:27017 --name docker_mongo1 --net net-mongoset  mongo:3.4.24 --replSet yinSet

docker run -d -p27002:27017 --name docker_mongo2 --net net-mongoset  mongo:3.4.24 --replSet yinSet

docker run -d -p27003:27017 --name docker_mongo3 --net net-mongoset  mongo:3.4.24 --replSet yinSet

 

mongo --port 27001

 

>rs.status()

 

> rs.initiate()

{

"info2" : "no configuration specified. Using a default configuration for the set",

"me" : "218f50912c40:27017",

"ok" : 1

}

 

>rs.add("docker_mongo2:27017")

>rs.add("docker_mongo3:27017")

 

>rs.status()

 

主节点插入数据,查看副节点同步

mongo --port 27001

yinSet:PRIMARY> use mytest

switched to db mytest

yinSet:PRIMARY> 

yinSet:PRIMARY> db.mytest.insert({"name":"axboy"})

WriteResult({ "nInserted" : 1 })

yinSet:PRIMARY> show dbs;

yinSet:PRIMARY> use mytest

switched to db mytest

 

yinSet:PRIMARY> db2 = (new Mongo('127.0.0.1:27002')).getDB('mytest')

mytest

yinSet:PRIMARY> db2.mytest.find()

Error: error: {

"ok" : 0,

"errmsg" : "not master and slaveOk=false",

"code" : 13435,

"codeName" : "NotMasterNoSlaveOk"

}

yinSet:PRIMARY> db2.setSlaveOk()

yinSet:PRIMARY> 

yinSet:PRIMARY> db2.mytest.find()

{ "_id" : ObjectId("5ee5aff0d779793f66bc376f"), "name" : "axboy" }

yinSet:PRIMARY> 

yinSet:PRIMARY> 

 

 

 

 

原因: mongodb默认是从主节点读写数据,副本节点上不允许读,设置副本节点可读。

db.getMongo().setSlaveOk();

注意:这条命令要在副节点上运行

然后就可以查询复制过来的数据了

 

 

 初始化副本集

 

config = {
  _id : "config_repl",
  members : [
    {_id : 0, host : "172.31.140.157:27101" },
    {_id : 1, host : "172.31.140.158:27101" },
    {_id : 2, host : "172.31.140.159:27101" }
  ]
}

 配置3节点副本集


config = {
_id : "shard1_repl",
members : [
{_id : 0, host : "172.31.140.157:27201" },
{_id : 1, host : "172.31.140.158:27201" },
{_id : 2, host : "172.31.140.159:27201" }
]
}

为mongos添加副本集类型的分片

sh.addShard("shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201")

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/xingchong/p/13124426.html