Created
August 1, 2019 21:15
-
-
Save igoralves1/bc89e67c3dee9574325e272d37245273 to your computer and use it in GitHub Desktop.
Install MongoDB on Ubuntu 18.04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-18-04 | |
How to Install MongoDB on Ubuntu 18.04 | |
Step 1 — Installing MongoDB | |
sudo apt update | |
sudo apt install -y mongodb | |
Step 2 — Checking the Service and Database | |
sudo systemctl status mongodb | |
or | |
mongo --eval 'db.runCommand({ connectionStatus: 1 })' | |
Step 3 — Managing the MongoDB Service | |
To verify the status of the service, type: | |
sudo systemctl status mongodb | |
You can stop the server anytime by typing: | |
sudo systemctl stop mongodb | |
To start the server when it is stopped, type: | |
sudo systemctl start mongodb | |
You can also restart the server with a single command: | |
sudo systemctl restart mongodb | |
By default, MongoDB is configured to start automatically with the server. If you wish to disable the automatic startup, type: | |
sudo systemctl disable mongodb | |
It's just as easy to enable it again. To do this, use: | |
sudo systemctl enable mongodb | |
Next, let's adjust the firewall settings for our MongoDB installation. | |
Step 4 — Adjusting the Firewall (Optional) | |
Assuming you have followed the initial server setup tutorial instructions to enable the firewall on your server, the MongoDB server will be inaccessible from the internet. | |
If you intend to use the MongoDB server only locally with applications running on the same server, this is the recommended and secure setting. However, if you would like to be able to connect to your MongoDB server from the internet, you have to allow the incoming connections in ufw. | |
To allow access to MongoDB on its default port 27017 from everywhere, you could use sudo ufw allow 27017. However, enabling internet access to MongoDB server on a default installation gives anyone unrestricted access to the database server and its data. | |
In most cases, MongoDB should be accessed only from certain trusted locations, such as another server hosting an application. To accomplish this task, you can allow access on MongoDB's default port while specifying the IP address of another server that will be explicitly allowed to connect: | |
sudo ufw allow from your_other_server_ip/32 to any port 27017 | |
You can verify the change in firewall settings with ufw: | |
sudo ufw status | |
You should see traffic to port 27017 allowed in the output: | |
Output | |
Status: active | |
To Action From | |
-- ------ ---- | |
OpenSSH ALLOW Anywhere | |
27017 ALLOW Anywhere | |
OpenSSH (v6) ALLOW Anywhere (v6) | |
27017 (v6) ALLOW Anywhere (v6) | |
If you have decided to allow only a certain IP address to connect to MongoDB server, the IP address of the allowed location will be listed instead of Anywhere in the output. | |
You can find more advanced firewall settings for restricting access to services in UFW Essentials: Common Firewall Rules and Commands. | |
Even though the port is open, MongoDB is currently only listening on the local address 127.0.0.1. To allow remote connections, add your server's publicly-routable IP address to the mongod.conf file. | |
Open the MongoDB configuration file in your editor: | |
sudo nano /etc/mongodb.conf | |
Add your server's IP address to the bindIP value: | |
... | |
logappend=true | |
bind_ip = 127.0.0.1,your_server_ip | |
#port = 27017 | |
... | |
Be sure to place a comma between the existing IP address and the one you added. | |
Save the file, exit the editor, and restart MongoDB: | |
sudo systemctl restart mongodb | |
MongoDB is now listening for remote connections, but anyone can access it. Follow Part 2 of How to Install and Secure MongoDB on Ubuntu 16.04 to add an administrative user and lock things down further. | |
Conclusion | |
You can find more in-depth tutorials on how to configure and use MongoDB in these DigitalOcean community articles. The official MongoDB documentation is also a great resource on the possibilities that MongoDB provides. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment