What port should i use for mongodb?

  • Reference >
  • Default MongoDB Port

The following table lists the default TCP ports used by MongoDB:

Default Port Description
27017 The default port for mongod and mongos instances. You can change this port with port or --port.
27018 The default port for mongod when running with --shardsvr command-line option or the shardsvr value for the clusterRole setting in a configuration file.
27019 The default port for mongod when running with --configsvr command-line option or the configsvr value for the clusterRole setting in a configuration file.
27020 The default port from which mongocryptd listens for messages. mongocryptd is installed with MongoDB Enterprise Server (version 4.2 and later) and supports automatic encryption operations.

Though this is - strictly speaking - not a programming related question and would rather belong on http://dba.stackexchange.com or on http://www.serverfault.com, I will answer it, since it might be useful for programmers.

Putting software on a nonstandard port does in no way enhance security from a conceptional point of view. This is because a determined attacker (I don't talk of script kiddies) will do a well hidden port scan on the machines of your network anyway. And since most servers identify who they are and what they do, it is quite easy for an attacker to determine which software is running on which open (aka reachable) ports of a machine. Actually, there are signature available, mapping the answers of various versions of the most important software (openssh, Apache httpd and MySQL) to known vulnerabilities of said versions of the software.

Putting a software on a nonstandard port for security reasons does not help. And it creates problems for the network and system administrators to configure the firewalls and system services correctly, resulting in increased maintenance work.

As @senfo correctly pointed out, you should use firewalls to restrict access. Measures offered by MongoDB to enhance security are authentication and authorization as well as keyfiles. In order to prevent sniffing attacks, you might want to use a tool like stunnel or recompile the MongoDB packages you plan to use with SSL support.

So, keep the ports where they belong to: at their defaults.

What port should i use for mongodb?

Introduction to MongoDB port

MongoDB port is the address where the protocol tries to establish the connection. There are some default ports supported by MongoDB such as 27017. However, we can also explicitly modify the default ports. MongoDB is a request-response kind of application that involves establishing a connection between its client and server involving multiple requests and responses which is based on sockets and the protocol used is TCP/ IP (Transport Layer Protocol) socket protocol. For exchanging the information, data, requests, and responses the sockets have their associated ports. Which port needs to be considered while communicating depends upon the internal configuration settings.

In this article, we will have a look at some of the default ports used by Mongo DB and also have a look at the process of changing the default ports.

List of default ports

Let us have a look at all the related default ports used by Mongo DB database management system. For this check out the following table –

Default Port Address Explanation
28017 While showing the status on the web pages this port address is considered by default.
27019 When we are performing a runtime operation in the system and while running the same, we are making the use of –configsvr option then this port address is considered as the default port.
27018 When we are performing a runtime operation in the system and while running the same, we are making the use of –shardsvr option then this port address is considered as the default port.
27017 For all the instances of Mongos and Mongod this port address is considered by default for establishing the communication.

Syntax:

Let us understand the syntax of the URL used for establishing the connection. Consider a sample URL for studying the various components present inside it –

mongodb://localhost:27017/educba

Let us discuss one by one all the components used in the above statement –

Mongodb:// – This is the definition of the protocol. Hence, when using the mongo db the protocol specification is given by using mongodb://

Localhost:27017c- It is the part that represents the address of the server with which we are trying to establish the connection.

Localhost – It is the address of the server

27017 – This is the port address that is considered for identification of socket by default when using MongoDB.
/educba – It is the name of the database to which we want to connect to.

Changing the default port address of MongoDB

There are plenty of ways using which we can modify the port which is being used by Mongo DB for a specific task. Before you go for modifying any of the default ports to a port of your choice, you need to make sure that there is no other application that is using the same port number you are trying to set the Mongo DB port to. Let us have a look at some of the methods to change the default port address one by one in the upcoming section –

First Method

We will have to begin the server of Mongo DB by specifying – port argument in its statement while firing the command. For this, we have to make sure that while opening the command prompt we open it with administrator privileges and role, and then we have to navigate to the bin folder present in Mongo DB installation directory. Further, you need to enter the command specified below –

mongod –dbpath “C:Datadb” –port 20000

After firing the above command, you will observe that the connection waiting message is displayed as shown below –

What port should i use for mongodb?

Further, the next step to perform is to open a new instance of the command prompt with the role and privileges of the administrator and then navigate to the bin folder of MongoDB installation directory and type the same command that is shown below –

Mongo --port 20000

The output of the above command will be as shown below which means that the mongo DB is now running on the specified port address 20000 instead of its default port address –

What port should i use for mongodb?

In this way, by using the above methodology and commands we can initiate the Mongo DB server at any custom port as per our requirement. Only the thing to be taken care of is that there should not be any service or application that is running on the port on which we are trying to establish the Mongo DB connection.

Second Method

In the second method, we will have to modify the configuration file of Mongo DB and modify the port address specified in the configuration details. For this, you will have to follow the below steps –

  • We will have to first create a file with YAML format which will act as the configuration file for our Mongo DB. We will give the name for this file as “mongod.conf”.
  • Next, you have to run the configuration file along with specifying the –config option value to a location of any of the configuration file to which you want it to refer. For example, if we have created mongod.conf file then we will specify the command as

mongod --config /etc/mongod.conf

The output of above command is as shown in the below image –

What port should i use for mongodb?

Let us have a look at one of the sample configuration files. For this refer to the below image. We can observe that the file contains the port specification as 20000 which was kept as per our requirement. You can change this port value by specifying the required port number in the commands used above.

What port should i use for mongodb?

We can now observe the process of changing the port is successfully completed.

By using either of the two methods, you can change the default port number of Mongo DB to whichever address you want. After this, your next step will be establishing the connection with Mongo DB.

Conclusion

The default port of the Mongo DB is already predefined and set for the Mongo and Mongod instances when using configsvr or shardvsr option while runtime operations or even when observing the status in web pages which are mentioned in the table. We can even modify the port number by using either of the two methods specified in the above article.

This is a guide to MongoDB port. Here we discuss the introduction, List of default ports with some methods respectively. You may also have a look at the following articles to learn more –

  1. MongoDB Skip()
  2. MongoDB Relationships
  3. MongoDB Users
  4. MongoDB Import

What ports does MongoDB use?

MongoDB uses TCP as its transport layer protocol. The predefined default port for connection is 27017.

Why is port 27017 used?

The port 27017 is used for mongoDB server, an important module for EAP Controller.

How do I connect to a MongoDB server?

How to connect to your remote MongoDB server.
Set up your user. First ssh into your server and enter the mongo shell by typing mongo . ... .
Enable auth and open MongoDB access up to all IPs. Edit your MongoDB config file. ... .
Open port 27017 on your EC2 instance. ... .
Last step: restart mongo daemon (mongod).

How do I access MongoDB?

To open up the MongoDB shell, run the mongo command from your server prompt. By default, the mongo command opens a shell connected to a locally-installed MongoDB instance running on port 27017 . Try running the mongo command with no additional parameters: mongo.