Last active
October 23, 2022 12:41
-
-
Save Maria-UET/af4332f6dd9e57f2d0f6141dbb8dd447 to your computer and use it in GitHub Desktop.
Setup MongoDB replica set with three nodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
######## Run this bash script on EACH replica set node to configure a Mongodb replica set ######### | |
# HOWTO: | |
# System recommendation : Ubuntu 16.04 | |
# Download the script on each system of the replica set | |
# Open in bash and make it executable with command: chmod +x mongo_replica_config.sh | |
# Run with command: sudo ./mongo_replica_config.sh | |
# OTHER USEFUL SCRIPTS: | |
# For MongoDB installation: https://gist.github.com/Maria-UET/1368387e2e72d2de96fc824919dad176 | |
# For initiating the MongoDB replica set after configuration: https://gist.github.com/Maria-UET/1368387e2e72d2de96fc824919dad176 | |
# Replace with the ip addresses of all nodes in the replica set | |
IPS=("10.0.0.1" "10.0.0.2" "10.0.0.3") | |
# Replace with the hostnames of all nodes in the replica set | |
HOSTS=("node1" "node2" "node3") | |
HOSTNAMES=("${IPS[0]} ${HOSTS[0]}" "${IPS[1]} ${HOSTS[1]}" "${IPS[2]} ${HOSTS[2]}") | |
ETC_HOSTS=/etc/hosts | |
REPL_SET="rs0" | |
BIND_IP=0.0.0.0 | |
echo "Opening port 27017 for connection" | |
sudo iptables -A OUTPUT -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT | |
sudo iptables -A INPUT -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT | |
# Add the hostnames and ip addresses of all nodes in the replica set into /etc/hosts file | |
for i in ${!HOSTS[@]} | |
do | |
if [ -n "$(grep ${HOSTS[$i]} $ETC_HOSTS)" ] | |
then | |
echo "${HOSTS[$i]} already exists in $ETC_HOSTS" # check if the host is already present | |
else | |
echo ${HOSTNAMES[$i]}>> /etc/hosts | |
fi | |
done | |
# Change the bingingIp of mongod server to allow binding to all three nodes. bindIp 0.0.0.0 allows binding to any ip, so do not use it without db authentication enabled | |
echo "Changing bindingIP address to $BIND_IP" | |
sudo sed -i[bindIp] "s/bindIp: /bindIp: $BIND_IP #/g" /etc/mongod.conf | |
echo "Adding replica set configuration to the /etc/mongod.conf" | |
echo "#replication config | |
replication: | |
replSetName: $REPL_SET" >> /etc/mongod.conf | |
# Add the directory for server data if such directory doesnt already exist | |
echo "Creating directory /data/db" | |
sudo mkdir -p /data/db | |
echo "Restarting the mongod server" | |
sudo systemctl stop mongod | |
sudo systemctl daemon-reload | |
#sudo systemctl start mongod | |
#echo `sudo systemctl restart mongod` | |
echo "Mongodb replica set configuration has been completed" | |
echo "Starting the mongod server with replica set $replSet with the following command:" | |
echo "sudo mongod --replSet $REPL_SET --bind_ip_all --fork --logpath /var/log/mongodb.log" | |
sudo mongod --replSet $REPL_SET --bind_ip $BIND_IP --fork --logpath /var/log/mongodb.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment