mongodb replicaset connection error - mongodb

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 ?

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 exception connection failed

when I'm trying to open mongodb with command prompt I'm getting a error message like
D:\mongo-bd\bin>mongo.exe
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017
2018-04-27T18:50:28.340+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1
:27017 after 5000ms milliseconds, giving up.
2018-04-27T18:50:28.340+0530 E QUERY [thread1] Error: couldn't connect to ser
ver 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:251:13
#(connect):1:6
exception: connect failed
I have tried by running mongo --repair command but still I'm facing this problem.
1 First set up the data directory - google it
2 then run the
{installationDirectory}/MongoDB/Server/{serverVersion}/bin/mongod.exe
to start the service
3 Get into the shell
{installationDirectory}/MongoDB/Server/{serverVersion}/bin/mongo.exe

Mongodb connection failed

I have created a mongodb cluster and trying to connect via mongo shell, but getting below error. Is there something wrong that I am doing?
mongo "mongodb://cluster0-shard-00-00-qvc6m.mongodb.net:27017,cluster0-shard-00-01-qvc6m.mongodb.net:27017,cluster0-shard-00-02-qvc6m.mongodb.net:27017/test?replicaSet=cluster0-shard-0" --authenticationDatabase <DATABASE> --ssl --username <USERNAME> --password <PASSWORD>
MongoDB shell version: 3.2.12
connecting to: mongodb://cluster0-shard-00-00-qvc6m.mongodb.net:27017,cluster0-shard-00-01-qvc6m.mongodb.net:27017,cluster0-shard-00-02-qvc6m.mongodb.net:27017/test?replicaSet=cluster0-shard-0
2017-06-02T18:59:22.341+0530 I NETWORK [thread1] Starting new replica set monitor for cluster0-shard-0/cluster0-shard-00-00-qvc6m.mongodb.net:27017,cluster0-shard-00-01-qvc6m.mongodb.net:27017,cluster0-shard-00-02-qvc6m.mongodb.net:27017
2017-06-02T18:59:22.342+0530 I NETWORK [ReplicaSetMonitorWatcher] starting
2017-06-02T18:59:27.220+0530 W NETWORK [thread1] No primary detected for set cluster0-shard-0
2017-06-02T18:59:28.685+0530 W NETWORK [thread1] No primary detected for set cluster0-shard-0
2017-06-02T18:59:30.120+0530 W NETWORK [thread1] No primary detected for set cluster0-shard-0
2017-06-02T18:59:31.553+0530 W NETWORK [thread1] No primary detected for set cluster0-shard-0
2017-06-02T18:59:32.679+0530 W NETWORK [ReplicaSetMonitorWatcher] No primary detected for set cluster0-shard-0
2017-06-02T18:59:34.072+0530 W NETWORK [thread1] No primary detected for set cluster0-shard-0
2017-06-02T18:59:35.546+0530 W NETWORK [thread1] No primary detected for set cluster0-shard-0
2017-06-02T18:59:36.981+0530 W NETWORK [thread1] No primary detected for set cluster0-shard-0
2017-06-02T18:59:36.981+0530 E QUERY [thread1] Error: connect failed to replica set cluster0-shard-0/cluster0-shard-00-00-qvc6m.mongodb.net:27017,cluster0-shard-00-01-qvc6m.mongodb.net:27017,cluster0-shard-00-02-qvc6m.mongodb.net:27017 :
connect#src/mongo/shell/mongo.js:223:14
#(connect):1:6
exception: connect failed
Thanks in advance.
I think it must be because of difference in the mongo shell version on the host(3.2.12) vs the mongo engine version used. You can try making the versions same for both then it should work.
if you have this error exception: connect failed
Type this command in terminator linux:
sudo -s
sudo service mongod start

Why mongodb connection url is not compatible for shell and driver?

I use this connection url to connect mongodb instance through driver: mongodb://localhost:27017?ssl=true and it works fine. But the same url doesn't work on mongo-shell instead I have to take out the ssl=true parameter and use mongo mongodb://localhost:27017 --ssl command. Why isn't they compatible with each other? Is there a universal url for both types of connections?
EDIT1
I have tried with mongo 3.4.3 and 3.4.2. Both version have the same issue.
Below is the detailed command and output I got:
mongo mongodb://host-1:27017,host-2:27017,host3:27017/admin?replicaSet=Cluster0-shard-0&ssl=true --username admin --password 123456
Below is the output:
connecting to: mongodb://host-1:27017,host-2:27017,host-3:27017/admin?replicaSet=Cluster0-shard-0
2017-04-10T20:52:09.345+1000 I NETWORK [thread1] Starting new replica set monitor for Cluster0-shard-0/host-1:27017,host-2:27017,host-3:27017
2017-04-10T20:52:09.416+1000 I NETWORK [thread1] Socket recv() Connection reset by peer 13.54.24.198:27017
2017-04-10T20:52:09.416+1000 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [13.54.24.198:27017]
2017-04-10T20:52:09.416+1000 W NETWORK [thread1] No primary detected for set Cluster0-shard-0
2017-04-10T20:52:09.416+1000 I NETWORK [thread1] All nodes for set Cluster0-shard-0 are down. This has happened for 1 checks in a row.
2017-04-10T20:52:09.987+1000 I NETWORK [thread1] Socket recv() Connection reset by peer 13.54.31.109:27017
2017-04-10T20:52:09.987+1000 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [13.54.31.109:27017]
2017-04-10T20:52:10.019+1000 W NETWORK [thread1] No primary detected for set Cluster0-shard-0
2017-04-10T20:52:10.019+1000 I NETWORK [thread1] All nodes for set Cluster0-sha
rd-0 are down. This has happened for 2 checks in a row.
2017-04-10T20:52:10.558+1000 I NETWORK [thread1] Socket recv() Connection reset
...
2017-04-10T20:53:44.940+1000 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [13.54.31.109:27017]
This should be fixed if you use mongo shell version >= 3.4.
Assuming that you are connecting using a 3.4 shell, the command line and the output would look something like this:
mongo "mongodb://localhost:27017/test?ssl=true" --authenticationDatabase admin --username xxx --password xxx
MongoDB shell version v3.4.3
connecting to: mongodb://localhost:27017/test?ssl=true
MongoDB server version: 3.4.3
>

Could not connect to mongodb. errno:111 Connection refused

I am using mongodb on ubuntu-16.04 but mongodb give server connection error:
MongoDB shell version: 3.2.9 connecting to: test
2016-09-23T12:01:10.258+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-09-23T12:01:10.286+0530 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : connect#src/mongo/shell/mongo.js:229:14 #(connect):1:6 exception: connect failed
What can I do?
looks like your mongod process is not running on port 27017. Make sure your mongod process is running maybe verify with linux command.
If you start mongod process and even then it's failing to connect with mongo command, it means it's failing to intialize, check out designated mongo log file for the reason