Writing in the front

What will you get out of this article?

  • Docker bridge built
  • Build mongodb cluster based on Docker

This paper establishes the environment:

  • The operating systemMacOs ( 10.14.5)
  • Docker
  • MongoMirroring (version4.4.3

Matters needing attention:

  1. Understand the network driving mode of Docker, which involves communication between containers;
  2. Note The operation permission of the current system user on the database file.

start

Step1: start a docker bridge

Create a Network Bridge using Docker

docker network new test-net
Copy the code

Step2: Start three Docker containers

Docker-compose (docker-compose is more convenient, not introduced here, why? Because I haven’t used it yet, I’ll try it later.

$ docker run --rm --network test-net  --name mongo1 -d -v /data/mongo1/db:/data/db -p 27021:27017 mongo:latest --replSet replSet1
$ docker run --rm --network test-net  --name mongo2 -d -v /data/mongo2/db:/data/db -p 27022:27017 mongo:latest --replSet replSet1
$ docker run --rm --network test-net  --name mongo3 -d -v /data/mongo3/db:/data/db -p 27023:27017 mongo:latest --replSet replSet1
Copy the code

-v parameter: mount the docker internal /data/db directory in the host /data/mongo4/db directory to prevent data loss when the container restarts

–network: add the docker container to the test-net bridge;

–replSet: Name the replica set to replSet1;

Step3

3-1. Go to mongo1 client

$ docker exec -it mongo1 mongo
Copy the code

3-2. Start a replica set (i.e. Mongo1 as main node)

$ rs.initiate()
Copy the code

3-3. Add a new member mongo2 to the replica set

$ rs.add('mongo2:27017')
Copy the code

Note: Since this article uses mongonDB service started by Docker, you need to understand the communication mode between Docker containers. As the previous article also reminds you, Because the three Mongo services we started are in the same bridge (test-net), this bridge is customized by us. In other words, if we started the container without specifying –network test-net and used the Docker default bridge, then add a new member using rs.add(‘mongo2:27017’) and the response would time out. This point oneself in inside toss about quite long time, the net similar article did not explain, perhaps they think I understand ~, the curse of knowledge!

3-4. Add a new member mongo3 to the replica set

$ rs.add('mongo3:27017')
Copy the code

3-4. View the status of the replica set

$ rs.status()

"members": [{"_id": 0."name" : "132895066d71:27017"."health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY". }, {"_id" : 1,
			"name" : "mongo2:27017"."health" : 1,
			"state": 2."stateStr" : "SECONDARY"."uptime": 195,... }, {"_id": 2."name" : "mongo3:27017"."health" : 1,
			"state": 2."stateStr" : "SECONDARY". }].Copy the code

At this time the cluster has been created, one boss (mongo1), two little cute (🇲mongo2, mongo3), next we verify!


Step4 verify the cluster operation

Mongo1 add a single piece of data to the master node:

replSet1:PRIMARY> db.foods.insert({"name":"Tomato"})
WriteResult({ "nInserted": 1})Copy the code

Mongo2 mongo3 check whether data is synchronized from node:

ReplSet1 :SECONDARY> rs.secondaryok () // Allow the read operation to be performed on the SECONDARY node replSet1:SECONDARY> db.foods.find()
{ "_id" : ObjectId("605ca97350221c02825ceaa7"), "name" : "Tomato" }
Copy the code

As shown above, data has been synchronized from the primary node to the secondary node.

conclusion

The above is the local simulation cluster building tutorial, cluster detailed operation and configuration is not the focus of this article. There are bound to be questions along the way. Because it needs to integrate a variety of suitable environment, an environment error, will toss for a long time. Be patient and learn!