mongodb shell login - password contains special characters like -(hyphen) and '(single quote)

7.5k Views Asked by At

I am trying to login to mongodb database using mongodb shell, and if the password contains any special characters like -(hyphen) or '(single quote) , it gives error - Error parsing command line: unrecognised option '-8B'df5='.

mongo -u username -p -8B'df5= --authenticationDatabase admin

Kindly help

2

There are 2 best solutions below

1
Charles Duffy On BEST ANSWER

The manual for the deprecated mongo command makes it clear that the preferred way to pass a connection string is as a URI. https://docs.mongodb.com/manual/reference/connection-string/ makes it clear that usernames and passwords can be passed as part of that URI.

mongo mongodb://username:-8B%27df5%3D@hostname/admin

%27 is the URL-escaping version of '

%3D is the URL-escaping version of =

Python's urllib.quote() is one of the many ways you can look up these mappings yourself.

1
Daniel Baktiar On

It has nothing to do with (the deprecated) mongo shell, instead it has something to do the way your operating system shell parsing the parameters. You may choose to:

  • keep using single quote, but you have to escape the single quote inside the string with backslash.
  • use double quote to enclose the string

So you may use below command instead:

mongo -u username -p '-8B\'df5=' --authenticationDatabase admin

or

mongo -u username -p "-8B'df5=" --authenticationDatabase admin