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 Training" This 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 port | Description |
27017 | The default port for mongod and mongos instances. |
27018 | The default port when running with --shardsvr runtime operation. |
27019 | The default port when running with --configsvr runtime operation. |
28017 | The 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
Subscribe to our youtube channel to get new updates..!
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.
Method 2
- Create a configuration file “mongod.conf”. The configuration file is in YAML format.
- Run with 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.
Connect to MongoDB
Please follow the steps to connect to the 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; }