Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have more than 1 'mongos' instance?

Tags:

java

mongodb

I am using Java to insert data into mongodb cluster. Can I have more than 1 mongos instance so that I have a backup when 1 of my mongos is down?

Here is my java code to connect to mongos.

MongoClient mongoClient = new MongoClient("10.4.0.121",6001);
DB db = mongoClient.getDB("qbClientDB");
DBCollection collection = db.getCollection("clientInfo");

How can I specify my second mongos instance in my Java code? (If possible).

Thanks in advance.

like image 566
Krishnalal P Avatar asked Sep 29 '14 08:09

Krishnalal P


People also ask

What is mongos instance?

Synopsis. For a sharded cluster, the mongos instances provide the interface between the client applications and the sharded cluster. The mongos instances route queries and write operations to the shards. From the perspective of the application, a mongos instance behaves identically to any other MongoDB instance.

What is the main purpose of the mongos process?

The mongos process, shown in the center of figure 1, is a router that directs all reads, writes, and commands to the appropriate shard. In this way, mongos provides clients with a single point of contact with the cluster, which is what enables a sharded cluster to present the same interface as an unsharded one.

WHO identified that there is an unprotected MongoDB instance to be hosted by data aggregator on social site *?

Three students from University of Saarland in Germany at the Centre for IT Security – Kai Greshake, Eric Petryka and Jens Heyens – discovered that MongoDB databases running at TCP port 27017 as a service on several thousands of commercial web servers are easily accessible on the Internet.


2 Answers

The MongoClient docs say that you can, something similar to (the dummy addresses);

MongoClient mongoClient = new MongoClient(Arrays.asList(
   new ServerAddress("10.4.0.121",6001),
   new ServerAddress("10.4.0.122",6001),
   new ServerAddress("10.4.0.123",6001)));

MongoClient will auto-detect whether the servers are a list of replica set members or a list of mongos servers.

like image 190
Joachim Isaksson Avatar answered Oct 01 '22 11:10

Joachim Isaksson


   public MongoClient(List<ServerAddress> seeds,
               MongoClientOptions options)


//Creates a Mongo based on a list of replica set members or a list of mongos. It will find all members (the master will be used by default). If you pass in a single server in the list, the driver will still function as if it is a replica set. If you have a standalone server, use the Mongo(ServerAddress) constructor.

//If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to, and automatically fail over to the next server if the closest is down. 

  MongoClient mongoClient = new MongoClient(Arrays.asList(
  new ServerAddress("10.4.0.121",6001),
  new ServerAddress("10.4.0.122",6001),
  new ServerAddress("10.4.0.123",6001)));
like image 20
Akhil Suresh Avatar answered Oct 01 '22 12:10

Akhil Suresh