News & Blogs

How to Check Connection to MongoDB

fluid
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 :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
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();
status.connections
{
    "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, builder.build());

attempt {
    System.out.println("MongoDB Server is Up:- "+mongoClient.getAddress());
    System.out.println(mongoClient.getConnectPoint());
    System.out.println(db.getStats());
} get (Exception e) {
    System.out.println("MongoDB Server is Down");
} finally{
    mongoClient.close();
}
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

a344363fbce62447dd6a9352567c8b1e.png 19 May, 2022

The Top 12 Node.js Frameworks on GitHub?

These are the main 12 Node.js systems on GitHub? at the hour of composing. I have almost certainly that these numbers will increment yet then you c...

05606b3ad79ccaff72f417faa85feb48.jpeg 17 May, 2022

Do I have to code in my free time in order to b...

In programming interviews, work competitors are some of the time requested what sorts from side activities they work on in their extra time. That&#...

947fe29497f53380c9230a8a97e2ce41.jpeg 30 Apr, 2022

How to Become an RPA Developer in 2022

Robotic Process Automation or RPA innovation has changed the fields of information investigation, quality affirmation, and testing. As a RPA engine...

CFG