-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-mongodb.sh
executable file
·73 lines (59 loc) · 2.01 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
set -e
purge=0
purge_data=0
yes=
while [[ "$1" =~ ^- ]]; do
if [ "$1" = "--purge" ]; then
purge=1
elif [ "$1" = "--purge-data" ]; then
purge_data=1
elif [ "$1" = "--yes" ] || [ "$1" = "-y" ]; then
yes="-y"
else
echo "ERROR: Unknown option: $1"
exit 1
fi
shift
done
mongodb_version="${1:-5.0}"
if [ "$mongodb_version" != 4.0 ] && [ "$mongodb_version" != 5.0 ]; then
echo "ERROR: Unknown mongodb_version: $mongodb_version"
exit 1
fi
echo
echo "*** Installing MongoDB $mongodb_version ***"
echo
set -x
sudo curl -o /etc/init.d/mongod https://raw.githubusercontent.com/heidemn/mongod-sysv/master/mongod
sudo chmod +x /etc/init.d/mongod
sudo service mongod status || true
if [ "$purge" = 1 ]; then
sudo apt-get purge $yes "mongodb*"
fi
if [ "$purge_data" = 1 ]; then
sudo rm -rf /var/lib/mongodb/
fi
sudo apt-get update
sudo apt-get $yes install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-${mongodb_version}.asc | sudo apt-key add -
source /etc/os-release
if [ "$mongodb_version" = 4.0 ]; then
# "focal" is N/A according to MongoDB 4.0 docs.
UBUNTU_CODENAME=bionic
fi
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu ${UBUNTU_CODENAME}/mongodb-org/${mongodb_version} multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-${mongodb_version}.list
sudo apt-get update
if [ "$mongodb_version" = 4.0 ]; then
# Used earlier: 4.0.21
sudo apt-get install $yes mongodb-org=4.0.26 mongodb-org-server=4.0.26 mongodb-org-shell=4.0.26 mongodb-org-mongos=4.0.26 mongodb-org-tools=4.0.26
else
sudo apt-get install $yes mongodb-org=5.0.2 mongodb-org-database=5.0.2 mongodb-org-server=5.0.2 mongodb-org-shell=5.0.2 mongodb-org-mongos=5.0.2 mongodb-org-tools=5.0.2
fi
sudo mkdir -p /var/lib/mongodb/
sudo chown mongodb:mongodb /var/lib/mongodb/
sudo service mongod status || true
sudo service mongod start
mongo --eval 'db.adminCommand( { listDatabases: 1 } )'
sudo service mongod stop
sudo service mongod status || true