How to Install and Secure MongoDB on Ubuntu Server 22.04
This tutorial explains how to install and secure MongoDB on Ubuntu Server 22.04. Follow our step-by-step guide to set up MongoDB, a popular NoSQL database, on an EcoStack Cloud VPS for scalable and high-performance data storage.
mongodbdatabaseubuntunosql
Micro
VPS
1
vCPU
2 GB
Memory
20 GB
NVMe Disk
1024 GB
Traffic
4.90€
/month
* Up to 6GB RAM, 60GB NVMe Disk Space and 1Gbit/s Network Speed
#### Introduction
MongoDB is a leading NoSQL database known for its flexibility, scalability, and performance. It is widely used in modern web applications to handle unstructured data and large volumes of information efficiently. This tutorial will walk you through the installation and initial security setup of MongoDB on Ubuntu Server 22.04. Hosting MongoDB on an EcoStack Cloud VPS provides a robust platform for your database needs.
#### Requirements
- A VPS running Ubuntu Server 22.04. EcoStack Cloud offers reliable VPS hosting with easy SSH access.
- Basic knowledge of SSH and terminal commands.
#### Steps to Install and Secure MongoDB on Ubuntu Server 22.04
##### Step 1: Connect to Your VPS
Begin by connecting to your EcoStack Cloud VPS using SSH. Replace `your-username` and `your-vps-ip` with your actual username and VPS IP address.
```bash
ssh your-username@your-vps-ip
```
##### Step 2: Import the MongoDB Public Key
To ensure that the packages are authentic, import MongoDB’s public GPG key:
```bash
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
```
##### Step 3: Add MongoDB Repository
Add the MongoDB repository to your system's package source list:
```bash
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
```
##### Step 4: Update the Package Index
Refresh your package database to include the MongoDB repository:
```bash
sudo apt update
```
##### Step 5: Install MongoDB
Install the MongoDB packages:
```bash
sudo apt install -y mongodb-org
```
##### Step 6: Start and Enable MongoDB
Start the MongoDB service and ensure it starts on boot:
```bash
sudo systemctl start mongod
sudo systemctl enable mongod
```
Check the status of MongoDB to confirm it’s running:
```bash
sudo systemctl status mongod
```
##### Step 7: Secure MongoDB
By default, MongoDB is configured to allow connections only from localhost. For additional security, you should enable authentication and create an administrative user.
- **Edit MongoDB configuration file**:
```bash
sudo nano /etc/mongod.conf
```
- **Uncomment the `security` section** and enable authorization by adding the following lines:
```yaml
security:
authorization: enabled
```
- **Restart MongoDB** to apply the changes:
```bash
sudo systemctl restart mongod
```
- **Access the MongoDB shell** to create an admin user:
```bash
mongo
```
Switch to the `admin` database:
```javascript
use admin
```
Create a new administrative user. Replace `admin` and `strongpassword` with your desired username and password:
```javascript
db.createUser({
user: "admin",
pwd: "strongpassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
```
- **Exit the MongoDB shell**:
```javascript
exit
```
- **Verify that authentication is working** by connecting to MongoDB with the new admin user:
```bash
mongo -u admin -p --authenticationDatabase admin
```
Enter your password when prompted. You should see the MongoDB shell prompt if authentication is successful.
##### Step 8: Enable Remote Access (Optional)
If you need to allow remote access to MongoDB, edit the MongoDB configuration file:
```bash
sudo nano /etc/mongod.conf
```
- **Bind MongoDB to all IP addresses** by changing the `bindIp` option under the `net` section. Replace `127.0.0.1` with `0.0.0.0`:
```yaml
net:
bindIp: 0.0.0.0
```
- **Restart MongoDB** to apply the changes:
```bash
sudo systemctl restart mongod
```
Ensure that your firewall is configured to allow incoming connections on MongoDB’s default port (27017). With `ufw`, you can allow specific IP addresses:
```bash
sudo ufw allow from to any port 27017
```
Replace `` with the IP address you want to allow.
##### Step 9: Test MongoDB Connection
Test your MongoDB setup by connecting from a remote machine. Use a MongoDB client like `mongo` or any GUI client that supports MongoDB. Replace `your-vps-ip` with your server’s IP address:
```bash
mongo -u admin -p --authenticationDatabase admin --host your-vps-ip --port 27017
```
Enter your password when prompted. You should be able to connect to your MongoDB server.
##### Step 10: Backup and Restore MongoDB Databases (Optional)
To back up your MongoDB databases, use the `mongodump` utility:
```bash
mongodump --out /path/to/backup
```
To restore a MongoDB database from a backup, use the `mongorestore` utility:
```bash
mongorestore /path/to/backup
```
#### Conclusion
You have successfully installed and secured MongoDB on Ubuntu Server 22.04. Deploying MongoDB on an EcoStack Cloud VPS provides a high-performance and scalable solution for your data management needs. Continue exploring MongoDB’s rich feature set to fully leverage its capabilities.
#### Additional Resources
- [Official MongoDB Documentation](https://docs.mongodb.com/)
---