Create MongoDB Replicaset for Replication

Hello readers, today we are going to go through the MongoDB installation process in ubuntu. We will create three database servers and also create MongoDB replicaset between those three servers. Lets first look at installing the database. If you already installed database, move to replicaset section.

Although this tutorial is bit intense please stay focused. ūüôā

Create MongoDB Database

I have taken the relevant steps from MongoDB official website. We are installing MongoDB version 3.2 Community Edition on Ubuntu 16.04.

Setting up the Server

  1. First, lets import the public key to make sure that there is package consistency and authenticity in ubuntu package management tools. Use this command to import: 
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
  2. Download the appropriate version of MongoDB file based on your operating system. In my case, it is MongoDB 3.2 with Ubuntu 16.04. Use this command to download: 
    echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
  3. Update ubuntu package database using 
    sudo apt-get update
  4. Install MongoDB packages using one of the two ways.
    • Install latest version using this command
      sudo apt-get install -y mongodb-org
    • Or use this command to install a specific version, 3.2 in my case.¬†
      sudo apt-get install -y mongodb-org=3.2.20 mongodb-org-server=3.2.20 mongodb-org-shell=3.2.20 mongodb-org-mongos=3.2.20 mongodb-org-tools=3.2.20
  5. In order for package management to NOT upgrade to latest releases of MongoDB, set a specific version using
    echo "mongodb-org hold" | sudo dpkg --set-selections
    echo "mongodb-org-server hold" | sudo dpkg --set-selections
    echo "mongodb-org-shell hold" | sudo dpkg --set-selections
    echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
    echo "mongodb-org-tools hold" | sudo dpkg --set-selections
    
  6. For this tutorial, I am installing the three MongoDB servers on the same machine. This can be useful for use-cases like SIT and UAT. But for deploying in production purposes, you have to install each MongoDB server on a different physical server or virtual machine.

Create Required Files and Folders

  1. Create a file /lib/systemd/system/mongod.service and copy the following content into it. If you are installing in three different machines then create this file in each of those servers.

    [Unit]
    Description=High-performance, schema-free document-oriented database
    After=network.target
    Documentation=https://docs.mongodb.org/manual
    
    [Service]
    User=mongodb
    Group=mongodb
    ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
    
    [Install]
    WantedBy=multi-user.target
  2. If you are deploying it in single machine then create separate service file for each MongoDB server. Use the following commands to copy it 3 times.
    sudo cp /lib/systemd/system/mongod.service /lib/systemd/system/mongod-24001.service
    sudo cp /lib/systemd/system/mongod.service /lib/systemd/system/mongod-24002.service
    sudo cp /lib/systemd/system/mongod.service /lib/systemd/system/mongod-24003.service
    
  3. Create required folders for MongoDB. We have four root folders required. They are for config, data, logs and keys. In my case, I am creating all of my MongoDB related files in /home/cloudadmin/external-drive/mongodb folder. So you can change the path in the following commands based on your folder location. If you are deploying it in different machines you need to execute these commands in all of them except for db-24001, db-24002 and db-24003 folders. Create these folders in one server each.
    sudo mkdir /home/cloudadmin/external-drive/mongodb
    sudo mkdir /home/cloudadmin/external-drive/mongodb/config
    sudo mkdir /home/cloudadmin/external-drive/mongodb/data
    sudo mkdir /home/cloudadmin/external-drive/mongodb/data/db-24001
    sudo mkdir /home/cloudadmin/external-drive/mongodb/data/db-24002
    sudo mkdir /home/cloudadmin/external-drive/mongodb/data/db-24003
    sudo mkdir /home/cloudadmin/external-drive/mongodb/logs
    sudo mkdir /home/cloudadmin/external-drive/mongodb/keys
    
  4. Make sure the folder is writeable using
    sudo chmod go+w /home/cloudadmin/external-drive/mongodb
  5. Create three configuration files for three mongo services using the following commands. If you are installing on three different machines then use each command on each of the system. Make sure to execute all commands with 24001 on one machine and similarly 24002, 24003 on respective machines.
    sudo cp /etc/mongod.conf /home/cloudadmin/external-drive/mongodb/config/mongod-24001.conf
    sudo cp /etc/mongod.conf /home/cloudadmin/external-drive/mongodb/config/mongod-24002.conf
    sudo cp /etc/mongod.conf /home/cloudadmin/external-drive/mongodb/config/mongod-24003.conf
  6. Now in /lib/systemd/system/mongod-24001.service change
    ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
    

    to

    ExecStart=/usr/bin/mongod --quiet --config /home/cloudadmin/external-drive/mongodb/config/mongod-24001.conf
  7. Similarly in /lib/systemd/system/mongod-24002.service change
    ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
    

    into

    ExecStart=/usr/bin/mongod --quiet --config /home/cloudadmin/external-drive/mongodb/config/mongod-24002.conf
  8. Likewise in /lib/systemd/system/mongod-24003.service change
    ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
    

    to

    ExecStart=/usr/bin/mongod --quiet --config /home/cloudadmin/external-drive/mongodb/config/mongod-24003.conf
  9. Change the configuration for first mongodb server in /home/cloudadmin/external-drive/mongodb/config/mongod-24001.conf. Change the following properties
    • dbPath to </span/home/cloudadmin/external-drive/mongodb/data/db-24001
    • path¬†into /home/cloudadmin/external-drive/mongodb/logs/mongo-24001.log
    • port to 24001
    • bindIp¬†into 0.0.0.0
    • create mongodb replicaset
  10. Likewise for second mongodb server in /home/cloudadmin/external-drive/mongodb/config/mongod-24002.conf. Change the following properties
    • dbPath to /home/cloudadmin/external-drive/mongodb/data/db-24002
    • pathinto /home/cloudadmin/external-drive/mongodb/logs/mongo-24002.log
    • port to 24002
    • bindIp¬†into 0.0.0.0
  11. Similarly for third mongodb server in /home/cloudadmin/external-drive/mongodb/config/mongod-24003.conf. Change the following properties
    • dbPath to /home/cloudadmin/external-drive/mongodb/data/db-24003
    • path¬†into /home/cloudadmin/external-drive/mongodb/logs/mongo-24003.log
    • port to 24003
    • bindIp¬†into 0.0.0.0
  12. Give permissions to MongoDB user and group using the following command
    sudo chown -R mongodb:mongodb external-drive/mongodb/
  13. If the logs, data and configurations are at different paths then you need to do this chown for each folder using the above command.

Start MongoDB Server

Execute the following commands to start each of the MongoDB servers.

sudo service mongod-24001 start
sudo service mongod-24002 start
sudo service mongod-24003 start

Setup MongoDB To Start On Reboot

If you want to start mongo instances on every time when the system reboots, enable them by using the following commands.

sudo su
systemctl enable /lib/systemd/system/mongod-24001.service
systemctl enable /lib/systemd/system/mongod-24002.service
systemctl enable /lib/systemd/system/mongod-24003.service

Now we have three MongoDB servers running on three different ports 24001, 24002 and 24003.

Create MongoDB Replicaset

Alright, so now we have deployed our MongoDB instances and they are up and running. But we haven’t created the replicaset yet. Also, we haven’t done the authentication and authorisation part as well. First we will look into creating replicaset without authentication and finally add authentication in the next section.

Note: If you are setting replicaset for a single MongodDB instance then check here.

    1. Add replication settings to configuration files /home/cloudadmin/external-drive/mongodb/config/mongod-24001.conf,  /home/cloudadmin/external-drive/mongodb/config/mongod-24002.conf, /home/cloudadmin/external-drive/mongodb/config/mongod-24003.conf
      replication:
          oplogSizeMB: 10240
          replSetName: rssk
      
    2. Restart all three servers using
      sudo service mongod-24001 restart
      sudo service mongod-24002 restart
      sudo service mongod-24003 restart
      
    3. Log in to one of the mongo servers. I am logging in the first server
      mongo --port 24001

      Note: If you are unable to login, it might be because of locale settings. Use this command first before logging in.

      export LC_ALL=C
    4. Note: In the next 3 commands replace localhost with the IP address of the server.
    5. In the mongo command line initiate replica-set using
      rs.initiate({ _id: "rssk", members: [{ _id: 0, host: "localhost:24001" }] })
    6. Then add the other 2 servers using
      rs.add("localhost:24002");
      rs.add("localhost:24003");
      
    7. Check the server settings using
      rs.conf()

      or

      rs.status()
    8. Now three mongo servers will replicate between each other. The replica set name is rssk but if you can change it, then you need to change it in the configuration as shown in the first step and restart.
  1. Authentication & Authorisation

    1. For authentication on replicaset, we should have a key. So lets create a key using
      openssl rand -base64 756 > /home/cloudadmin/rssk.key

      Note: Change the path to an existing directory

    2. Move the newly created key to appropriate location
      sudo mv rssk.key /home/cloudadmin/external-drive/mongodb/keys/rssk.key

      Note: Change the path to your keys folder that you have created earlier

    3. To protect file from accidental overwriting, we must change the file permissions using
      sudo chmod 400 /home/cloudadmin/external-drive/mongodb/keys/rssk.key
    4. Since MongoDB user will use it, we should give the user and group permissions to access it.
      sudo chown -R mongodb:mongodb external-drive/mongodb/keys
    5. Now lets start creating users for authentication. In order to do that lets login to our first database.
      mongo --port 24001
    6. Change to admin database using
      use admin
    7. In the mongo command create a user using
      db.createUser({
         "user": "<dba username>", 
         "pwd": "<any password>", 
         "customData": { }, 
         "roles": [{
            "role": "clusterAdmin", 
            "db": "admin"
         }, { 
            "role": "dbAdminAnyDatabase", 
            "db": "admin" 
         }, { 
            "role" : "readWriteAnyDatabase", 
            "db" : "admin"
         }, {
            "role": "userAdminAnyDatabase", 
            "db" : "admin"
         }]
      })
      

      Note: Change the user and pwd values to the values that you like.

    8. Add security settings to configuration files /home/cloudadmin/external-drive/mongodb/config/mongod-24001.conf,  /home/cloudadmin/external-drive/mongodb/config/mongod-24002.conf, /home/cloudadmin/external-drive/mongodb/config/mongod-24003.conf
      security:
          keyFile: /home/cloudadmin/external-drive/mongodb/keys/rssk.key
          authorization: enabled
          javascriptEnabled: false
    9. Restart all three servers using
      sudo service mongod-24001 restart
      sudo service mongod-24002 restart
      sudo service mongod-24003 restart
      
    10. Now all three servers should have authentication.
    11. If you want to set access to different databases and oplog then follow these additional steps.
    12. If you are using a framework like Meteor then you might need to create an oplogger role and user.
      mongo --port <primary-port-number> -u <username> -p <password> --authenticationDatabase admin
      use admin
      db.runCommand({ createRole: "oplogger", privileges: [{ resource: { db: 'local', collection: 'system.replset'}, actions: ['find']}, ], roles: [{role: 'read', db: 'local'}] })
      db.createUser({ "user" : "oplogger", "pwd" : "<oplogger password>", "roles" : [ { "role" : "oplogger", "db" : "admin" }, { "role" : "read", "db" : "local" } ] })

      Note: Change the pwd value to the values that you like.

    13. Generate another user on a different database. Lets say luckyblogger
      mongo --port <primary-port-number> -u <username> -p <password>
      use luckyblogger
      db.createUser({ "user" : "<username>", "pwd": "<password>", "customData" : {}, "roles" : [{ "role" : "readWrite", "db" : "luckyblogger" }] })

      Note: Change the user and pwd values to the values that you like. You may also like to change the db value wherever necessary.

Create MongoDB Replicaset On One Server

  1. In configuration file generally at /etc/mongodb.conf, uncomment the following lines. In the above tutorial, we have changed it to our config folder.
    replSet=rs0
    oplogSize=1024
  2. Log in to database using mongo command. If you are using username and password, use the relevant mongo command to log in.
    mongo
  3. Switch to local database. After switching then initiate the replicaset using
    use local
    rs.initiate()

Now, you have replicaset available for your single instance.

Thats all. You are able to create a MongoDB replicaset with authentication on top of it. I hope you have enjoyed this tutorial. Thank you for reading my blog. Finally, please leave your feedback.

Want to see other blogs? Please go to our recent blogs here.

Please follow and like us:
error

Leave a Reply

Your email address will not be published. Required fields are marked *