Score:0

Install MongoDB using shell script

gs flag

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 avatar
vn flag
Could you include more of the `systemctl status mongod` output, including the log entries? There must be some logs showing errors.
Artur Meinild avatar
vn flag
Using `apt-key add` is a really bad idea - [see here why](https://askubuntu.com/questions/1286545/what-commands-exactly-should-replace-the-deprecated-apt-key). You should instead install the key to `/etc/apt/keyrings`.
William Le avatar
gs flag
@ArturMeinild that's interesting, this is the instruction I get from MongoDB official site
Artur Meinild avatar
vn flag
Yes, and that instruction is clearly outdated. See my revised answer below.
Score:1
vn flag

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.

Score:0
gs flag

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
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.