Software & AppsOperating SystemLinux

How To Find the Port on Which MongoDB is Running?

Ubuntu 4

In this article, we will discuss various methods to determine the port on which MongoDB is running. MongoDB, a popular NoSQL database, typically runs on port 27017 by default. However, it can be configured to run on any port. Here’s how you can find out the exact port number.

Method 1: Checking the Default Port

By default, MongoDB is set to run on port 27017. If you haven’t made any changes to the MongoDB configuration after installation, you can connect to the MongoDB instance on this port. However, if the port has been changed or you’re unsure, you can use the following methods to find the port.

Method 2: Using the lsof Command

The lsof command in Linux stands for “list open files”. This command provides information about files that are opened by processes. Here’s how you can use it:

lsof -i | grep mongo

This command will list all open files related to the Internet that match the word “mongo”. The -i option is used to list Internet network files. If MongoDB is running, this will show you the ports that MongoDB is listening on.

Method 3: Using the netstat Command

The netstat command is a useful tool that displays network connections, routing tables, and a number of network interface statistics. You can use it as follows:

netstat -tulnp | grep 2701

This command will display all listening ports that match the specified port number (2701 in this case). The parameters -tulnp stand for TCP, UDP, listening, numeric, and program respectively. Make sure to run this command as root to see the process names associated with the ports.

Method 4: Checking the MongoDB Configuration File

The MongoDB configuration file (/etc/mongod.conf) contains settings that control the behavior of the MongoDB server. You can find the port setting in this file. Here’s how:

cat /etc/mongod.conf | grep port

This command will display the line in the configuration file that contains the word “port”. If the port has been set in the configuration file, this command will show it.

Method 5: Using the db.getCmdLineOpts() Command in the MongoDB Shell

You can connect to the MongoDB instance using the mongo shell and run the db.getCmdLineOpts() command. This command displays the command line options passed to the MongoDB server, including the port number. Here’s how:


The output of this command will include a section named “parsed”, which contains the configuration options including the port number.

By using these methods, you should be able to determine the port on which MongoDB is running. Understanding how to find this information can be crucial when troubleshooting connectivity issues or configuring applications to connect to MongoDB.

What is MongoDB?

MongoDB is a popular NoSQL database that provides a flexible and scalable solution for storing and retrieving data. It is designed to handle large amounts of unstructured data and is widely used in modern web applications.

How can I check if MongoDB is running on my system?

You can check if MongoDB is running on your system by using the ps command in Linux or the Task Manager in Windows. In Linux, you can run ps aux | grep mongod to see if the MongoDB process is running. In Windows, open the Task Manager and look for the "mongod" process in the list of running processes.

Can I change the default port on which MongoDB runs?

Yes, you can change the default port on which MongoDB runs. The port setting can be modified in the MongoDB configuration file (/etc/mongod.conf for Linux systems) by changing the value of the port parameter. After making the change, you need to restart the MongoDB service for the new port to take effect.

How can I connect to MongoDB using the `mongo` shell?

To connect to MongoDB using the mongo shell, open a terminal or command prompt and run the mongo command. This will start the MongoDB shell and connect to the default MongoDB instance running on port 27017. If you need to connect to a different MongoDB server or specify a different port, you can pass the appropriate options to the mongo command. For example, mongo --host myserver --port 12345 will connect to a MongoDB server running on the "myserver" host and port 12345.

Is it possible to run multiple MongoDB instances on the same system?

Yes, it is possible to run multiple MongoDB instances on the same system. Each instance needs to be configured with a different port number and data directory. By default, MongoDB instances use the /data/db directory for data storage. To run multiple instances, you can specify a different data directory for each instance using the --dbpath option when starting the MongoDB server. Additionally, you need to make sure that each instance is configured to listen on a different port in the MongoDB configuration file.

Leave a Comment

Your email address will not be published. Required fields are marked *