0

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?

Artur Meinild
  • 26,018

2 Answers2

1

Use of apt-key add is heavily not recommended, and not even possible on Ubuntu 22.04 and later. Instead, install the key to /etc/apt/keyrings and refer to this key in the .list file:

So your script would look like:

MONGO_VERSION=6.0
sudo apt-get update
sudo apt-get install gnupg -y
# Changes here!
  sudo mkdir -p /etc/apt/keyrings
  curl -fsSL https://www.mongodb.org/static/pgp/server-${MONGO_VERSION}.asc | sudo gpg --dearmor -o /etc/apt/keyrings/mongodb-${MONGO_VERSION}.gpg
cd /etc/apt/sources.list.d/
sudo touch mongodb-org-${MONGO_VERSION}.list
# And here!
  echo "deb [arch=amd64,arm64 signed-by=/etc/apt/keyrings/mongodb-${MONGO_VERSION}.gpg] 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 systemctl start mongod

And yes, somebody who uses MongoDB should probably tell them that their guide is outdated in this aspect.

Artur Meinild
  • 26,018
0

I made 2 changes in order for this to work:

  1. Add -y flag when installing gnupg: sudo apt-get install gnupg -y
  2. Remove the chown section:
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
sudo service mongod start
sudo systemctl status mongod

The resulting shell script for installing MongoDB 6.0 on Ubuntu 20.04 is as follows:

MONGO_VERSION=6.0
sudo apt-get update
sudo apt-get install gnupg -y
wget -qO - https://www.mongodb.org/static/pgp/server-${MONGO_VERSION}.asc | sudo apt-key add -
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 systemctl start mongod