News & Blogs

How to Check Connection to MongoDB

How to Check Connection to MongoDB11 Apr, 2022

How to Check Connection to MongoDB

1. Overview
In this instructional exercise, we'll figure out how to really take a look at the association with MongoDB.

Critically to interface with a solitary MongoDB example, we really want to determine the URI of the MongoDB case.

2. Checking Connection Using Mongo Shell
In this segment, we'll associate with the MongoDB server utilizing the mongo shell order. We'll investigate various instances of interfacing with MongoDB.

2.1. Checking Connection on the Default Port
Of course, MongoDB runs on port 27017, yet we can likewise run it on another port. We can interface with the MongoDB server utilizing the basic mongo order:

$ mongo
MongoDB shell variant v4.4.2
interfacing with: mongodb://localhost:27017/?compressors=disabled&gssapiServiceName=mongodb
Implied meeting: meeting { "id" : UUID("b7f80a0c-c7b9-4aea-b34c-605b85e601dd") }
MongoDB server adaptation: 4.0.1-rc0-2-g54f1582fc6
In the order above, as a matter of course, MongoDB expected to be the port as 27017. Assuming that the MongoDB server is down, we get the accompanying mistake:

$ mongo - - have localhost - - port 27017 administrator
MongoDB shell form v4.4.2
interfacing with: mongodb://localhost:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Blunder: couldn't interface with server localhost:27017, association endeavor fizzled:
  SocketException: Error interfacing with localhost:27017 :: brought about by :: Connection rejected :
exemption: interface fizzled
leaving with code 1
For this situation, we got the mistake since we were unable to interface with the server.

2.2. Actually taking a look at Connection on Secured MongoDB Database
MongoDB can be safeguarded with validation. All things considered, we want to pass the username and secret phrase in the order:

$ mongo mongodb://baeldung:baeldung@localhost:27017
Here we utilized the username "baeldung" and the secret word "baeldung" to interface with the MongoDB running on localhost.

2.3. Actually looking at Connection on Custom Port
We can likewise run MongoDB on a custom port. We should simply make changes in the mongod.conf record. Assuming the MongoDB is running on another port, we want to give that port in the order:

$ mongo mongodb://localhost:27001
Here, in the mongo shell, we can likewise really look at the right now dynamic associations of the information base server.

var status = db.serverStatus();
    "current" : 21,
    "accessible" : 15979
The serverStatus returns an archive that gives an outline of the ongoing status of the information base interaction. Routinely running the serverStatus order will gather insights about the MongoDB occasion.

3. Checking Connection Using Java Driver Code
Up to this point, we have figured out how to check the association with MongoDB utilizing the shell. Presently we should investigate a similar utilizing Java driver code:

MongoClientOptions.Builder developer = MongoClientOptions.builder();
// developer settings
ServerAddress = new ServerAddress("localhost", 27017);
mongoClient = new MongoClient(ServerAddress,;

attempt {
    System.out.println("MongoDB Server is Up:- "+mongoClient.getAddress());
} get (Exception e) {
    System.out.println("MongoDB Server is Down");
} finally{
In the above code, we previously made the MongoClientOption manufacturer to tweak the arrangements of the MongoClient network, then, at that point, made the MongoClient association utilizing the server address. Assume the MongoDB server is running on the 27017 port of the localhost. Any other way, the MongoClient will toss a mistake.

4. Conclusion
In this instructional exercise, we figured out how to check the association of the MongoDB server with various constant cases.

In the first place, we checked the association with the mongo default order, then, at that point, utilized the confirmed order and furthermore associated with the MongoDB server running on a redid port. Then, we really look at the association for both the MongoDB shell and Java driver code.

Have A Look Creative News

eee75a56bbd063ce982fd16402073720.jpeg 22 Feb, 2023

Top 7 Tools for Faster Development in React

Reply is a protean and flexible development library that can be used to produce everything from massive Single- runner operations to compact draw- ...

35e1eacdabb102d23e24941f9dc4708e.jpeg 20 Feb, 2023

Tools For Remote Developers

Are you a inventor that has made the trendy shift from the office to remote work? Or are you looking to conceivably work from home via a new positi...

7c0c416eb15c6d7aa08b4901e0d23bfa.jpg 04 Feb, 2023

Top 5 Habits for a Kickass Career as a Software...

Want to come a rockstar software inventor? In this composition, I'll reveal the habits that separate successful tech professionals from the res...