'mongodb shell login - password contains special characters like -(hyphen) and '(single quote)
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
Solution 1:[1]
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.
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Daniel Baktiar |
