How to find primary host of MongoDB replica and access in Python - mongodb

I've one primary host as mongo1.ppshein.net, secondary host as mongo1.ppshein.net and arbiter as mongo3.ppshein.net, and configured MongoDB replica as above shown in AWS EC2. And in each of MongoDB config file, bindIP is as its host name and App Server host.
To access that MongoDB replica from python, I thought I could be able to use following code-snippet,
>>> from pymongo import MongoClient
>>> db = MongoClient('mongodb://serverA:27017, serverB:27017, serverC:27017/?replicaSet=foo').db_name
But problem is if serverA is down/unhealthy, I'm not sure whether above code-snippet would be working properly or not. That's why I'm curious to know how to get primary host of MongoDB instead of adding multiple hosts in connection string?

Once you connect to a replica set the driver will always reconnect you to the primary if one can be elected (i.e. you have a quorum of nodes). the only reason you give a list of nodes in the argument is to prevent a situation where you are attempting to connect to a node that is down. if that node was down you would get a server timeout.
The full specification for the server discovery and monitoring protocol is here.

Related

Does PL/Proxy send query to replica if master is not available?

I have sharded and replicated Postgre database. I use CLUSTER + RUN way of running functions. I define target (master/replica) using CLUSTER param and shard using RUN ON. How can I make PL/Proxy run or not to run function on master, if originally target was replica, but it failed?
In PL/Proxy, you define shards via a libpq connection string. Now if a shard is replicated, you can simply use a connection string like
host=slave.myorg.com,master.myorg.com port=5432,5432 dbname=...
This will try to connect to the first host, and if that fails, it will fall back to the second host. PostgreSQL v14 has the additional connection string parameter target_session_attrs=prefer-standby to preferable connect to the standby server, even if it is not the first in the list.

How to configure multi mongos instances access to a sharded cluster?

I couldn't find any clear document that explains how to config multi mongos instances in a single sharded cluster with numbers of shards. I want to have more mongos and assign them to numbers of application servers to reduce latency.
You access a sharded cluster from a client via a mongos router.
Your cluster configuration can have many mongos routers (see Number of mongos and
Distribution). You connect from a client (e.g., a mongo shell) specifying multiple mongos's - and the client connects to the cluster using one of the mongos
(see: Connecting to a Sharded Cluster).
From a mongo shell client you use a connection string to connect to a MongoDB server. To connect to a sharded cluster specify mongos host(s) in the connection string using Standard ConnectionString Format; an example.

getting shards under a mongos

I have a MongoConnection to a mongos instance. Mongos internally connects to three shards. Is there any way using which I can get the individual connection each shards or at least get the info on the shards so that I can create new connections ?
I have looked at the MongoConnection class and didn't find anything related to mongos.
http://api.mongodb.org/ruby/1.3.1/Mongo/Connection.html
Mongo version 2.2 and MongoDriver 1.3.1 and language ruby
Eg:
Mongos is a localhost:27107
which has shards at 214.187.112.113:27107 , 209.117.116.103:27107 114.117.162.123:27107
Now My MongoConnection variable has connection to localhost:27107 is there a way from this connection I can create connections to individual shards # 214.187.11.113 etc
MongoDB cluster comes with 3 types of components
1.) Query Router(a mongos instance)
2.) Config server (a mongod instance
3.) Sharding servers (mongod instances)
Mongos is a sharding service provided by MomgoDB.
Mongo shard servers gets registered to config server via mongos.
sh.addShard( "mongo-shard-1:27017" )
sh.addShard( "mongo-shard-2:27017" )
When any request (read/write) comes to mongos, the mongos first gets shard information from config server and then serves that request by forwarding it to the appropriate sharding server(s).
As each shard server is also running mongod, you can connect to it directly but then it will serve as an independent MongoDB instance and will not be a part of cluster.
if you want to control sharding you can do it as follow.
https://www.linode.com/docs/databases/mongodb/build-database-clusters-with-mongodb

Does arbiter node in mongodb has admin database

I have set 3 MongoDB nodes in replication set - primary, secondary and arbiter.
I just want to know does arbiter node in Mongo Database has admin database by default when its configured.
Thanks
The arbiter does not contain any data therefore you or the application won't need to authenticate if you connect directly.
When your application is connecting to a replicaset it will detect if a node is an arbiter and don't attempt to connect to it.
Depending on the driver there could be exceptions (bugs) where the application could still attempt to authenticate to the arbiter.

Mongo Java Replica Set - Can't find master

I'm having problem with connecting to Mongo Replica Set in production. Replica Set has master on one machine, and secundary + arbiter on other machine.
When I run my application locally it connects and uses Replica Set properly. I'm using Windows OS.
Mongo URI is like this:
mongodb://192.168.2.95:20000,192.168.2.96:20000/testDatabase
I'm using Java Driver 2.10.1.
When I deploy application in production server, It can't use replica set. Error is "can't find master".
When I change mongo URI to use single node, like this:
mongodb://192.168.2.95:20000/testDatabase
then it works!
What's the problem? I have search the web and I found few people with similar problem but I didn't found any proper solution...