Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created November 13, 2024 18:09
Show Gist options
  • Save sibelius/a9a2dfd32c72955ea61fb0a4779e4b10 to your computer and use it in GitHub Desktop.
Save sibelius/a9a2dfd32c72955ea61fb0a4779e4b10 to your computer and use it in GitHub Desktop.
mongodb-rs.sh
args="$@"
docker-compose up -d $args
sleep 5
updateEtcHost() {
local content=$1
if grep -q "$content" /etc/hosts; then
echo "Line already exists in /etc/hosts. No changes made."
else
# Append the line to /etc/hosts
echo "$content" | sudo tee -a /etc/hosts
echo "Line added to /etc/hosts."
fi
}
updateEtcHost '172.20.0.2 mongo1'
updateEtcHost '172.20.0.3 mongo2'
updateEtcHost '172.20.0.4 mongo-hidden'
if [[ -z $args || $args != *"mongo-hidden"* ]]; then
docker exec mongo1 ./scripts/rs-init.sh
sleep 5
source scripts/mongodb/mongo-reconfigure.sh
else
docker exec mongo1 ./scripts/rs-init-hidden.sh
sleep 5
source scripts/mongodb/mongo-reconfigure-hidden.sh
fi
#!/bin/bash
mongosh <<EOF
const config = {
"_id": "rs0",
"version": 1,
"members": [
{
"_id": 1,
"host": "mongo1:27017",
"priority": 2
},
{
"_id": 2,
"host": "mongo2:27017",
"priority": 1
},
]
};
rs.initiate(config, { force: true });
rs.status();
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment