MongoDb ReplicaSet in container, accessable on bridge and host network - mongodb

I am running 3 MongoDB instances in a container named mongodb-local, and initiating them as a replica set with the command:
mongo --port 27020 --eval 'rs.initiate({_id: "rs1", members: [ { _id: 0, host: "mongodb-local:27018"}, { _id: 1, host: "mongodb-local:27019"}, { _id: 2, host: "mongodb-local:27020"} ] })'
This allows the MongoDB instances to talk to other services on the bridge network as mongodb://mongodb-local:27018,mongodb-local:27019,mongodb-local:27020/?replicaSet=rs1
However, I also want to connect to the MongoDB replica-set from the host/public. I have published the 3 ports there, however attempting to connect to and use mongodb://localhost:27018,localhost:27019,localhost:27020/?replicaSet=rs1 fails because the replicas know eachother as mongodb-local, which is not resolvable to the host.
Errors for example in the c# driver such as:
One or more errors occurred. (A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27018" }", EndPoint: "Unspecified/localhost:27018", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
or in the shell
...>mongo mongodb://localhost:27020,localhost:27019,localhost:27018/?replicaSet=rs1
MongoDB shell version v4.0.2
connecting to: mongodb://localhost:27020,localhost:27019,localhost:27018/?replicaSet=rs1
2020-07-22T11:24:23.036+0100 I NETWORK [js] Starting new replica set monitor for rs1/localhost:27020,localhost:27019,localhost:27018
2020-07-22T11:24:23.042+0100 I NETWORK [js] Successfully connected to localhost:27018 (1 connections now open to localhost:27018 with a 5 second timeout)
2020-07-22T11:24:23.042+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to localhost:27019 (1 connections now open to localhost:27019 with a 5 second timeout)
2020-07-22T11:24:23.046+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to localhost:27020 (1 connections now open to localhost:27020 with a 5 second timeout)
2020-07-22T11:24:23.048+0100 I NETWORK [ReplicaSetMonitor-TaskExecutor] changing hosts to rs1/mongodb-local:27018,mongodb-local:27019,mongodb-local:27020 from rs1/localhost:27018,localhost:27019,localhost:27020
2020-07-22T11:24:28.563+0100 W NETWORK [js] Unable to reach primary for set rs1
2020-07-22T11:24:28.563+0100 I NETWORK [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2020-07-22T11:24:32.072+0100 W NETWORK [js] Unable to reach primary for set rs1
2020-07-22T11:24:32.072+0100 I NETWORK [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 2 checks in a row.
2020-07-22T11:24:35.581+0100 W NETWORK [js] Unable to reach primary for set rs1
2020-07-22T11:24:35.581+0100 I NETWORK [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 3 checks in a row.
2020-07-22T11:24:39.091+0100 W NETWORK [js] Unable to reach primary for set rs1
2020-07-22T11:24:39.092+0100 I NETWORK [js] Cannot reach any nodes for set rs1. Please check network connectivity and the status of the set. This has happened for 4 checks in a row.
2020-07-22T11:24:39.097+0100 E QUERY [js] Error: connect failed to replica set rs1/localhost:27020,localhost:27019,localhost:27018 :
connect#src/mongo/shell/mongo.js:257:13
#(connect):1:6
exception: connect failed
Conversely, I could initiate the replica set as:
mongo --port 27020 --eval 'rs.initiate({_id: "rs1", members: [ { _id: 0, host: "localhost:27018"}, { _id: 1, host: "localhost:27019"}, { _id: 2, host: "localhost:27020"} ] })'
Which will allow connections from the host machine/public, and by coincedence since the replicas are all running on the same container, they can also talk to eachother.
However, other services cannot connect with mongodb://mongodb-local:27018,mongodb-local:27019,mongodb-local:27020/?replicaSet=rs1, with a similar error.
This would also fail if I wanted to put each replica on a separate container.
I believe this is because the client receives the replica-set record after connecting and expects that to be available on the network it is connecting from? This is not a great assumption if so.
How can I initiate the replica-set on the bridge network and also allow connections from outside the bridge network?

Related

Unable to connect to MongoDB from a VPN

My Mongo database is hosted on MongoDB Atlas cloud server. I am unable to connect to the server when I am on VPN(company network). When I disconnect from the VPN and try, it works fine. I have tried through multiple options, from Mongo Compass, command line, through code snippet. It's the same problem.
I have whitelisted my public IP address on the Mongo Atlas server but still unable to connect.
On Mongo Compass I get the following error :
Connection URI : mongodb+srv://<username>:<password>#<hostname>
Error : connect ECONNREFUSED 52.42.97.161:27017
From command line :
Command : mongo "mongodb://<hostname>:27017" --ssl --authenticationDatabase admin --username <username> --password <password>
Error :
W NETWORK [thread1] Failed to connect to 54.148.205.233:27017 after 5000ms milliseconds, giving up.
W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 34.211.199.170:27017 after 5000ms milliseco ds, giving up.
W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 52.42.97.161:27017 after 5000ms millisecond , giving up.
W NETWORK [ReplicaSetMonitor-TaskExecutor-0] No primary detected for set Field-shard-0
I NETWORK [ReplicaSetMonitor-TaskExecutor-0] All nodes for set Field-shard-0 are down. This has happened for checks in a row.
W NETWORK [thread1] Failed to connect to 34.211.199.170:27017 after 5000ms milliseconds, giving up.
W NETWORK [thread1] Failed to connect to 52.42.97.161:27017 after 5000ms milliseconds, giving up.
W NETWORK [thread1] Failed to connect to 54.148.205.233:27017 after 5000ms milliseconds, giving up.
W NETWORK [thread1] No primary detected for set Field-shard-0
I NETWORK [thread1] All nodes for set Field-shard-0 are down. This has happened for 2 checks in a row.
E QUERY [thread1] Error: connect failed to replica set Field-shard-0/field-shard-00-00.xvwf9.mongodb.net:27 17,field-shard-00-01.xvwf9.mongodb.net:27017,field-shard-00-02.xvwf9.mongodb.net:27017 :
connect#src/mongo/shell/mongo.js:237:13 #(connect):1:6 exception: connect failed
Please let me know what I can try to make this work. Thanks!

MongoDB Atlas connection

hello can anybody help me with this error
$ mongo "mongodb+srv://cluster0-zdzwa.mongodb.net/test" --username admin-dalia
MongoDB shell version v4.2.6
Enter password: Cannot get console mode 6
connecting to: mongodb://cluster0-shard-00-02-zdzwa.mongodb.net:27017,cluster0-shard-00-00-zdzwa.mongodb.net:27017,cluster0-shard-00-01-zdzwa.mongodb.net:27017/test?authSource=admin&compressors=disabled&gssapiServiceName=mongodb&replicaSet=Cluster0-shard-0&ssl=true
2020-05-01T18:00:58.985+0300 I NETWORK [js] Starting new replica set monitor for Cluster0-shard-0/cluster0-shard-00-02-zdzwa.mongodb.net:27017,cluster0-shard-00-00-zdzwa.mongodb.net:27017,cluster0-shard-00-01-zdzwa.mongodb.net:27017
2020-05-01T18:00:58.986+0300 I CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to cluster0-shard-00-02-zdzwa.mongodb.net:27017
2020-05-01T18:00:58.986+0300 I CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to cluster0-shard-00-01-zdzwa.mongodb.net:27017
2020-05-01T18:00:58.986+0300 I CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to cluster0-shard-00-00-zdzwa.mongodb.net:27017
2020-05-01T18:00:59.983+0300 I NETWORK [ReplicaSetMonitor-TaskExecutor] Confirmed replica set for Cluster0-shard-0 is Cluster0-shard-0/cluster0-shard-00-00-zdzwa.mongodb.net:27017,cluster0-shard-00-01-zdzwa.mongodb.net:27017,cluster0-shard-00-02-zdzwa.mongodb.net:27017
2020-05-01T18:01:00.530+0300 I NETWORK [js] Marking host cluster0-shard-00-01-zdzwa.mongodb.net:27017 as failed :: caused by :: Location40659: can't connect to new replica set master [cluster0-shard-00-01-zdzwa.mongodb.net:27017], err: AuthenticationFailed: Missing expected field "pwd"
It looks like this is a MongoDB Atlas cluster. Please ensure that your IP whitelist allows connections from your network.
2020-05-01T18:01:00.530+0300 E QUERY [js] Error: can't connect to new replica set master [cluster0-shard-00-01-zdzwa.mongodb.net:27017], err: AuthenticationFailed: Missing expected field "pwd" :
connect#src/mongo/shell/mongo.js:341:17
#(connect):2:6
----------
2020-05-01T18:01:00.533+0300 F - [main] exception: connect failed
2020-05-01T18:01:00.533+0300 E - [main] exiting with code 1
thanks in advance
You need to add your IP to whitelist.
In your mongoAtlas there is a tab securicy -> network access -> add there 0.0.0.0
This will allow access from any IP from web.
If you look at the error it looks like you didn't or can't enter your password.
$ mongo "mongodb+srv://cluster0-zdzwa.mongodb.net/test" --username admin-dalia
MongoDB shell version v4.2.6
Enter password: Cannot get console mode 6
The error from Atlas is:
err: AuthenticationFailed: Missing expected field "pwd"
pwd is a short name for password.
Try passing it in with the --password argument.
You might also try Compass which allows you to paste the connection string straight into the tool as a way to validate that you are doing the right thing.

FailedToSatisfyReadPreference: Could not find host matching read preference { mode: "primary" }

I want to use mongodb-shard for my project. I created a helm chart https://github.com/b-rohit/mongodb-shard-chart to deploy to kubernetes cluster.
I use kind cluster running locally to test it. The config and shard servers are running properly. I am able to execute commands in their mongo shells. The mongos server is not able to connect to the replica set in config server. I get following error message in mongos
2020-04-17T13:33:31.579+0000 W SHARDING [replSetDistLockPinger] pinging failed for distributed lock pinger :: caused by :: FailedToSatisfyReadPreference: Could not find host matching read preference { mode: "primary" } for set mongors1conf
2020-04-17T13:33:31.579+0000 W SHARDING [mongosMain] Error initializing sharding state, sleeping for 2 seconds and trying again :: caused by :: FailedToSatisfyReadPreference: Error loading clusterID :: caused by :: Could not find host matching read preference { mode: "nearest" } for set mongors1conf
2020-04-17T13:33:31.579+0000 I SHARDING [shard-registry-reload] Periodic reload of shard registry failed :: caused by :: FailedToSatisfyReadPreference: could not get updated shard list from config server :: caused by :: Could not find host matching read preference { mode: "nearest" } for set mongors1conf; will retry after 30s
On config server logs are following
2020-04-17T13:33:11.578+0000 I NETWORK [listener] connection accepted from 10.244.0.6:34400 #5 (1 connection now open)
2020-04-17T13:33:11.578+0000 I NETWORK [conn5] received client metadata from 10.244.0.6:34400 conn5: { driver: { name: "NetworkInterfaceTL", version: "4.2.5" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "18.04" } }
2020-04-17T13:33:11.589+0000 I ACCESS [conn5] Successfully authenticated as principal __system on local from client 10.244.0.6:34400
2020-04-17T13:33:38.197+0000 I SHARDING [replSetDistLockPinger] Marking collection config.lockpings as collection version: <unsharded>
2020-04-17T13:33:38.202+0000 W SHARDING [replSetDistLockPinger] pinging failed for distributed lock pinger :: caused by :: LockStateChangeFailed: findAndModify query predicate didn't match any lock document
2020-04-17T13:44:39.743+0000 I CONTROL [LogicalSessionCacheRefresh] Failed to create config.system.sessions: Cannot create config.system.sessions until there are shards, will try again at the next refresh interval
2020-04-17T13:44:39.743+0000 I CONTROL [LogicalSessionCacheRefresh] Sessions collection is not set up; waiting until next sessions refresh interval: Cannot create config.system.sessions until there are shards
2020-04-17T13:44:39.743+0000 I SH_REFR [ConfigServerCatalogCacheLoader-1] Refresh for collection config.system.sessions took 0 ms and found the collection is not sharded
2020-04-17T13:44:39.743+0000 I CONTROL [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: Collection config.system.sessions is not sharded.
2020-04-17T13:44:42.570+0000 I NETWORK [conn5] end connection 10.244.0.10:37922 (0 connections now open)
I am new to mongodb. It took lot of time to put this chart together. I checked other similar questions also. could not find host matching read preferences in mongodb
I am not able to debug it further.
Your config server replica set is either:
not running (not all nodes are up)
not a replica set (replSetInitiate not executed, or failed)
is referenced from the shard nodes incorrectly (wrong host, ip or wrong replica set name)
is up and running but your shards aren't allowed to access it due to firewall rules
Ensure you can access the replica set nodes from mongo shell from the machines on which the shard mongods are running.

Mongo: Bad Auth Authentication failed.

I am trying to do a mongoose connection from Mongo Atlas. I have a cluster ayman and I am trying to connect it from Terminal(Ubuntu 18).
It says that I have successfully connected to the shard, but it says authentication failed. I am giving credentials defined as SCRAM on Mongo Atlas. Mongo Security Page
mongo "mongodb://ayman-shard-00-00-xxzs1.mongodb.net:27017,ayman-shard-00-01-xxzs1.mongodb.net:27017,ayman-shard-00-02-xxzs1.mongodb.net:27017/test?replicaSet=ayman-shard-0" --ssl --authenticationDatabase admin --username aymanpatel --password <PASSWORD>
MongoDB shell version v3.6.3
connecting to: mongodb://ayman-shard-00-00-xxzs1.mongodb.net:27017,ayman-shard-00-01-xxzs1.mongodb.net:27017,ayman-shard-00-02-xxzs1.mongodb.net:27017/test?replicaSet=ayman-shard-0
2018-10-07T09:40:34.288+0530 I NETWORK [thread1] Starting new replica set monitor for ayman-shard-0/ayman-shard-00-00-xxzs1.mongodb.net:27017,ayman-shard-00-01-xxzs1.mongodb.net:27017,ayman-shard-00-02-xxzs1.mongodb.net:27017
2018-10-07T09:40:34.336+0530 I NETWORK [thread1] Successfully connected to ayman-shard-00-01-xxzs1.mongodb.net:27017 (1 connections now open to ayman-shard-00-01-xxzs1.mongodb.net:27017 with a 5 second timeout)
2018-10-07T09:40:34.339+0530 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to ayman-shard-00-02-xxzs1.mongodb.net:27017 (1 connections now open to ayman-shard-00-02-xxzs1.mongodb.net:27017 with a 5 second timeout)
2018-10-07T09:40:34.393+0530 I NETWORK [thread1] Successfully connected to ayman-shard-00-00-xxzs1.mongodb.net:27017 (1 connections now open to ayman-shard-00-00-xxzs1.mongodb.net:27017 with a 5 second timeout)
MongoDB server version: 3.6.8
2018-10-07T09:40:34.517+0530 I NETWORK [thread1] Marking host ayman-shard-00-00-xxzs1.mongodb.net:27017 as failed :: caused by :: Location8000: can't authenticate against replica set node ayman-shard-00-00-xxzs1.mongodb.net:27017: bad auth Authentication failed.
2018-10-07T09:40:34.562+0530 I NETWORK [thread1] Successfully connected to ayman-shard-00-01-xxzs1.mongodb.net:27017 (1 connections now open to ayman-shard-00-01-xxzs1.mongodb.net:27017 with a 0 second timeout)
2018-10-07T09:40:34.609+0530 I NETWORK [thread1] Marking host ayman-shard-00-01-xxzs1.mongodb.net:27017 as failed :: caused by :: Location8000: can't authenticate against replica set node ayman-shard-00-01-xxzs1.mongodb.net:27017: bad auth Authentication failed.
2018-10-07T09:40:34.655+0530 I NETWORK [thread1] Successfully connected to ayman-shard-00-02-xxzs1.mongodb.net:27017 (1 connections now open to ayman-shard-00-02-xxzs1.mongodb.net:27017 with a 0 second timeout)
2018-10-07T09:40:34.705+0530 I NETWORK [thread1] Marking host ayman-shard-00-02-xxzs1.mongodb.net:27017 as failed :: caused by :: Location8000: can't authenticate against replica set node ayman-shard-00-02-xxzs1.mongodb.net:27017: bad auth Authentication failed.
2018-10-07T09:40:34.731+0530 I NETWORK [thread1] Marking host ayman-shard-00-00-xxzs1.mongodb.net:27017 as failed :: caused by :: Location8000: can't authenticate against replica set node ayman-shard-00-00-xxzs1.mongodb.net:27017: bad auth Authentication failed.
2018-10-07T09:40:34.731+0530 E QUERY [thread1] Error: can't authenticate against replica set node ayman-shard-00-00-xxzs1.mongodb.net:27017: bad auth Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1608:20
#(auth):6:1
#(auth):1:2
exception: login failed
Cluster name: ayman
Username: aymanpatel
MongoDB shell version v3.6.3
Try to do something like these:
var mongoDB = 'mongodb+srv://aymanpatel:<password>#cluster0-lnhl4.mongodb.net/test?
retryWrites=true&w=majority';
mongoose.connect(mongoDB, { useNewUrlParser: true })
.then(() => console.log("MongoDb connected"))
.catch(err => console.log(err));

mongodb replicaset connection error

I am getting connection errors, when I try to connect to my replica set either in PHP code or in the mongo shell.
1. PHP
This is the php code trying to make the connection:
$manager = new MongoDB\Driver\Manager("mongodb://remoteServerIp:27017/".$db."?replicaSet=rs0");
and the connection result is this error:
No suitable servers found (`serverSelectionTryOnce` set):
[connection timeout calling ismaster on '10.10.0.16:27017']
[connection timeout calling ismaster on '10.10.0.17:27017']
[connection timeout calling ismaster on '10.10.0.15:27017']
2. mongo shell
If I try this command to connect in the mongo shell:
shell> mongo -host "rs0/remoteServerIp"
... the result is this error:
MongoDB shell version: 3.2.10 connecting to: rs0/remoteServerIp:27017/test
2016-12-28T02:19:02.216+0000 I NETWORK [thread1] Starting new replica set monitor for rs0/xxx.xxx.xxx.xxx:27017
2016-12-28T02:19:02.216+0000 I NETWORK [ReplicaSetMonitorWatcher] starting
2016-12-28T02:19:07.216+0000 W NETWORK [thread1] Failed to connect to xxx.xxx.xxx.xxx:27017, reason: errno:115 Operation now in progress
2016-12-28T02:19:07.216+0000 W NETWORK [thread1] No primary detected for set rs0
2016-12-28T02:19:12.716+0000 W NETWORK [thread1] Failed to connect to xxx.xxx.xxx.xxx:27017, reason: errno:115 Operation now in progress
2016-12-28T02:19:12.716+0000 W NETWORK [thread1] No primary detected for set rs0
2016-12-28T02:19:18.216+0000 W NETWORK [thread1] Failed to connect to xxx.xxx.xxx.xxx:27017, reason: errno:115 Operation now in progress
2016-12-28T02:19:18.216+0000 W NETWORK [thread1] No primary detected for set rs0
2016-12-28T02:19:18.216+0000 E QUERY [thread1] Error: connect failed to replica set rs0/xxx.xxx.xxx.xxx:27017 : connect#src/mongo/shell/mongo.js:231:14 #(connect):1:6
3. mongo shell (private ip address)
By contrast, this connection attempt succeeds:
shell> mongo -host "rs0/privateip(10.10.0.16)"
How can I connect to mongo server with replicaset ?