5.2 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
.
Adding a user / database
- Connect to MongoDB, so that we can create a user. Run the following command:
mongo -u admin -p --authenticationDatabase admin
This will ask you for the password that you set earlier in the Securing MongoDB section.
- Switch to the database that you want to add a user to eg:
forum
. Run the following command:
use forum
- Create a user. Make sure to change the
db
to match the database used above. Run the following command:
db.createUser(
{
user: "forum",
pwd: "SET ME", # Generate a random string with: openssl rand -base64 32 | cut -c1-32
roles: [ { role: "readWrite", db: "forum" } ]
}
)
- Exit the mongo shell. Run the following command:
exit
You have now created a user for the forum
database. You can repeat this process for any other databases that you want to create users for.
Optional: Allowing remote connections
- Open the MongoDB configuration file. Run the following command:
sudo nano /etc/mongod.conf
- Find the
net
section, we need to update thebindIp
value, it should look like this:
net:
port: 27017
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.