Requirements:
Step 1) Open all your VPS' in an SSH client such as MobaXterm or however you ssh into a VPS. Like so:
Step 2) Update all packages using:
sudo yum update -y
This may take some time so be patient with it.
Step 3) Ensure nano is installed. Complete this all on VPS'.
sudo yum install nano -y
Since MongoDB doesn't exist by default you will have to install it manually on all machines/VPS'.
sudo vi /etc/yum.repos.d/mongodb-org-4.4.repo
Then press the i key to insert content.
Add/copy paste in the following content:
[mongodb-org-4.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
Install using this command:
sudo yum install -y mongodb-org
If you have SELinux enabled you will have to run:
semanage port -a -t mongod_port_t -p tcp 27017
You may have disabled this previously and may be running a firewall instead. You must allow the port 27017/tcp to access the database.
Then to start the mongodb instance run:
sudo systemctl start mongod
If you experience an error, please make sure you update the VPS or reinstall it(can be quicker and less demanding).
Generate the key file on ONE of them. Then copy and paste the contents to a mongo-keyfile on the rest.
openssl rand -base64 756 > mongo-keyfile
Create the directory for the mongo-keyfile in /opt/mongo
sudo mkdir -p /opt/mongo/
Move the keyfile to the directory we just created and give it appropriate permissions.
sudo mv ~/mongo-keyfile /opt/mongo/ && sudo chmod -R 400 /opt/mongo/
If this fails with chmod: cannot access ‘/opt/mongo/mongo-keyfile’: Not a directory
cd /opt/mongo && mv mongo mongo-keyfile && cd ~
Then set the appropriate permissions:
chown -R mongod:mongod /opt/mongo chmod 700 /opt/mongo chmod 600 /opt/mongo/mongo-keyfile chcon system_u:object_r:mongod_var_lib_t:s0 /opt/mongo/mongo-keyfile
Access the mongodb instance with mongo:
mongo
While in the mongo shell type:
use admin
Then create an admin user, change the pwd with a more secure password.
db.createUser({user: "admin", pwd: "VerySecurePassword", roles:[{role: "root", db: "admin"}]})
Once done, type:
exit
To return back to the shell.
Next we need to edit the configuration file to enable it to work for a replica set.
sudo nano /etc/mongod.conf
Once opened put in the following information:
net: port: 27017 bindIp: 0.0.0.0 security: keyFile: /opt/mongo/mongo-keyfile authorization: enabled replication: replSetName: rs0
bindIp is to allow access from all hosts, then uncomment the security and replication line.
After typing that in you need to execute:
sudo systemctl restart mongod
You can find out what is causing the issue by running:
sudo tail /var/log/mongodb/mongod.log
mongo -u admin -p --authenticationDatabase admin
Then type in the password you chose.
Run the following on the one you want to be the slave master/main one
Replace ip1 and ip2 with the ip addresses of the other servers and if different ports specify them.
rs.initiate(
{_id:'rs0',
members:[ { _id:1, host:'ip1:27017'},
{ _id:2, host:'ip2:27017'}]
});
Get the config into a variable:
cfg = rs.conf()
Replace currentIP0 with the VPS public ip address. Then edit the host one:
cfg.members[0]["host"] = "currentIP0:27017"
Then to make the changes live:
rs.reconfig(cfg, {force: true})
Use the test database:
use test
Insert some data to the repl collection to check if it's working
db.repl.insert({"name": "john"})
You will get a result like:
WriteResult({ "nInserted" : 1 })
Now run the following on one of the other servers and type in the password:
mongo -u admin -p --authenticationDatabase admin
Use the test database:
use test
You should get a result with:
{ "_id" : ObjectId("60e0cc625429aa121b8e677e"), "name" : "john" }
Now you have successfully done replication with MongoDB!