While creating new connection on Mongo Compass the UI try to discover the entire replica set topology and connect to the primary/secondary IP.
Is there a way to create a DIRECT connection to an HOST/IP+Port just like the clients api and disable the topology discovery step?
From pymongo documentation:
directConnection (optional): if True, forces this client to
connect directly to the specified MongoDB host as a standalone. If false, the client connects to the entire replica set of which the given MongoDB host(s) is a part
If you don't want to connect to a replicaset, use a mongodb connection string (as opposed to mongodb+srv), use a host IP / port, and drop the &replicaSet= parameter.
A bug ticket was opened to Mongo on Jira
https://jira.mongodb.org/browse/COMPASS-4534
The fix should be in version 1.42.2
Related
I use mongo connection uri string to connect to my mongo.
mongodb+srv://myname:mypass#mydb.mongodb.net/myDb?retryWrites=true&w=majority
This replica set has 4 nodes. I wish to execute my read operation on a specifc node. I have the node url. But I am not sure where I should specify this in the connection uri. I could not find anything regarding this in the connection-options for connection uri.
This can be done using the readPreferenceTags. The node that I wanted to connect to had a tag with key nodeType and a value analytics. So my connection uri had to be modified to
mongodb+srv://myname:mypass#mydb.mongodb.net/myDb?retryWrites=true&w=majority&readPreference=secondary&readPreferenceTags=nodeType:ANALYTICS
When connecting to mongo cluster do we need replicaSet option in connection URI like below
mongodb://db1.example.net:27017,db2.example.net:2500/?replicaSet=test
What happens if replicaSet option is not used but all the nodes are given in connection URI like below
mongodb://db1.example.net:27017,db2.example.net:2500/
What is the advantage of giving and not giving replicaSet in the connection URI for the above 2 cases.
It's always recommended to include the replicaSet in the MongoDB connection String URI Format as a best practice. Enabling this will help to explore much more options for better application connectivity.
Advantages of including replicaSet:
Once enabled the client/driver will be aware of the all other members in the replica set.
If fail-over occurs client/driver automatically connects to next available member with zero downtime.
Using readConcern we can scale the reads better with other replica members.
replicaSet=myRepl&readConcernLevel=majority
To acknowledge all the write operation we can use write concern along with the URI
replicaSet=myRepl&w=majority&wtimeoutMS=5000
We can enable the connection timeout to maintain a better connectivity.
replicaSet=test&connectTimeoutMS=200000
Securing the application to use only TLS/SSL encrypted connections.
replicaSet=myRepl&ssl=true
For better secured application support and connectivity always use the replicaSet on connection String URI.
You should specify also the replicaSet.
If you don't specify the replicaSet then you still connect to the PRIMARY node. However, in case the PRIMARY becomes unavailable then your connection is lost. If you specify the replicaSet then the client will automatically re-connect to the new primary member.
You an play around and make test with these commands:
db.hello().primary returns the current primary member
db.hostInfo().system.hostname returns the member where you are currently connected to
if you have an endpoint like: mongodb+srv://username:password#mycluster.mik05g.mongodb.net
run the nslookup CLI
nslookup -type=TXT mycluster.mik05g.mongodb.net
Server: 127.0.0.53
Address: 127.0.0.53#53
Non-authoritative answer:
mycluster.mik05g.mongodb.net text = "authSource=admin&replicaSet=MyCluster-shard-0"
So the replicaSet name here is: MyCluster-shard-0
I am using MongoDB with Loopback in my application with a loopback connector to the MongoDB. My application was working fine but now it throws an error
not master and slaveOk=false.
try running rs.slaveOk() in a mongoDB shell
You are attempting to connect to secondary replica whilst previously your app (connection) was set to connect likely to the primary, hence the error. If you use rs.secondaryOk() (slaveOk is deprecated now) you will possibly solve the connection problem but it might not be what you want.
To make sure you are doing the right thing, think if you want to connect to the secondary replica instead of primary. Usually, it's not what you want.
If you have permissions to amend the replica configuration
I suggest to connect using MongoDB Compass and execute rs.status() first to see the existing state and configuration for the cluster. Then, verify which replica is primary.
If necessary, adjust priorities in the replicaset configuration to assign primary status to the right replica. The highest priority number sets the replica as primary. This article shows how to do it right.
If you aren't able to change the replica configuration
Try a few things:
make sure your hostname points to the primary replica
if it is a local environment issue - make sure you added your local replica hostnames to the /etc/hosts pointing to 127.0.0.1
experiment with directConnection=true
experiment with multiple replica hosts and ?replicaSet=<name> - read this article (switch tabs to replica)
The best bet is that your database configuration has changed and your connection string no longer reflects it correctly. Usually, slight adjustments in the connection string are needed or just checking to what instance you want to connect.
Using recent client libraries (pymongo 3.4, mongodb (nodejs) 2.2.27), I am having trouble connecting to my mongodb servers with replication.
The replicaset configuration contains either the internal ips of the servers or the hostnames. I'm getting the following error:
pymongo.errors.ServerSelectionTimeoutError: mongodbdriver20151129-arbiter-1:27017: [Errno 8] nodename nor servname provided, or not known,mongodbdriver20151129-instance-1:27017: [Errno 8] nodename nor servname provided, or not known,mongodbdriver20151129-instance-2:27017: [Errno 8] nodename nor servname provided, or not known
or
pymongo.errors.ServerSelectionTimeoutError: 10.0.0.5:27017: timed out,10.0.0.6:27017: timed out,10.0.0.4:27017: timed out
I am currently working around it by changing the replicaset config to contain the external ips for the servers but I guess that would slow down the inter-server communication. How can I connect to my servers from an external location with the original rsconf?
[update] Note: I am trying to connect to the external ip of the servers and this worked fine when using pymongo 2.8 or mongodb (js) 2.1.4
[update] Follow this chat for more details/examples
Later versions of all officially supported MongoDB drivers (including the node driver) follows the Server Discovery and Monitoring spec (SDAM), which mandates that all drivers to monitor all nodes in a replica set (see Monitoring).
The reason for this monitoring is to be able to discover the status of the whole replica set at all times, and reconnect to a new primary should the current primary goes offline for any reason. See What's the point of periodic monitoring
To be able to monitor all nodes in a replica set, the driver must have access to each of the replica set member. Since your replica set is defined using internal IPs inaccessible by the driver, the driver cannot connect to them. This is the reason for the error you're seeing.
There are a couple of ways to solve this issue:
Use IP addresses or hostnames for the replica set configuration that are accessible by the driver (recommended).
Connect to one of the nodes without specifying a replica set, essentially treating the node as a standalone (not recommended).
If the older driver can connect without complaint, then either the driver is very outdated or doesn't follow the proper SDAM spec and should not be used, since its behaviour cannot be guaranteed. MongoDB publishes the SDAM spec and mandates all drivers to follow it for a good reason.
I have a MongoDB replica set that recently got hacked and hackers deleted my database. I don't want this to happen again. What I would like is, only a handful of programs like my MongoDB replica set members, node.js program, and my terminal should be the only one that can communicate with the database and no other program. How should I go about it?
For starters, I have set bind_ip property in my mongod.conf to be [127.0.0.1,ip_1,ip_2,ip_3], one of the IP is it's own IP. Is this enough?
First I identified it is possible to securely connect all my servers. I found the answer with this article on setting up ufw on my ubuntu 16.04. Then I established mongo replica set connection between them. Now the challenge came in establishing Keyfile access control in existing replica set
which I found to be difficult Because of this issue on Github in mongoose.
Using SSL/TLS is easy on the native driver than it is on the mongoose. Plus native driver is much faster than any ORM. So in a phase wise manner I replaced mongoose with mongodb native driver and introduced key file access. Maybe in later versions of mongoose, they might introduce this access.