MongoDb replica set install

Data integrity and fault tolerance are key areas for any database. For MongoDb, we want the ability to replicate data on 3 or more separate database servers. This provides us with significant uptime, and ensuring that if one or two random outages occur, we can still operate the business.

First, you want to ensure that you have all of the MongoDb server prerequisites installed. You can visit MongoDb here for more information:

We are going to be starting with a base Ubuntu 22.04 install. Download the latest from Ubuntu Server at https://ubuntu.com/download/server.

Next, before doing anything, download VirtualMin: https://www.virtualmin.com/download/. Click the “Download Virtualmin GPL” button.

sudo su -
cd /root    
mv /home/ddietrich/virtualmin-install.sh .
chown root:root virtualmin-install.sh 
chmod u+x virtualmin-install.sh
./virtualmin.sh 

Once done, you will have a completed install with Virtualmin (including Webmin). Go to your server at http://YouServerNameHere.com:10000/. Login with your root password. Once you are logged in, select the Refresh and Update Settings button to configure your Virtualmin settings for your usage.

Once you’ve installed Virtualmin and Webmin, install your SSL certificate for the server, if you have one. If you do have one, I like to install my SSL certificates in /root/{{Domain Name for this server}}. In our case here, you will want to copy your SSL certificate .pem file (contains both the private and public key), and the gd_bundle.crt. You may already have a gd_bundle.crt. If so and this is NOT a GoDaddy registered domain, leave it alone. Otherwise if you DO have a GoDaddy account, pull your gd_bundle.crt from godaddy.com and copy it, and the .pem file, to a /root/ssl-certs directory.

Now, edit the /etc/webmin/miniserv.conf file. Ensure you have the following two lines (comment any preexisting lines that start with keyfile= and/or extracas= by placing a # as the first character on the line), add these lines with your domain SSL certificate:

keyfile=/root/ssl-certs/domain-for-my-ssl-cert.com.pem
extracas=/root/ssl-certs/gd_bundle.crt

Reboot.

Next, I like to copy over a set of shell scripts I use for common everyday tasks:

cd /opt
mv /home/ddietrich/scripts/ .
chown -R root:root scripts/
cd /etc/profile.d
ln -s /opt/scripts/bash_completion.sh grayarrow.sh
reboot

Next, you want to ensure that you have a fully-qualified domain name (FQDN) for your server. In our case, we used u22mongo1.grayarrow.com. Set this in your /etc/hostname name file. Then ensure it is in your /etc/hosts file too. Additionally, since this is going to be a replica set, be sure to add your other server names and their IP addresses to your /etc/hosts file. Once completed, reboot.

Example /etc/hosts with the local machine name, and the FQDNs of the three machine replica set:

127.0.0.1 localhost
127.0.1.1 u22mongo1

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.0.201	u22mongo1.grayarrow.com
192.168.0.202	u22mongo2.grayarrow.com
192.168.0.203	u22mongo3.grayarrow.com

Now we are ready for MongoDb. Follow the MongoDb page here for setting up MongoDb on Ubuntu Linux: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/.

1. install MongoDB on Ubuntu: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
2. Convert to a Replica Set: https://www.mongodb.com/docs/manual/tutorial/convert-standalone-to-replica-set/
3. If needed, here’s a Bash script to convert to a Replica Set: https://www.mongodb.com/community/forums/t/bash-script-to-create-a-replica-set/3986/3
4. Deploy the Replica set: https://www.mongodb.com/docs/v5.0/tutorial/deploy-replica-set/
5. Connect to the Replica set: https://www.mongodb.com/docs/mongodb-shell/connect/