Search code examples
linuxmongodbshellcommand-linemongodb-shell

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

  • 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.