MongoDB is popular database software capable of running on a variety of systems, including Linux. In this guide, we'll be taking you through the steps of installing MongoDB on Ubuntu Linux, as well as some basic configuration after it's up and running.

In this tutorial you will learn:
  • How to install MongoDB
  • How to control MongoDB (start, stop, etc)
  • How to connect to MongoDB remotely
  • How to change MongoDB default port
How to install MongoDB on Ubuntu Linux
How to install MongoDB on Ubuntu Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux
Software MongoDB
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Install MongoDB

Open a terminal and type the following commands to install MongoDB on Ubuntu.

$ sudo apt update
$ sudo apt install mongodb

When you're ready to start working with MongoDB, all you need to do is type the following command to launch the mongo shell.

$ mongo
Launch MongoDB
Launch MongoDB

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


How to control MongoDB

MongoDB can be controlled with the systemctl command. The commands you'll need to know are below.

Start or stop the MongoDB service:

$ sudo systemctl start mongodb
AND
$ sudo systemctl stop mongodb

Enable or disable MongoDB from starting auomatically at system boot:

$ sudo systemctl enable mongodb
AND
$ sudo systemctl disable mongodb

Check on the current status of MongoDB:

$ sudo systemctl status mongodb

Connect to MongoDB remotely

MongoDB is configured by default to only allow connections from localhost (where it's installed from). This is normal default behavior for database systems.



If you'd like to connect to MongoDB from a different system, you'll need to bind MongoDB to the IP address of an outward facing network interface. This configuration can be done by opening the following config file:

$ sudo nano /etc/mongodb.conf

Then, change the line bindIp: 127.0.0.1 to bindIp: 0.0.0.0. This will let MongoDB listen on all network interfaces. Once you've made the change, save and exit the file, then restart MongoDB for the changes to take effect.

$ sudo systemctl restart mongodb

In case you have a UFW firewall enabled you will also need to open TPC port 27017 to incoming traffic:

$ sudo ufw allow from any to any port 27017 proto tcp

To connect remotely, use the command:

$ mongo --host MONGODB-IP-OR-HOST:27017

Change MongoDB default port

You can change the default port for MongoDB by editing the /etc/mongodb.conf configuration file and updating the line that says port: 27017 to any desired port number.

Bind IP address and port number in the MongoDB configuration file
Bind IP address and port number in the MongoDB configuration file


Be sure to restart MongoDB once again for the changes to take effect.

$ sudo systemctl restart mongodb

Conclusion

In this tutorial, we saw how to install MongoDB on Ubuntu Linux. We also learned how to control the MongoDB service with systemctl commands. Lastly, we went over the configuration of MongoDB's bind address and default listening port. This should be enough to get your MongoDB up and running on Ubuntu, plus connectable from other network devices.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions