Introduction
MongoDB is a popular NoSQL database known for its flexibility and scalability. This guide provides detailed instructions on how to install MongoDB 4 on an Ubuntu 22.04 system.
Prerequisites
Before you begin, ensure you have the following:
- An Ubuntu 22.04 system with root or sudo access.
- Basic knowledge of terminal commands.
Step 1: Import the MongoDB GPG Key
First, import the MongoDB public GPG key used for package signing:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
Step 2: Create a List File for MongoDB
Next, create the /etc/apt/sources.list.d/mongodb-org-4.4.list
file to add the MongoDB repository:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Step 3: Update the Package Database
Refresh your package database with the newly added MongoDB repository:
sudo apt update
Step 4: Install MongoDB Packages
Now, install the MongoDB packages:
sudo apt install -y mongodb-org
Step 5: Start and Enable MongoDB
After the installation, start the MongoDB service and enable it to start on boot:
sudo systemctl start mongod
sudo systemctl enable mongod
Step 6: Verify MongoDB Installation
Check the status of MongoDB to ensure it is running correctly:
sudo systemctl status mongod
You should see an output indicating that the MongoDB service is active and running.
Step 7: Secure MongoDB (Optional)
For added security, you may want to enable authentication and create an administrative user. Follow these steps:
- Edit the MongoDB configuration file:
sudo nano /etc/mongod.conf
- In the file, under
#security
, add the following lines:
security:
authorization: "enabled"
- Restart MongoDB to apply the changes:
sudo systemctl restart mongod
- Connect to the MongoDB instance:
mongo
- In the MongoDB shell, switch to the
admin
database and create an administrative user:
use admin
db.createUser(
{
user: "admin",
pwd: "secure_password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Replace secure_password
with a strong password.
Conclusion:
You have successfully installed MongoDB 4 on your Ubuntu 22.04 system. You can now start using MongoDB for your database needs. Remember to regularly update and secure your MongoDB instance to keep it running smoothly and securely.