YouTube Icon

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

731ab9ad4a64dbf8ddbd178c87a0588a.png 28 Mar, 2024

Top Advice for Beginner Go Programmers

A companion on LinkedIn inquired me for pointers for a companion of theirs setting out on a travel to learn Go. Since my list of pointers distant s...

7a6d6b55c7448c4c5d2d9930e7d2c134.jpg 25 Oct, 2023

Top 5+ 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...

6fffb1d49ee894fe09ff88fb1267a3ea.png 22 Sep, 2023

Top 3 things the best senior developers do

Working under the care of a more educated inventor can make or break a inferior inventor's career. Then are 3 effects the stylish elderly inven...

CFG