Keep getting this error when connecting: Server record does not share hostname with parent URI

Hello. Please I have been working on this for days now. Whenever I try to connect to my mongodb database, I keep getting an error like this:
Server record does not share hostname with parent URI

MY CONNECTION:

const connectionString = MONGODB_URI ?? '';
mongoose
  .connect(connectionString)
  .then(() => {
    app.listen(port)
    console.log('MongoDB connected successfully.');
  })
  .catch((err) => {
    console.error('Failed to connect to MongoDB:', err);
  });

This is how my password is formated (Note: It’s in a .env file and I am able to console.log it successfully)
MONGODB_URI=mongodb+srv://username:jSnSniWBTlbu4SjX@hostname.fzsalgc.mongodb.net/?retryWrites=true&w=majority

There are no special characters in the password. I have even tried using:
MONGODB_URI=mongodb+srv://username:jSnSniWBTlbu4SjX%40@hostname.fzsalgc.mongodb.net/?retryWrites=true&w=majority
still doesn’t work…

The following is not a valid cluster.

There is no DNS entry for it. The fzcalgc.mongodb.net seems correct. You should go back in Atlas and copy the URI from there. You might have use the default name cluster0 so you may try to replace hostname with cluster0.

Try

const connectionString = MONGODB_URI;

rather than

const connectionString = MONGODB_URI ?? '';

Yes I am aware. I changed it when posting here. Am not so sure if when exposed, could pose a security risk.

even when I directly put the mongo db url, it still doesn’t work:

mongoose
  .connect('mongodb+srv://username:jSnSniWBTlbu4SjX@hostname.fepglzk.mongodb.net/?retryWrites=true&w=majority')
  .then(() => {
    app.listen(port)
    console.log('MongoDB connected successfully.');
  })
  .catch((err) => {
    console.error('Failed to connect to MongoDB:', err);
  });

I get the same error

This is what I think is throwing the error:

    for (let i = 0; i < addresses.length; i++) {
      if (!matchesParentDomain(addresses[i].name, result.hostname, result.domainLength)) {
        return callback(
          new MongoParseError('Server record does not share hostname with parent URI')
        );
      }
    }

Any ideas?

Looks like a DNS issue but without the real cluster name, no one can help finding what is wrong.

But since it is an Atlas cluster, I will be surprised if there is a real issue with DNS records.

I suspect a version error from node or deno.

My last idea is that you go on Atlas, and get the old style mongodb:// URI rather than the SRV style mongodb+srv:// URI.

I posted the real cluster name in the last message. The one you replied to.

I tried to connect locally to mongodb but it doesn’t also work.

I get the error:
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017

About

If you want to connect locally you must run mongod locally.

About

Sorry but it is hard to see the difference between fepglzk and fzsalgc in the middle of the text of 2 posts so I did not see that it was different. With the correct cluster address I confirm that there is no DNS issue with.

So it is a node version, or a mongoose version issue or a mongodb driver version issue.

I just solved it. Practically as you replied. Thankfully to this post

Probably has something to do with my node js version

EDIT: I am already running mongodb

Do you have any idea as to how this fix might apply to the ATLAS mongo db URI?

Only you can answer that by trying with your Atlas cluster URI.

Why does it occur? Can we make work with LOCALHOST?