0

I have a problem connecting to MongoDB atlas through mongo shell, even though I have done everything right and triple checked data username and password, included my IP in the whitelist, changed the database name to test, and tried adding --password in the same line

`$ mongo "mongodb+srv://cluster******.mongodb.net/test" --username admin --password admin MongoDB shell version v4.4.6 connecting to: mongodb://cluster0-shard-00-00.4d5jd.mongodb.net:27017,cluster0-shard-00-01.4d5jd.mongodb.net:27017,cluster0-shard-00-02.4d5jd.mongodb.net:27017/test?compressors=disabled&gssapiServiceName=mongodb&ssl=true

*** You have failed to connect to a MongoDB Atlas cluster. Please ensure that your IP whitelist allows connections from your network.

Error: bad auth : Authentication failed. : connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6 exception: connect failed exiting with code 1`

M. Tayeb
  • 3
  • 3
  • Can you access the page www.portquiz.net:27017 ? it checks if you have a firewall. If you do, then the problem is on Atlas. Go to your Atlas cluster -> Network tab -> white list all IPs adding **0.0.0.0** to the field – Minsky Jul 02 '21 at 09:00
  • 1
    Also, if you share your cluster subdomain: `cluster0.4d5jd` people are likely to hack you, so better never share that data. – Minsky Jul 02 '21 at 09:03
  • Have a look at https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Jul 02 '21 at 11:17
  • `Error: bad auth : Authentication failed...` - generally means wrong userid / password. have you tried connecting to your Atlas cluster using any other tools like Compass? – prasad_ Jul 02 '21 at 11:40
  • I accessed "www.portquiz.net:27017" everything is good there and add 0.0.0.0 including my IP address to the whitelist.// cluster subdomain removed thank you so much! I have tried connecting using Compass keeps trying to connect until it gives Timeout Error . – M. Tayeb Jul 03 '21 at 07:21

1 Answers1

1

MongoDB Atlas creates all users in the admin database.

The output from your attempt does not indicate that it was using the admin database for authentication, so you will likely need to use either the --authenticationDatabase command line option, or include authSource= in the URL, i.e. one of:

mongo "mongodb+srv://cluster******.mongodb.net/test" --username admin --password admin --authenticationDatabase admin

or

mongo "mongodb+srv://cluster******.mongodb.net/test?authSource=admin" --username admin --password admin
Joe
  • 25,000
  • 3
  • 22
  • 44
  • Thank you so much for the great help, Joe! I really appreciate the effort. The first link worked and connected me to Atlas thank you so much! – M. Tayeb Jul 04 '21 at 08:50
  • But it gives "MongoDB Enterprise atlas-65olkl-shard-0:SECONDARY>" not primary is it ok to give SECONDARY because it doesn't take "show dbs" command – M. Tayeb Jul 04 '21 at 09:04
  • That seems a bit odd, try adding `&replicaSet=atlas-65olkl-shard-0` to the URL – Joe Jul 04 '21 at 10:30
  • IT WORKED thank you so much, Joe !!! I really appreciate the help, I just wish that I can understand those things you helped me with. Again thank you so much! – M. Tayeb Jul 04 '21 at 14:49