Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: check connection to DB

Tags:

java

mongodb

I'm looking for best way to check connection to Mongo DB. Situation: client makes request (api) to server. And server returns status of all databases.

What the best way to do it?

like image 280
Lugaru Avatar asked Jun 19 '13 14:06

Lugaru


People also ask

How do I list connections in MongoDB?

For more info you can use this command sudo lsof | grep mongod | grep TCP . I need it when i did replication and primary node have many client connection greater than secondary. This shows that I currently have five connections open to the MongoDB port (27017) on my computer.

How do I monitor my MongoDB connections?

You can leverage tools like mongostat and mongotop. Use MongoDB's built-in free monitoring feature to get information on Operation Execution Times and Operation Counts. Once you connect via compass to your instance, you can use the MongoDB Compass Performance Tab, which is similar to Atlas RealTime Performance panel.


4 Answers

I use this:

Builder o = MongoClientOptions.builder().connectTimeout(3000);  
MongoClient mongo = new MongoClient(new ServerAddress("192.168.0.1", 3001), o.build());    

try {
  mongo.getAddress();
} catch (Exception e) {
  System.out.println("Mongo is down");
  mongo.close();
  return;
}
like image 199
rupweb Avatar answered Oct 01 '22 14:10

rupweb


The ping command is a no-op used to test whether a server is responding to commands. This command will return immediately even if the server is write-locked:

        try{

            DBObject ping = new BasicDBObject("ping", "1");
            mongoTemplate.getDb().getMongo().getDB("DATABASE NAME"").command(ping);
        } catch (Exception exp){
            // MongoDb is down..
        }
like image 37
gifpif Avatar answered Oct 01 '22 14:10

gifpif


Use MongoClient for Java, all the info you need is here...

http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/

like image 22
raffian Avatar answered Oct 01 '22 16:10

raffian


In Java MongoDriver 3.3.0 use ServerMonitorListener to determine whether server is up and connected or not. Here is the example code,

public class ServerConnection implements ServerMonitorListener {
    private MongoClient client;

    public ServerConnection(){
        try {
            MongoClientOptions clientOptions = new MongoClientOptions.Builder()
                .addServerMonitorListener(this)
                .build();

            client = new MongoClient(new ServerAddress("localhost", 27017), clientOptions);
        } catch (Exception ex) {

        }
    }

    @Override
    public void serverHearbeatStarted(ServerHeartbeatStartedEvent serverHeartbeatStartedEvent) {
        // Ping Started
    }

    @Override
    public void serverHeartbeatSucceeded(ServerHeartbeatSucceededEvent serverHeartbeatSucceededEvent) {
        // Ping Succeed, Connected to server
    }

    @Override
    public void serverHeartbeatFailed(ServerHeartbeatFailedEvent serverHeartbeatFailedEvent) {
        // Ping failed, server down or connection lost
    }
}
like image 34
Md Samiul Alim Sakib Avatar answered Oct 01 '22 16:10

Md Samiul Alim Sakib