Configuring Remote Access for MongoDB on Ubuntu and Windows
Written on
Chapter 1: Introduction to MongoDB Remote Access
MongoDB stands out as the leading NoSQL database, widely utilized for various modern web applications due to its document-oriented nature. By default, MongoDB restricts connections to the local server. If you aim to access it from a different server within a private network or over the internet, this guide will walk you through the necessary configurations.
To successfully enable remote access, we will cover the following steps:
- Configuring IP Binding
- Adjusting the Firewall
- Testing Remote Connectivity
Let's dive right in!
Prerequisites
You will need:
- A server running MongoDB on either Windows or Ubuntu with UFW configured.
- A secondary computer for remote access to your MongoDB instance.
Chapter 2: Configuring IP Binding
IP Binding allows MongoDB to listen exclusively to specific IP addresses. By default, it binds to localhost, limiting connections to the same server. To enable access from trusted IPs, we must modify the default configuration.
#### Ubuntu Configuration
The MongoDB configuration file on Ubuntu can be found at /etc/mongod.conf. Open this file using your preferred text editor, such as nano:
sudo nano /etc/mongod.conf
Locate the following section:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
Now, append a comma to 127.0.0.1 followed by your remote server's public or private IP address:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,192.168.1.33
You can also allow all IPv4 addresses by using 0.0.0.0:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,0.0.0.0
Finally, restart MongoDB with the following command:
sudo systemctl restart mongod
#### Windows Configuration
On Windows, the MongoDB configuration file is typically found at:
C:Program FilesMongoDBServer<version>binmongod.cfg
Open this file in your preferred text editor and find the same section:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
Append your remote server's IP address:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,192.168.1.33
To allow all IPv4 addresses, modify it to:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,0.0.0.0
Restart MongoDB by opening the command prompt as an administrator and executing:
net stop MongoDB
net start MongoDB
Chapter 3: Configuring the Firewall
With MongoDB now set to listen on the specified IP addresses, the next step is to adjust your firewall settings to permit incoming connections.
#### Ubuntu Firewall Configuration
To allow connections on the default port 27017, execute:
sudo ufw allow 27017
To restrict access to a specific trusted IP, use:
sudo ufw allow from <trusted_IP> to any port 27017
Verify the firewall status with:
sudo ufw status
You should see something like:
Status: active
To Action From
-- ------ ----
27017 ALLOW Anywhere
#### Windows Firewall Configuration
For Windows, follow these steps:
- Open Firewall & network protection.
- Navigate to Advanced settings.
- Select Inbound Rules and click on New Rule…
- Choose Port and proceed to the next step.
- Enter your MongoDB port number (default is 27017) and continue.
- Select Allow the connection and proceed.
- Choose the appropriate network type (Private for local, Public for internet) and continue.
- Name the rule and finish.
Chapter 4: Testing Remote Connectivity
Now that your MongoDB instance is accessible from a remote machine, it’s advisable to enhance security by establishing an administrative user and enabling authentication.
To test the connection, use the MongoDB Shell from a remote computer with the following command (adjust the port as necessary):
mongo "mongodb://<your_ip>:27017"
If you’ve set up authentication, use:
mongo "mongodb://username:password@<your_ip>:27017"
You should connect to your MongoDB instance without any issues.
If you found this guide helpful, feel free to leave your questions in the comments, and I’ll be glad to assist!
The first video provides a step-by-step process on enabling remote access to MongoDB, ideal for beginners looking to expand their database management skills.
The second video focuses on configuring remote access for MongoDB on Ubuntu 22.04 LTS, perfect for those using this platform.