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
- 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.
- 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.
- 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.
- Install MongoDB. Run the following command:
sudo apt install mongodb-org
This will install the latest stable version of MongoDB.
- Start MongoDB. Run the following command:
sudo systemctl start mongod
- Enable MongoDB so that it will automatically start on system boot. Run the following command:
sudo systemctl enable mongod
- 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
- Connect to MongoDB, so that we can create an admin user. Run the following command:
mongo
- Switch to the
admin
database. Run the following command:
use admin
- 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" } ]
}
)
- Exit the mongo shell. Run the following command:
exit
- 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
- Find the
security
section. We need to uncomment it, and add the following line below it, it should look like this:
security:
authorization: enabled
- Save the file and exit the editor. You can do this by pressing
CTRL + X
, thenY
, thenENTER
.
Optional: Allowing remote connections
- Open the MongoDB configuration file. Run the following command:
sudo nano /etc/mongod.conf
- Find the
net
section and replace thebindIp
line with the following:
bindIp: 0.0.0.0
-
Save the file and exit the editor. You can do this by pressing
CTRL + X
, thenY
, thenENTER
. -
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.