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

224 lines
5.4 KiB
Markdown

---
title: Installing MongoDB
description: How to install MongoDB on Ubuntu 22.04
published: true
date: 2023-06-28T12:09:18.672Z
tags: linux, server, ubuntu, mongodb, ubuntu 22.04
editor: markdown
dateCreated: 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:
```bash
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:
```bash
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:
```bash
sudo apt install gnupg2 wget
```
## Installing MongoDB
1. Import the MongoDB GPG key. Run the following command:
```bash
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.
2. Add MongoDB to the apt sources list. Run the following command:
```bash
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.
3. Update the apt package list. Run the following command:
```bash
sudo apt update
```
This will update the apt package list so that we ensure that the latest versions of packages are available.
4. Install MongoDB. Run the following command:
```bash
sudo apt install mongodb-org
```
This will install the latest stable version of MongoDB.
5. Start MongoDB. Run the following command:
```bash
sudo systemctl start mongod
```
6. Enable MongoDB so that it will automatically start on system boot. Run the following command:
```bash
sudo systemctl enable mongod
```
7. Verify that MongoDB is running. Run the following command:
```bash
sudo systemctl status mongod
```
You should see something like this:
```bash
● 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
```
## Finished
You have successfully installed MongoDB on Ubuntu 22.04. For user creation and securing MongoDB, please see the next sections.
## Optional: Securing MongoDB
1. Connect to MongoDB, so that we can create an admin user. Run the following command:
```bash
mongosh
```
2. Switch to the `admin` database. Run the following command:
```bash
use admin
```
3. Create an admin user. Run the following command:
```bash
db.createUser(
{
user: "admin",
pwd: "SET ME", # Generate a random string with: openssl rand -base64 32 | cut -c1-32
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
```
4. Exit the mongo shell. Run the following command:
```bash
exit
```
5. Now, we need to secure enable authentication, this will secure the server and prevent unauthorized access. Run the following command:
```bash
sudo nano /etc/mongod.conf
```
6. Find the `security` section. We need to uncomment it, and add the following line below it, it should look like this:
```bash
security:
authorization: enabled
```
7. Save the file and exit the editor. You can do this by pressing `CTRL + X`, then `Y`, then `ENTER`.
## Optional: Adding a user / database
1. Connect to MongoDB, so that we can create a user. Run the following command:
```bash
mongosh -u admin -p --authenticationDatabase admin
```
This will ask you for the password that you set earlier in the Securing MongoDB section, or if you didn't secure MongoDB, run the following command:
```bash
mongosh
```
2. Switch to the database that you want to add a user to eg: `forum`. Run the following command:
```bash
use forum
```
3. Create a user. Make sure to change the `db` to match the database used above. Run the following command:
```bash
db.createUser(
{
user: "forum",
pwd: "SET ME", # Generate a random string with: openssl rand -base64 32 | cut -c1-32
roles: [ { role: "readWrite", db: "forum" } ]
}
)
```
4. Exit the mongo shell. Run the following command:
```bash
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
1. Open the MongoDB configuration file. Run the following command:
```bash
sudo nano /etc/mongod.conf
```
2. Find the `net` section, we need to update the `bindIp` value, it should look like this:
```bash
net:
port: 27017
bindIp: 0.0.0.0
```
3. Save the file and exit the editor. You can do this by pressing `CTRL + X`, then `Y`, then `ENTER`.
4. Restart MongoDB. Run the following command:
```bash
sudo systemctl restart mongod
```
You should now be able to connect to MongoDB from a remote machine.