Hurry! 20% Off Ends SoonRegister Now

MongoDB Port - How to Change MongoDB Default Port

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://localhost:27017/myproject
       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
systemLog:
 destination: file
  path: "/mongodb/log/mongod.log"
  logAppend: true
storage:
  journal:
     enabled: true
processManagement:
  fork: true
net:
  bindIp: 127.0.0.1
  port: 20000
setParameter:
  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)
{ 
    if(err)
       console.log(err);
     else
     {
       console.log('Mongo Connected....');
     }
}

Checkout MongoDB Interview Questions

Example 2: C++

#include
#include
#include "mongo/client/dbclient.h"
void run() {
 mongo::DBClientConnection c;
 c.connect("localhost");
}
int main()
{
 try
{
   run();
   std::cout << "connected ok" << std::endl;
 } catch( const mongo::DBException &e )
{
   std::cout << "caught " << e.what() << std::endl;
 }
 return EXIT_SUCCESS;
}

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
MongoDB Training Dec 21 to Jan 05View Details
MongoDB Training Dec 24 to Jan 08View Details
MongoDB Training Dec 28 to Jan 12View Details
MongoDB Training Dec 31 to Jan 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 less