MongoDB Default Port

MongoDB Default Port

MongoDB has a simple structure since it is a request-response style protocol. In MongoDB, the connection between the client and server occurs through a regular TCP/IP socket. TCP works as a transport layer protocol in MongoDB and its default connection port is 27017. But if you want you can change this default port, and this blog is exactly for that. Here, we will show you how to change the MongoDB default port in Windows and Ubuntu. But please note that along with the default port, the HTTP status interface port will also be changed.

What is MongoDB?

Built out of NoSQL MongoDB is an open-sourced database management system. NoSQL is not like the traditional database management system that won’t allow you to manage a large set of data. But with MongoDB, you can control document-oriented information as well as retrieve or store them in a safe place. You can use it for load balancing, indexing of data, server-side JavaScript execution, data aggregation, and many other functions that you may require to manage an organizational database.

In MongoDB, the data or records are based on documents that involve a proper structure with values and basic units. These documents are like JavaScript Object Notation but in MongoDB, we call it Binary JSON (BSON) that supports multiple types of data. The entries in these files contain columns that define the value of the documents. The various data types include documents, a group of documents, etc. These documents have unique keys that work as identifiers. In addition to the unique or primary keys in MongoDB, it has different port numbers that allow you to manage the instances in MongoDB.

Cheap SSL

What are the Default Ports in MongoDB?

Here are the default ports in MongoDB:

  • 27017: the port for MongoDB and mongos instances
  • 27018: the port for running the –shardsvr runtime operation
  • 27019: the port for running the –configsvr runtime operation
  • 28017: this is the default port for the web status page.

But you can change these default ports whenever you want. You just have to follow some simple instructions.

How to Change MongoDB Default Port in Linux/Ubuntu?

This method should work on all Linux distros that include Ubuntu and CentOS. But to change the default port in MongoDB, you need to configure the main configuration file that is known as “/etc/mongod.conf”. Open the /etc/mongod.conf file and look for the line that says port: 27017 and then change the value of the port number like this:

port: 20000

Then restart the MongoDB service:

sudo systemctl restart mongod.service

If you are running CentOS and enabled Selinux, then you should configure that first before changing the default port:

sudo semanage port -a -t mongod_port_t -p tcp 20000

But this step is for Linux users only and Windows users need to follow a different step.

How to Change MongoDB Default Port in Windows?

You will have to add a new port number to the primary config file in the net configuration option. Here is a mongod.cfg file example:


destination: file

path: c:\mongo\logs\mongod.log


dbPath: c:\mongo\data



serviceName: “MongoDB”

displayName: “Mongo DB”

description: “mongod service”


port: 20000

When you are connecting with the shell from MongoDB, the Mongo command uses the default port 27017. But by changing the default port, you will have to use the –port option in the mongo command:

mongo –port 20000

Now after changing the port, you should try to connect to MongoDB.

How to Connect to MongoDB?

Here are the steps you can follow to connect to MongoDB:

  1. Open the MongoDB installation directory and find the bin folder. Open mongod.exe which you can also do from the command prompt. But mongod.exe is a MongoDB server and you need to have a database available for MongoDB.
  2. Launch the mongod.exe as administrator and it will open as a MongoDB shell.

The steps are simple and easy to perform. Also, note that MongoDB offers different drivers for different programming languages. For example, if you are using JavaScript, it will be:

ar mongo = require(‘mongodb’);

var MongoClient = mongo.MongoClient;

MongoClient.connect(‘mongodb://’dbusername’:’dbpassword’@’dbhost’:’dbport’/’dbname,function(err, db)






console.log(‘Mongo Connected….’);



If you are using C++, the drivers will be:



#include “mongo/client/dbclient.h”

void run() {

mongo::DBClientConnection c;



int main()





std::cout << “connected ok” << std::endl;

} catch( const mongo::DBException &e )


std::cout << “caught ” << e.what() << std::endl;




There are other ways that would allow you to modify the MongoDB port that the database management system uses for a certain kind of task. Before you change a port number to a port number of your choice, you need to make sure that no other app is using the same port that you are trying to change. If you are sure that the port number is ready to change, you can follow the methods below to modify the number.

First method:

In the first method, you will initiate the server by stating the port argument in the statement while running the command. And to do that, you should open the command prompt with administrator privileges and roles. After that, go to the bin folder available inside the installation directory. We have used the command mongod –dbpath “C:Datadb” –port 20000 that will initiate the MongoDB server with any of your custom port.

Second method:

The second method is about modifying the config file as well as the port address of Mongo. Create the YAML file first and name it “mongod. conf” and run the file with the specified value of a location where the file is or the local port number in the mongod –config /etc/mongod.conf command.


When you are changing the default ports of MongoDB, make sure you know the purposes of each port. Otherwise, you will run the risk of using the different ports for different purposes and it might cause technical errors in the MongoDB interface. If you need any help with MongoDB default ports, do ask us in the comment section below.


Please enter your comment!
Please enter your name here