I'm trying to use a shell script to automate the task of setting up MongoDB on Ubuntu 20.04 as follows:
mongodb_install.sh*
MONGO_VERSION=6.0
sudo apt-get update
sudo apt-get install gnupg
# import MongoDB GPG public key
wget -qO - https://www.mongodb.org/static/pgp/server-${MONGO_VERSION}.asc | sudo apt-key add -
sudo cd /etc/apt/sources.list.d/
sudo touch mongodb-org-${MONGO_VERSION}.list
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/${MONGO_VERSION} multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-${MONGO_VERSION}.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
sudo service mongod start
sudo systemctl status mongod
After running the script I receive status 'active':
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2022-11-03 14:04:45 +07; 7ms ago
Docs: https://docs.mongodb.org/manual
Main PID: 74147 (mongod)
Memory: 796.0K
CGroup: /system.slice/mongod.service
└─74147 /usr/bin/mongod --config /etc/mongod.conf
Yet when I recheck in the terminal mongod service does not start:
$ systemctl status mongod
mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2022-11-03 14:04:45 +07; 7s ago
Docs: https://docs.mongodb.org/manual
Process: 74147 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
Main PID: 74147 (code=exited, status=14)
I have tried different combinations of sudo ./mongodb_install.sh
, sudo . ./mongodb_install.sh
, etc., but nothing has worked so far.
Could you show me a way to resolve this?
systemctl status mongod
output, including the log entries? There must be some logs showing errors.apt-key add
is a really bad idea - see here why. You should instead install the key to/etc/apt/keyrings
.