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.
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. |
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
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).
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.
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
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.
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.
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
#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;
}
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:
Name | Dates | |
---|---|---|
MongoDB Training | Dec 24 to Jan 08 | View Details |
MongoDB Training | Dec 28 to Jan 12 | View Details |
MongoDB Training | Dec 31 to Jan 15 | View Details |
MongoDB Training | Jan 04 to Jan 19 | View Details |
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.