Last active
August 11, 2021 10:50
-
-
Save jeka-kiselyov/67132057f35e8ece67d199612c80c468 to your computer and use it in GitHub Desktop.
Install and setup mongodb server on AWS Ubuntu 18.04 EC2 Instance
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 | |
#Makes it public and set up authentication | |
echo "Installing repo" | |
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4 | |
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list | |
echo "Installing binaries" | |
sudo apt-get update | |
sudo apt-get install -y mongodb-org | |
echo "Stoping mongodb if it's running" | |
sudo service mongod stop | |
echo "Setting up default settings" | |
sudo tee /etc/mongod.conf <<'EOF' | |
storage: | |
dbPath: /var/lib/mongodb | |
journal: | |
enabled: true | |
systemLog: | |
destination: file | |
logAppend: true | |
path: /var/log/mongodb/mongod.log | |
net: | |
port: 27017 | |
bindIp: 0.0.0.0 | |
bindIpAll: true | |
maxIncomingConnections: 100 | |
security: | |
authorization: enabled | |
EOF | |
echo "Starting mongodb service..." | |
sudo service mongod start | |
sleep 5 | |
echo "Is it there?" | |
sudo cat /var/log/mongodb/mongod.log | tail -n 10 | |
echo "Adding admin user" | |
read -p "Enter Master Username: " username | |
read -s -p "Enter Master Password: " password | |
mongo admin <<EOF | |
use admin | |
rs.initiate() | |
var user = { | |
"user" : "$username", | |
"pwd" : "$password", | |
roles : [ | |
{ | |
"role" : "userAdminAnyDatabase", | |
"db" : "admin" | |
}, | |
"readWriteAnyDatabase" | |
] | |
} | |
db.createUser(user); | |
exit | |
EOF | |
echo "Start mongodb service on startup" | |
sudo systemctl enable mongod.service | |
echo "Complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment