MongoServerSelectionError: connection <monitor> to closed

MongoServerSelectionError: connection to closed

Getting Authentication failed error.

Thanks in advance.

from a console, your connection string has this error; it is not an accesible address.

getaddrinfo("") failed: No address associated with hostname
Error: couldn't initialize connection to host, address is invalid

other than that, your username/password might have a typo, you might be forgetting auth admin database, or tls/ssl flags.

try your connection string in a terminal with mongo, mongosh, or use Compass. if you connect succesfuly with them, you can then use it in your app

you seem you are still in beginner’s steps. if not, sorry for the impression.

let me suggest you 2 things for you to work with MongoDB and Javascript.

1- Run a local MongoDB server. This will be both faster during development and easy to setup.

  • Download a zipped version of the community server. MongoDB Community Download
  • extract it to some folder, I have mine in c:\DevTools\mongodb
  • create a data folder, preferably in the same folder c:\DevTools\mongodb
  • run with c:\DevTools\mongodb\bin\mongod.exe --dbpath c:\DevTools\mongodb\data
  • create a batch file with this line in it on desktop, or in a place where you most work, maybe in your project directory.
  • connect to it with MongoClient.connect("mongodb://")

2- If you haven’t done so, check this course: M220JS: MongoDB for JavaScript Developers | MongoDB University.

Means you have the wrong user name or the wrong password.

There is no DNS address since this is a cluster address. There is TXT DNS entry and corresponding SRV record.

id 62723
opcode QUERY
flags QR RD RA
;ANSWER 44 IN TXT "authSource=admin&replicaSet=atlas-x8f8fc-shard-0"