Mongosh command rejects host names with an underscore which are accepted by mongo command

Hi MongoDB Community,

I have been using MongoDB 5.0.1. I am converting a few bash scripts from mongo to mongosh. I noticed MongoDB shell is rejecting hostnames with an underscore in the name “_” (and other characters which are not to DNS standards). These are accepted by the mongo command.

$ mongosh --host host_abc
MongoshInvalidInputError: [COMMON-10001] The --host argument contains an invalid character: _

For better or worse there are non standard DNS names in use on many networks which may not be easy to change. Is there any way around this hostname validation?

I am new to MongoDB. Is this the correct forum to raise these types of issues?


Hi KC!

Thanks for reaching out! Right now you could also specify a connection string like this:

mongosh mongodb://host_abc

The validation of the hostname does not happen there.

Just out of curiosity - the _ is the not allowed character in that case. Do you have other characters you have locally that would be regarded as invalid characters but you have as part of your DNS names?

If you want to you could also file a bug report in JIRA directly: But I’ll take care of that for this request :slight_smile:

1 Like

Why would you expect non compliant names to work anywhere. Even if a tool parses it okay there is no guarantees that the underlying resolution standard libraries or system calls will allow it either.

The correct solution is to used compliant host names.


Hi Michael,

thanks for that workaround. We only use the underscore in our DNS CNAMES. I tried to get the Active Directory DNS manager client to accept a few other characters but all it seemed accept was alphanumerics, hyphens and underscores.

Regards, KC

Agreed. I would generally stick to using alphanumerics and hyphens however underscores look to be valid characters for DNS CNAMES. They are accepted by Microsoft’s DNS Manager for CNAMES and I’d say they are probably in use in the wild. We have quite a few clients tools in use and mongodb shell is the first I have come across which fails to connect if the hostname contains an underscore.

@K_C1 I’ve opened a ticket and we’ll get the _ in as an allowed character for more convenience for the --host parameter:


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.