forked from ant-media/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_mongodb.sh
54 lines (44 loc) · 1.66 KB
/
install_mongodb.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
#
# MongoDB Installation Script
#
help() {
echo "Usage: $0"
echo ""
echo "Options:"
echo " --auto-create Automatically create a MongoDB user with a random username and password"
echo " --help Show this help menu"
}
if [ "$1" == "--help" ]; then
help
exit 1
fi
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
sudo apt-get install -y curl gnupg2
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg --dearmor --yes --output /usr/share/keyrings/mongodb-server-6.0.gpg
echo "deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
if [[ $1 == "--auto-create" ]]; then
# Generate a random username and password
username=$(openssl rand -hex 6)
password=$(openssl rand -hex 12)
# Start MongoDB and configure authentication
sudo systemctl restart mongod
sudo systemctl enable mongod
sleep 10
echo "use admin;
db.createUser({ user: '$username', pwd: '$password', roles: ['root'], mechanisms: ['SCRAM-SHA-1'] });" | mongosh
sudo sed -i 's/#security:/security:\n authorization: "enabled"/g' /etc/mongod.conf
sudo sed -i 's/bindIp:.*/bindIp: 0.0.0.0/g' /etc/mongod.conf
sudo systemctl restart mongod
echo "MongoDB username: $username"
echo "MongoDB password: $password"
else
# Start MongoDB without authentication
sudo sed -i 's/bindIp:.*/bindIp: 0.0.0.0/g' /etc/mongod.conf
sudo systemctl restart mongod
sudo systemctl enable mongod
echo "MongoDB installed without username and password"
fi