docs/guides/ubuntu/pages/mongodb-install.md

4.3 KiB

title description published date tags editor dateCreated
Installing MongoDB How to install MongoDB on Ubuntu 22.04 true 2023-06-28T12:09:18.672Z linux, server, ubuntu, mongodb markdown 2023-06-28T12:09:18.672Z

Installing MongoDB

This guide provides instructions on how to install MongoDB on Ubuntu 22.04. Installing Mongo is a fairly simple process.

Updating the System

  • We will start by updating the apt package list. Run the following command:
sudo apt update

This will update the apt package list so that we ensure that the latest versions of packages are available.

  • Next, we will upgrade the packages. Run the following command:
sudo apt upgrade

This will upgrade the packages to the latest versions.

Installing required packages

  • Now, we will install some packages that are required. Run the following command:
sudo apt install gnupg2 wget

Installing MongoDB

  1. Import the MongoDB GPG key. Run the following command:
wget -nc https://www.mongodb.org/static/pgp/server-6.0.asc
cat server-6.0.asc | gpg --dearmor | sudo tee /etc/apt/keyrings/mongodb.gpg >/dev/null

These commands will download the GPG key and import it, which will allow us to verify the packages.

  1. Add MongoDB to the apt sources list. Run the following command:
sudo sh -c 'echo "deb [ arch=amd64,arm64 signed-by=/etc/apt/keyrings/mongodb.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" >> /etc/apt/sources.list.d/mongo.list'

This command will add the MongoDB apt repository to the sources list.

  1. Update the apt package list. Run the following command:
sudo apt update

This will update the apt package list so that we ensure that the latest versions of packages are available.

  1. Install MongoDB. Run the following command:
sudo apt install mongodb-org

This will install the latest stable version of MongoDB.

  1. Start MongoDB. Run the following command:
sudo systemctl start mongod
  1. Enable MongoDB so that it will automatically start on system boot. Run the following command:
sudo systemctl enable mongod
  1. Verify that MongoDB is running. Run the following command:
sudo systemctl status mongod

You should see something like this:

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-06-27 08:00:00 UTC; 1min 30s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 1234 (mongod)
      Tasks: 33 (limit: 4915)
     Memory: 1.2G
     CGroup: /system.slice/mongod.service
             └─1234 /usr/bin/mongod --config /etc/mongod.conf

Securing MongoDB

  1. Connect to MongoDB, so that we can create an admin user. Run the following command:
mongo
  1. Switch to the admin database. Run the following command:
use admin
  1. Create an admin user. Run the following command:
db.createUser(
  {
    user: "admin",
    pwd: "SET ME", # Generate a random string with: openssl rand -base64 32 | cut -c1-32
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
  1. Exit the mongo shell. Run the following command:
exit
  1. Now, we need to secure enable authentication, this will secure the server and prevent unauthorized access. Run the following command:
sudo nano /etc/mongod.conf
  1. Find the security section. We need to uncomment it, and add the following line below it, it should look like this:
security:
  authorization: enabled
  1. Save the file and exit the editor. You can do this by pressing CTRL + X, then Y, then ENTER.

Optional: Allowing remote connections

  1. Open the MongoDB configuration file. Run the following command:
sudo nano /etc/mongod.conf
  1. Find the net section and replace the bindIp line with the following:
bindIp: 0.0.0.0
  1. Save the file and exit the editor. You can do this by pressing CTRL + X, then Y, then ENTER.

  2. Restart MongoDB. Run the following command:

sudo systemctl restart mongod

You should now be able to connect to MongoDB from a remote machine.

Finished

You have successfully installed MongoDB on Ubuntu 22.04. You can now connect to MongoDB and start using it.