First, the specific operation method
1. Start two MONGO containers
Docker run--name mongo1-p 21117:27017-d MONGO--noprealloc--smallfiles--replset rs1docker run--name mongo2-p 22117: 27017-d MONGO--noprealloc--smallfiles--replset rs1
2. Viewing a running container
Docker PS | grep MONGO
3. View Mongo1, Mongo2 container IP
Docker Inspect Mongo1 | grep IPA
4. Use the native (host) MONGO client to connect to port 21117 (that is, connect to the MONGO1 container)
MONGO--port 21117
5. Start configuring member information for replication sets
{"_id":"Rs1", "members": [{"_id": 0, "host": "172.17.0.13:27017"},{"_id":1, "Host": "172.17.0.14:27017"}]}
6. Initializing a synchronous replication cluster
Rs.initiate (myconf)
Note: When you start the container,--replset rs1 This value mongo1 and mongo2 to be consistent, otherwise an error occurs when the synchronization is initialized.
7. View node information for the current replica set
Rs.ismaster ()
You can see that the container IP you are connecting to is 172.17.0.13 and you can see who is the primary node
View status
Rs.status ()
View Configuration
Rs.conf ()
8. Another terminal is connected to the second container locally.
MONGO--port 22117
View Current Master
Rs.ismaster ()
You can see the information of the master node consistent with the information from the previous container
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 ( -), "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)}
9. If there is an error in viewing the database from the node at this point, the information display is not the primary node
Show DBS;
Settings from a node can be read
Db.getmongo (). Setslaveok ();
or with
Rs.slaveok ()
10. Inserting data into the master node Mongo1
Show Dbs;use Test;db.user1.insert ({"name":"MongoDB is user1 " }) Db.user2.insert ({"name":"MongoDB is user2" })
11. Viewing from Node Mongo2
show Dbs;use test;show collections;db.user1. Find () db.user2. Find ()
Can see the data inserted in the MONGO1, synchronized to the Mongo2 database came ~
13. If you insert data from a node at this point in Mongo2
Db.user3.insert ({"name":"MongoDB is User3"})
You can see that the prompt is not the master master node and cannot write data ~
To this, we have introduced how to achieve the master-slave synchronization through the Docker+mongo operation method, like to pay attention to the public number Oh ~
A master-Slave synchronization configuration method for Docker MONGO database