Skip to content

Instantly share code, notes, and snippets.

@euhansarkar
Created May 4, 2025 22:42
Show Gist options
  • Select an option

  • Save euhansarkar/20d236a61b73e3d1b84a28d8cf60bc2b to your computer and use it in GitHub Desktop.

Select an option

Save euhansarkar/20d236a61b73e3d1b84a28d8cf60bc2b to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
GREEN='\033[0;32m'
NC='\033[0m'
# Check for Ubuntu version
echo -e "${GREEN}Checking Ubuntu version...${NC}"
UBUNTU_CODENAME=$(lsb_release -cs)
# Supported versions: noble, jammy, focal
if [[ ! " noble jammy focal " =~ " $UBUNTU_CODENAME " ]]; then
echo "Error: Unsupported Ubuntu version ($UBUNTU_CODENAME). Supported: noble, jammy, focal"
exit 1
fi
# Step 1: Remove conflicting packages
echo -e "${GREEN}Removing conflicting mongodb packages...${NC}"
sudo apt-get remove -y mongodb mongodb-clients || echo "No conflicting packages found"
# Step 2: Install dependencies
echo -e "${GREEN}Installing dependencies...${NC}"
sudo apt-get update -y
sudo apt-get install -y wget gnupg curl
# Step 3: Import MongoDB GPG key
echo -e "${GREEN}Importing MongoDB GPG key...${NC}"
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
--dearmor
# Step 4: Add MongoDB repository
echo -e "${GREEN}Adding MongoDB repository...${NC}"
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu $UBUNTU_CODENAME/mongodb-org/8.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
# Step 5: Install MongoDB
echo -e "${GREEN}Installing MongoDB 8.0...${NC}"
sudo apt-get update -y
sudo apt-get install -y mongodb-org
# Step 6: Start and enable MongoDB service
echo -e "${GREEN}Starting and enabling MongoDB service...${NC}"
sudo systemctl start mongod
sudo systemctl enable mongod
# Wait for MongoDB to initialize
sleep 10
# Step 7: Create admin user
echo -e "${GREEN}Creating admin user...${NC}"
mongo admin --eval "db.createUser({user: 'admin', pwd: 'adminpassword', roles: [{role: 'userAdminAnyDatabase', db: 'admin'}, {role: 'readWriteAnyDatabase', db: 'admin'}]})"
# Step 8: Enable authorization and bindIp in config
echo -e "${GREEN}Updating MongoDB configuration...${NC}"
sudo tee -a /etc/mongod.conf > /dev/null <<EOL
security:
authorization: enabled
# Ensure bindIp includes localhost
net:
bindIp: 127.0.0.1
EOL
# Step 9: Restart MongoDB to apply changes
echo -e "${GREEN}Restarting MongoDB to apply configuration...${NC}"
sudo systemctl restart mongod
# Wait for MongoDB to restart
sleep 10
# Step 10: Create application user
APP_DB="mydatabase"
APP_USER="myuser"
APP_PASS="mypassword"
echo -e "${GREEN}Creating application user in '$APP_DB'...${NC}"
mongosh "$APP_DB" -u admin -p adminpassword --authenticationDatabase admin --eval "db.createUser({user: '$APP_USER', pwd: '$APP_PASS', roles: [{role: 'readWrite', db: '$APP_DB'}]})"
# Output connection string
echo -e "${GREEN}MongoDB setup completed successfully!${NC}"
echo ""
echo "✅ Connection String:"
echo "mongodb://$APP_USER:$APP_PASS@localhost:27017/$APP_DB?authSource=admin"
echo ""
echo "💡 To connect via mongosh or drivers, use the above string."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment