mongodb shell 登录 - 密码包含特殊字符,如 -(连字符)和 '(单引号)

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

我正在尝试使用 mongodb shell 登录 mongodb 数据库,如果密码包含任何特殊字符,如 -(连字符)或 '(单引号),它给出错误 - Error parsing command line: unrecognised option '-8B'df5='.

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

请帮忙

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/ 明确表示用户名和密码可以作为该 URI 的一部分传递。

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

%27'

的 URL-escaping 版本

%3D=

的 URL-escaping 版本

Python 的 urllib.quote() 是您可以自己查找这些映射的众多方法之一。

它与(已弃用的)mongo shell 无关,而是与您的操作系统 shell 解析参数的方式有关。 您可以选择:

  • 继续使用单引号,但您必须使用反斜杠转义字符串中的单引号。
  • 使用双引号将字符串括起来

因此您可以改用以下命令:

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

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