--- title: MongoDb Cluster using 3 nodes --- # How to set up a MongoDB Cluster using 3 nodes with Linux? To follow, you will need three compute nodes. You can launch compute nodes on-demand on E2E Public Cloud. [Click here](/docs/myaccount/node/getting-started/create-node/) to know more. As part of this 3 node MongoDB Cluster, one node will be primary and the other two would be secondary nodes. The benefit of this setup is that it helps you overcome a single point of failure. When the primary node goes down, the secondary nodes act as primary; thus, ensuring high availability. ![](images/MDB1.png) Follow the below steps to set up a 3 node MongoDB cluster: **Update and upgrade all the 3 nodes** ```bash sudo apt-get update sudo apt-get upgrade ``` **MongoDB installation:** ```bash sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4 echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list sudo apt-get update sudo apt-get install -y mongodb-org=4.0.10 mongodb-org-server=4.0.10 mongodb-org-shell=4.0.10 mongodb-org-mongos=4.0.10 mongodb-org-tools=4.0.10 ``` **Setup hostname & hostentry on all the 3 nodes:** ```bash sudo vim /etc/hosts 172.16.1.x test-mongo-1 172.16.2.x test-mongo-2 172.16.3.x test-mongo-3 hostname test-mongo1 sudo vim /etc/hostname test-mongo-1 ``` Set corresponding hostname on all the 3 nodes If the server is from E2E Cloud, then remove 05-hostname 02-ssh_public_key from /etc/one-context.d/ directory **Generate a Key file and copy that to the other 2 nodes:** ```bash sudo cd /etc/ sudo mkdir mongo cd sudo openssl rand -base64 756 > /etc/mongo/mongo-keyfile sudo cd /etc/mongo/ sudo chmod 400 mongo-keyfile sudo chown mongodb:mongodb /etc/mongo -R cd sudo rsync -avrP /etc/mongo root@172.16.2.x:/etc/ sudo rsync -avrP /etc/mongo root@172.16.3.x:/etc/ ``` **Add Replication section on mongodb.conf on all the 3 nodes:** ```bash replication: replSetName: mongo-cluster enableMajorityReadConcern: true ``` **Start and check status of mongodb on all the 3 nodes:** ```bash sudo service mongod start sudo service mongod status ``` Ensure that mongodb starts on all the 3 nodes. **Change MongoDB bind address to server’s private ip on all 3 nodes:** ```bash sudo vim /etc/mongod.conf # network interfaces net: port: 27017 bindIp: 172.16.1.x sudo service mongod restart ``` **Login to Mongodb on any one of the node:** ``` bash mongo -host 172.16.1.x config = { _id : "mongo-cluster", members : [ {_id : 0, host : "test-mongo-1:27017"}, {_id : 1, host : "test-mongo-2:27017"}, {_id : 2, host : "test-mongo-3:27017"}, ] } rs.initiate(config) ``` :::info Note After some time, this machine (from where the command executed will become the primary – you can log out of MongoDB and login to check the same). ::: **Now create admin & maindatabase DB and user to access the DB:** ```bash use admin db.createUser({ user: "admin", pwd: "E580nOOUE6cDhQ", roles: [{ role: "root", db: "admin" }] }) stagedatabase db.createUser({ user: "stage", pwd: "4n1PdUKkyoU9wcTNW", roles: [{ role: "dbOwner", db: "maindatabase" }] }) ``` **Add security section on mongod.conf on all the 3 nodes:** ```bash security: keyFile: /etc/mongo/mongo-keyfile clusterAuthMode: keyFile authorization: enabled ``` **Restart and check status of mongodb on all the 3 nodes:** ```bash sudo service mongod restart sudo service mongod status ``` **Login to Mongodb with the credentials:** ```bash mongo -host 172.16.1.x admin -u admin -p'password' ``` **Login to Mongodb with the other user stagedatabase:** ```bash mongo 172.16.1.x:27017/maindatabase -u stage -p password ``` Hurray! Now the setup is complete. ---