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



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

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

请帮助

不推荐使用的mongo命令的手册清楚地表明,传递连接字符串的首选方式是作为URI。https://docs.mongodb.com/manual/reference/connection-string/明确了用户名和密码可以作为URI的一部分进行传递。

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

%27'的URL转义版本

%3D=的URL转义版本

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

它与(不推荐使用的(mongoshell无关,而是与操作系统shell解析参数的方式有关。您可以选择:

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

所以您可以使用以下命令:

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

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

最新更新