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 -SyuHow 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-binThis command will download and build the MongoDB package. After the installation is complete, you should see the MongoDB version by running:
mongod --versionHow 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/mongodbNext, create a new configuration file /etc/mongodb.conf using your preferred text editor, such as Vim or Nano:
sudo nano /etc/mongodb.confAdd 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: 27017Save 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/mongodbFinally, enable and start the MongoDB service:
sudo systemctl enable --now mongodbManaging MongoDB on Arch Linux
To check the status of MongoDB, run:
sudo systemctl status mongodbYou can also stop, start, and restart the MongoDB service using:
sudo systemctl stop mongodb
sudo systemctl start mongodb
sudo systemctl restart mongodbFor additional MongoDB management tasks, use the mongo command-line interface. To start the mongo shell, simply run:
mongoSecuring MongoDB on Arch Linux
It’s crucial to prevent unauthorized access. Follow these steps to set up authentication:
- Start the mongoshell by running:
mongo- Switch to the admindatabase:
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 mongoshell by typingexit.
- Update the /etc/mongodb.confconfiguration file to enable authentication. Add the following line under thenetsection:
security:
  authorization: enabledSave and close the file.
- Restart the MongoDB service:
sudo systemctl restart mongodbFrom 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 adminConclusion
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:
 
			



