糊一笑 2020-06-14
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>