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
- 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
- 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
- Update ubuntu package database using
sudo apt-get update
- 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
- Install latest version using this command
- 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
- 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
- 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
- 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
- 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
- Make sure the folder is writeable using
sudo chmod go+w /home/cloudadmin/external-drive/mongodb
- 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
- 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
- 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
- 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
- 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
to24001
bindIp
into0.0.0.0
- 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
path
into/home/cloudadmin/external-drive/mongodb/logs/mongo-24002.log
port
to24002
bindIp
into0.0.0.0
- 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
to24003
bindIp
into0.0.0.0
- Give permissions to MongoDB user and group using the following command
sudo chown -R mongodb:mongodb external-drive/mongodb/
- 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.
-
- 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
- Restart all three servers using
sudo service mongod-24001 restart sudo service mongod-24002 restart sudo service mongod-24003 restart
- 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
- Note: In the next 3 commands replace localhost with the IP address of the server.
- In the mongo command line initiate replica-set using
rs.initiate({ _id: "rssk", members: [{ _id: 0, host: "localhost:24001" }] })
- Then add the other 2 servers using
rs.add("localhost:24002"); rs.add("localhost:24003");
- Check the server settings using
rs.conf()
or
rs.status()
- 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.
- 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
-
Authentication & Authorisation
- 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
- 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
- To protect file from accidental overwriting, we must change the file permissions using
sudo chmod 400 /home/cloudadmin/external-drive/mongodb/keys/rssk.key
- 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
- Now lets start creating users for authentication. In order to do that lets login to our first database.
mongo --port 24001
- Change to admin database using
use admin
- 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.
- 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
- Restart all three servers using
sudo service mongod-24001 restart sudo service mongod-24002 restart sudo service mongod-24003 restart
- Now all three servers should have authentication.
- If you want to set access to different databases and oplog then follow these additional steps.
- 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.
- 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.
- For authentication on replicaset, we should have a key. So lets create a key using
Create MongoDB Replicaset On One Server
- 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
- Log in to database using mongo command. If you are using username and password, use the relevant mongo command to log in.
mongo
- 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.
He has over 9 years of experience in design, development and implementation of innovative business IT solutions. He is highly skilled in cloud computing, information management and application architecture. In free time, he writes some blogs and strives to learn new technical skills. Check out his recent blogs here.