Mongo: Bad Auth Authentication failed. - mongodb

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));

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.

Cannot connect to mongodb atlas through mongo shell

I've checked other similar posts but none worked for my case.
I upgraded my mongo shell right before connecting to MongoDB Atlas, from which I created a free sandbox database. I used this following for my connection, which was given by atlas.
mongo "mongodb+srv://cluster0-z2lf6.mongodb.net/test" --authenticationDatabase admin --username <user> --password <password>
image of the user of database:
screenshot of the overview of the database:
These are the detail of database:
However, the terminal displays this (I only included about 15 lines, the rest are repetitions.):
MongoDB shell version v3.6.0
connecting to: mongodb+srv://cluster0-z2lf6.mongodb.net/test
2017-12-24T14:39:42.806+0800 I NETWORK [thread1] Starting new replica set monitor for Cluster0-shard-0/cluster0-shard-00-00-z2lf6.mongodb.net.:27017,cluster0-shard-00-01-z2lf6.mongodb.net.:27017,cluster0-shard-00-02-z2lf6.mongodb.net.:27017
2017-12-24T14:39:42.990+0800 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 34.232.245.97:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-24T14:39:42.991+0800 W NETWORK [thread1] Failed to connect to 35.168.27.238:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-24T14:39:43.492+0800 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 52.20.90.64:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-24T14:39:43.492+0800 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Unable to reach primary for set Cluster0-shard-0
2017-12-24T14:39:43.492+0800 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Cannot reach any nodes for set Cluster0-shard-0. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2017-12-24T14:39:43.997+0800 W NETWORK [thread1] Failed to connect to 35.168.27.238:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-24T14:39:44.000+0800 W NETWORK [thread1] Failed to connect to 34.232.245.97:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-24T14:39:44.003+0800 W NETWORK [thread1] Failed to connect to 52.20.90.64:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-24T14:39:44.004+0800 W NETWORK [thread1] Unable to reach primary for set Cluster0-shard-0
2017-12-24T14:39:44.004+0800 I NETWORK [thread1] Cannot reach any nodes for set Cluster0-shard-0. Please check network connectivity and the status of the set. This has happened for 2 checks in a row.
What could be the reason for this connection error?
In my case reason was that provider changed my ip address (I have dynamic ip, behind NAT)
after I've updated ip whitelist, connection was restored
Actually the reason why I couldn't connect is because I had a network restriction from my university's wifi. I was given a test url http://portquiz.net:27017 from MongoDB's support team, I opened it in a browser and I couldn't access it.
I changed to another place with wifi and it worked.
The new atlas ui - please note that for better security you should not use 0.0.0.0/0 nut your own ip address or use available timer to terminate the rule

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
>

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 ?