Replication with MongoDB
Configure MongoDB ReplicaSets using Mongo on a Server or via Docker Compose
Updated: 03 September 2023
Using a single MongoDB instance is cool, but you know what’s cooler? Using more than one. Sometimes we need to set up a couple of different database replicas, this is a general method of how this can be done
On a Development Machine
We’ll create some additional MongoDB services on the machine and then link them together
Create the DB Folders
We will need the folders for databases to store their data, we can create this from a clean folder like so:
This will create the folders for three database nodes
Startup the DB Services
You can start three DB services in different terminals with the following:
Note that you can also configure an instance to startup with replication by using the mongod.cfg
file (for a normal instance). To enable replication you can use the following:
You can thereafter go on to configure the replicaset, additionally it’s helpful to note that any of the other options we passed in via the command line can be set-up including the Port, IPs, Oplog Size and DB Path
Configure the Replica Set
Next, log in to one of the Database instances to use as the PRIMARY
using the mongo
shell:
And then create an configuration object and initiate the replicaset with the configuration:
You can then view the replicaset config with:
From this, we should see each member’s current config look something like:
Create Some Data
From the Instance we are already logged into, we can create some data like so:
- Set a DB to use (this won’t exist, will be created automatically)
- Insert a document into a collection
- View the inserted document
This data should now be written to each instance within the replicaset we configured
Check Replication Status
There are two functions we can use to get information while on the instance, that is rs.status()
which will give a lot of detail about the status of nodes, or rs.printSlaveReplicationInfo()
which will give us a summary of the sync status like so:
If we want, we can stop one of the SECONDARY
DB containers and then run the above, for example, stopping Node 2 we can see:
Which is an indication that the node is down. If we disable the primary one of the other nodes will then become the primary node and the original primary will be marked as behind:
Inside of Some Containers
Start Some Databases
If we’re just playing around with this it can be useful for us to start up a few databases that we can use for testing this process. To start a few simple database nodes we can use the following docker-compose
file and start it all up with docker-compose up
from the directory in which we have this file
This database setup is not a production setup, just for testing purposes. It will not persist data once the containers are stopped
docker-compose.yml
The above yaml file will run three mongo containers along with the following details:
Container Name | Internal Endpoint | From-Host Endpoint |
---|---|---|
mongonode0 | mongonode0:27017 | localhost:37000 |
mongonode1 | mongonode1:27017 | localhost:37001 |
mongonode2 | mongonode2:27017 | localhost:37002 |
Note that in the docker-compose.yml
file we specify some additional entrypoint params, these are very similar to the parameters we start the mongodb instance with on our local machine, these tell docker to start the containers wit the replSet
option
Set-up the ReplicaSet
Now that we have three DBs, we need to set up the replication. This is very similar to the way we did it above, we have two options to log into the instance:
- From the Host Computer
- From within the Docker container
Connecting from the Host is identical to what we did using the “On a Development Machine” section, to connect via the Docker container do the following:
- Run the
docker exec
command for any node, this will become thePRIMARY
node when we set up the replication
- Log into the Mongo Instance of the container
Note that because we are accessing the DB from
localhost
we don’t need to worry about authentication
- Initiate the replication, note that here we are using the internal hosts that are configured within the compose cluster
- View the Configuration:
We can then go on to insert documents into the DB as we did on the local instance as well as verify the sync status
Arbiters and 2-Node Setups
From the MongoDB Documentation
In a MongoDB ReplicaSet an Arbiter is a node that does not store data but is allowed to vote on the validity of data
Before doing this ensure that you stop and remove previously running containers or delete your DB folders (based on the method you used)
Since it’s not always possible to set-up a third (or additional) that store data, we can configure an arbiter. We can use either of the methods of starting up the Three DB instances as above, but we will instead only configure two instances as nodes. the below config will do that:
Next, we can use the rs.addArb(HOST_URL)
function to add the third node as an arbiter:
Remove a Member
From the MongoDB Documentation
To remove any member node you can use the rs.remove(HOST_URL)
function like so:
The above applies to both Replication and Arbiter nodes
Edit Configuration
We can use the rs.reconfig(NEW_CONFIG)
function to provide a new replicaset configuration. There are a few different ways this can be used:
- Get the Configuration with
rs.config()
, then edit the config and re-provide it like so:
- Manipulate the config directly from the mongo console, for example you can use javascript in the console to manipulate the config object like so: