Updated March 8, 2023
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.|
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 –
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 –
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 –
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 –
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.
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 –
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.
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.
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 –