一、啓兩個Mongo容器mongodb
docker run --name mongo1 -p 21117:27017 -d mongo --noprealloc --smallfiles --replSet rs1
docker run --name mongo2 -p 22117:27017 -d mongo --noprealloc --smallfiles --replSet rs1
也能夠在啓動容器的時候,自定義Mongo數據存放路徑,且將mongo數據掛載到本地docker
docker run --name mongo_rs1 -v ~/test/mongo_sr1:/mongodb -p 21117:27017 -d mongo mongod --logpath /mongodb/mongo.log --logappend --dbpath /mongodb --replSet rs1
docker run --name mongo_rs2 -v ~/test/mongo_sr2:/mongodb -p 22117:27017 -d mongo mongod --logpath /mongodb/mongo.log --logappend --dbpath /mongodb --replSet rs1
二、查看正在運行的容器數據庫
docker ps | grep mongo
三、查看mongo一、mongo2容器ipapp
docker inspect mongo1 | grep IPA
四、使用本機(宿主機)mongo客戶端鏈接到21117端口(即鏈接到mongo1容器)spa
mongo --port 21117
五、開始配置 複製集的成員信息3d
myconf = {"_id":"rs1","members":[{"_id":0,"host":"172.17.0.13:27017"},{"_id":1,"host":"172.17.0.14:27017"}]}
六、初始化同步複製集羣code
rs.initiate(myconf)
注意:在啓動容器時,--replSet rs1 這個值mongo1和mongo2要保持一致,不然在初始化同步時,會出錯。blog
七、查看當前複製集的節點信息ip
rs.isMaster()
能夠看出鏈接到的容器ip是172.17.0.13 也能夠看見誰是主節點ci
查看狀態
rs.status()
查看配置
rs.conf()
八、另外在本地再開一個終端 鏈接到第二個容器
mongo --port 22117
查看當前master
rs.isMaster()
能夠看到主節點的信息 和上個容器的信息一致
rs.status()
{ "set" : "rs1", "date" : ISODate("2018-07-07T01:53:14.330Z"), "myState" : 2, "term" : NumberLong(1), "syncingTo" : "172.17.0.13:27017", "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1530928387, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1530928387, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1530928387, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1530928387, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "172.17.0.13:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 310, "optime" : { "ts" : Timestamp(1530928387, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1530928387, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-07-07T01:53:07Z"), "optimeDurableDate" : ISODate("2018-07-07T01:53:07Z"), "lastHeartbeat" : ISODate("2018-07-07T01:53:12.960Z"), "lastHeartbeatRecv" : ISODate("2018-07-07T01:53:12.960Z"), "pingMs" : NumberLong(0), "electionTime" : Timestamp(1530928094, 1), "electionDate" : ISODate("2018-07-07T01:48:14Z"), "configVersion" : 1 }, { "_id" : 1, "name" : "172.17.0.14:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 366, "optime" : { "ts" : Timestamp(1530928387, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-07-07T01:53:07Z"), "syncingTo" : "172.17.0.13:27017", "configVersion" : 1, "self" : true } ], "ok" : 1, "operationTime" : Timestamp(1530928387, 1) }
九、若是在從節點此時查看數據庫會出錯 ,信息顯示不是主節點
show dbs;
設置從節點能夠讀
db.getMongo().setSlaveOk();
或者用
rs.slaveOk()
十、在主節點mongo1中插入數據
show dbs; use test; db.user1.insert({"name":"mongodb is user1"}) db.user2.insert({"name":"mongodb is user2"})
十一、在從節點mongo2中查看
show dbs; use test; show collections; db.user1.find() db.user2.find()
能夠看到在mongo1插入的數據,同步到了mongo2數據庫來了~
1三、此時若是在mongo2從節點中插入數據
db.user3.insert({"name":"mongodb is user3"})
能夠看到提示不是master主節點,沒法寫數據~
到此,就給你們介紹完了如何經過docker+mongo來實現主從同步的操做方法,喜歡的能夠關注公衆號哦~