Create a MongoDB Replica set with 3 members on local machine with Docker
- MongoDB Replica Set are inspired and based on the official tutorial
- We assume you already knew how to use Docker
- We'll use the following DNS hostnames
www.r0.mongodb.my www.r1.mongodb.my www.r2.mongodb.my
- Windows 10 PC
- Working installation of Docker
- Working installation of MongoDB for windows
- Open
Notepad
(or any text editor) as Administrator - Open the file
C:\Windows\System32\drivers\etc\hosts
- Append the following to
hosts
file127.0.0.1 www.r0.mongodb.my 127.0.0.1 www.r1.mongodb.my 127.0.0.1 www.r2.mongodb.my
- Save
Now, all calls to address www.r0.mongodb.my
, www.r1.mongodb.my
, and www.r2.mongodb.my
will be mapped to machine's localhost.
We'll name our Docker containers as follow
mongo-www-r0-mongodb-my
mongo-www-r1-mongodb-my
mongo-www-r2-mongodb-my
Then we'll map the container ports to the host machine ports as follow
mongo-www-r0-mongodb-my:27017
->28015
mongo-www-r1-mongodb-my:27017
->28016
mongo-www-r2-mongodb-my:27017
->28017
mongo-www-r0-mongodb-my
$ docker exec -ti $(docker run -ti -d -p 28015:27017 --name mongo-www-r0-mongodb-my mongo:latest bash) bash # create and ssh to container
root@xxxxxx:~# mongod --replSet "myReplica" --bind_ip_all --fork --logpath /var/log/mongod.log # start mongo server with configuration
root@xxxxxx:~# exit # exit ssh
--replSet "myReplica"
- Configure replica with name of myReplica
--bind_ip_all
- Allow all ipv4 client connections
--fork --logpath /var/log/mongod.log
- Start mongodb process in background with specified path for logs
mongo-www-r1-mongodb-my
$ docker exec -ti $(docker run -ti -d -p 28016:27017 --name mongo-www-r1-mongodb-my mongo:latest bash) bash # create and ssh to container
root@xxxxxx:~# mongod --replSet "myReplica" --bind_ip_all --fork --logpath /var/log/mongod.log # start mongo server with configuration
root@xxxxxx:~# exit # exit ssh
mongo-www-r2-mongodb-my
$ docker exec -ti $(docker run -ti -d -p 28017:27017 --name mongo-www-r2-mongodb-my mongo:latest bash) bash # create and ssh to container
root@xxxxxx:~# mongod --replSet "myReplica" --bind_ip_all --fork --logpath /var/log/mongod.log # start mongo server with configuration
root@xxxxxx:~# exit # exit ssh
For this, we'll use www.r0.mongodb.my (mongo-www-r1-mongodb-my) as the primary cluster
$ mongo --host www.r0.mongodb.my --port 28015
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:8513/?compressors=disabled&gssapiServiceName=mongodb
...
Configure Replica Set Note: It's recommended to read more from the official documentation
> rs.initiate( {
_id : "myReplica",
members: [
{ _id: 0, host: "www.r0.mongodb.my:28015" },
{ _id: 1, host: "www.r1.mongodb.my:28016" },
{ _id: 2, host: "www.r2.mongodb.my:28017" }
]
})
Note: For more details, visit the official documentation for Connection Strings.
Our connection string would be mongodb://www.r0.mongodb.my:28015,www.r1.mongodb.my:28016,www.r2.mongodb.my:28017/?replicaSet=myReplica
On the host machine's terminal (i.e. Command Prompt, PowerShell), run the following command
$ mongo "mongodb://www.r0.mongodb.my:28015,www.r1.mongodb.my:28016,www.r2.mongodb.my:28017/?replicaSet=myReplica"
Now, try to write any documents to the database and check if the inserted data reflected to all secondary clusters.
- Try to takedown/disable the primary cluster and then check if the new primary cluster is elected.