NoSQL Top

Build a Dashboard Using Cassandra, Astra, and Stargate:

>> CHECK OUT THE ARTICLE
Persistence top

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll learn to check the connection with MongoDB.

Importantly to connect to a single MongoDB instance, we need to specify the URI of the MongoDB instance.

2. Checking Connection Using Mongo Shell

In this section, we'll connect to the MongoDB server using the mongo shell command. We'll explore different cases of connecting to MongoDB.

2.1. Checking Connection on the Default Port

By default, MongoDB runs on port 27017, but we can also run it on some other port. We can connect to the MongoDB server using the simple mongo command:

$ mongo
MongoDB shell version v4.4.2
connecting to: mongodb://localhost:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b7f80a0c-c7b9-4aea-b34c-605b85e601dd") }
MongoDB server version: 4.0.1-rc0-2-g54f1582fc6

In the command above, by default, MongoDB assumed the port as 27017. If the MongoDB server is down, we get the following error:

$ mongo --host localhost --port 27017 admin
MongoDB shell version v4.4.2
connecting to: mongodb://localhost:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server localhost:27017, connection attempt failed:
  SocketException: Error connecting to localhost:27017 :: caused by :: Connection refused :
[email protected]/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

In this case, we got the error because we couldn't connect to the server.

2.2. Checking Connection on Secured MongoDB Database

MongoDB can be protected with authentication. In that case, we need to pass the username and password in the command:

$ mongo mongodb://baeldung:[email protected]:27017

Here we used the username “baeldung” and the password “baeldung” to connect to the MongoDB running on localhost.

2.3. Checking Connection on Custom Port

We can also run MongoDB on a custom port. All we need to do is make changes in the mongod.conf file. If the MongoDB is running on some other port, we need to provide that port in the command:

$ mongo mongodb://localhost:27001

Here, in the mongo shell, we can also check the currently active connections of the database server.

var status = db.serverStatus();
status.connections
{
    "current" : 21,
    "available" : 15979
}

The serverStatus returns a document that gives an overview of the current status of the database process. Regularly running the serverStatus command will collect statistics about the MongoDB instance.

3. Checking Connection Using Java Driver Code

So far, we have learned to check the connection with MongoDB using the shell. Now let's look into the same using Java driver code:

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

try {
    System.out.println("MongoDB Server is Up:- "+mongoClient.getAddress());
    System.out.println(mongoClient.getConnectPoint());
    System.out.println(db.getStats());
} catch (Exception e) {
    System.out.println("MongoDB Server is Down");
} finally{
    mongoClient.close();
}

In the above code, we first created the MongoClientOption builder to customize the configurations of the MongoClient connectivity, then created the MongoClient connection using the server address. Suppose the MongoDB server is running on the 27017 port of the localhost. Otherwise, the MongoClient will throw an error.

4. Conclusion

In this tutorial, we learned to check the connection of the MongoDB server with different real-time cases.

First, we checked the connection with the mongo default command, then used the authenticated command and also connected to the MongoDB server running on a customized port. Next, we checked the connection for both the MongoDB shell and Java driver code.

The example of all the cases can be found over on GitHub.

NoSql Bottom

Build a Dashboard Using Cassandra, Astra, and Stargate

>> CHECK OUT THE ARTICLE
Persistence bottom
Get started with Spring Data JPA through the reference Learn Spring Data JPA course: >> CHECK OUT THE COURSE
Persistence footer banner
Comments are closed on this article!