MongoDB is a highly scalable, flexible, and powerful NoSQL database that provides high performance and availability for various applications. In this guide, we’ll walk you through the process of how to install and configure MongoDB on your Arch Linux system.
Table of Contents:
- Prerequisites
- Installing MongoDB
- Configuring MongoDB
- Managing MongoDB
- Securing MongoDB
- Conclusion
How to Install and configure MongoDB on Arch Linux
Prerequisites
Before we proceed, make sure you have the following:
- A running Arch Linux system
- Administrative (root) access or a user account with sudo privileges
- A working internet connection
Also, ensure your system is up-to-date by running the following command:
sudo pacman -Syu
How to Install MongoDB on Arch Linux
MongoDB is available in the Arch User Repository (AUR), so we’ll need to use an AUR helper like yay
or paru
. In this example, we’ll use yay
. If you don’t have yay
installed, you can install it by following the instructions here.
To install MongoDB using yay
, run the following command:
yay -S mongodb-bin
This command will download and build the MongoDB package. After the installation is complete, you should see the MongoDB version by running:
mongod --version
How to Configure MongoDB on Arch Linux
Now that MongoDB is installed, we need to configure it. First, create a new directory to store MongoDB data:
sudo mkdir -p /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
Next, create a new configuration file /etc/mongodb.conf
using your preferred text editor, such as Vim or Nano:
sudo nano /etc/mongodb.conf
Add the following configuration:
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
bindIp: 127.0.0.1
port: 27017
Save and close the file. Next, create a new directory for MongoDB logs and set the appropriate permissions:
sudo mkdir -p /var/log/mongodb
sudo chown -R mongodb:mongodb /var/log/mongodb
Finally, enable and start the MongoDB service:
sudo systemctl enable --now mongodb
Managing MongoDB on Arch Linux
To check the status of MongoDB, run:
sudo systemctl status mongodb
You can also stop, start, and restart the MongoDB service using:
sudo systemctl stop mongodb
sudo systemctl start mongodb
sudo systemctl restart mongodb
For additional MongoDB management tasks, use the mongo
command-line interface. To start the mongo
shell, simply run:
mongo
Securing MongoDB on Arch Linux
It’s crucial to prevent unauthorized access. Follow these steps to set up authentication:
- Start the
mongo
shell by running:
mongo
- Switch to the
admin
database:
use admin
- Create an administrative user:
Replace {username}
and {password}
with the desired username and password.
db.createUser(
{
user: "{username}",
pwd: "{password}",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
- Exit the
mongo
shell by typingexit
. - Update the
/etc/mongodb.conf
configuration file to enable authentication. Add the following line under thenet
section:
security:
authorization: enabled
Save and close the file.
- Restart the MongoDB service:
sudo systemctl restart mongodb
From now on, you’ll need to authenticate when connecting to the mongo
shell. To do so, use the following command, replacing {username}
and {password}
with your chosen credentials:
mongo -u {username} -p {password} --authenticationDatabase admin
Conclusion
Congratulations! You have successfully installed and configured MongoDB on your Arch Linux system. You’ve also taken the necessary steps to secure your MongoDB installation. To learn more about MongoDB and its various features, visit the official MongoDB documentation.
While you’re here, you might also be interested in other tutorials related to Arch Linux: