Home  >  Blog  >   MongoDB

MongoDB Port - How to Change MongoDB Default Port

Rating: 5

The MongoDB protocol is a simple socket-based, request-response style protocol. Connection with the client and Database server happens through a regular TCP/IP socket.

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

If you want to enrich your career and become a professional in MongoDB, then visit Mindmajix - a global online training platform: "MongoDB TrainingThis course will help you to achieve excellence in this domain. 

List of default MongoDB ports

The following table lists the default TCP ports used by MongoDB

Default portDescription
27017The default port for mongod and mongos instances.
27018The default port when running with --shardsvr runtime operation.
27019The default port when running with --configsvr runtime operation.
28017The default port for the web status page.

Connection URL:

       mongodb:// is the protocol definition
       localhost:27017 is the server we are connecting to
               localhost is the address
               27017 is the default port
       /myproject is the database we wish to connect to

How to change MongoDB default port

You can also change the default MongoDB port from 27017 to any port of your choice (make sure that no other application is using that port).

Method 1

1. Start MongoDB server with – port argument
Open command prompt as administrator, navigate to MongoDB installation directory’s bin folder then type mongod --dbpath "C:Datadb" --port 20000
You will see waiting for a connection on port 20000

2. Open another instance of command prompt as administrator, navigate to MongoDB installation directory’s bin folder then type mongo --port 20000
The connection will be established.

 MindMajix YouTube Channel

Method 2

1. Create a configuration file “mongod.conf”. The configuration file is in YAML format.

2. Run with the flag --config location to a configuration file. e.g. mongod --config /etc/mongod.conf

Example configuration file
 destination: file
  path: "/mongodb/log/mongod.log"
  logAppend: true
     enabled: true
  fork: true
  port: 20000
  enableLocalhostAuthBypass: false

Notice that the port number changed to 20000.

On mongo shell type

db.runCommand({whatsmyuri : 1}).

It will show the IP and port number the server is listening to.

Checkout MongoDB Tutorial

Connect to MongoDB

Please follow the steps to connect to MongoDB.

1. Go to the MongoDB installation directory and navigate to the bin folder. Launch mongod.exe. You can also do this from a command prompt. This is the MongoDB server. You must have a Datadb directory defined for MongoDB.

2. Launch mongo.exe. This is the mongo client. You can do this from a command prompt but launch another instance. Please use run as administrator for both. Mongo.exe will open the MongoDB shell.

3. MongoDB also provides drivers for different programming languages.

Example 1: JavaScript

var mongo = require('mongodb');
var MongoClient = mongo.MongoClient;   
MongoClient.connect('mongodb://'dbusername':'dbpassword'@'dbhost':'dbport'/'dbname,function(err, db)
       console.log('Mongo Connected....');

Checkout MongoDB Interview Questions

Example 2: C++

#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;
Join our newsletter

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
MongoDB Training Apr 20 to May 05View Details
MongoDB Training Apr 23 to May 08View Details
MongoDB Training Apr 27 to May 12View Details
MongoDB Training Apr 30 to May 15View Details
Last updated: 03 Apr 2023
About Author

Prasanthi is an expert writer in MongoDB, and has written for various reputable online and print publications. At present, she is working for MindMajix, and writes content not only on MongoDB, but also on Sharepoint, Uipath, and AWS.

read more