最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】MongoDB的用户的密码中包含@如何写URI

MongoDB crifan 5345浏览 0评论

折腾:

【已解决】Python中openpyxl处理excel去判断单元格是合并后的以及合并的范围

期间,尝试去优化代码。

参考:

Tutorial — PyMongo 3.6.1 documentation

MongoDB 连接 | 菜鸟教程

去尝试把参数设置:

if gUseLocalMongo:

    # connect to local mongo

    mongoClient = MongoClient()

if gUseRemoteOnlineMongo:

    # connect to remote mongo

    mongoClient = MongoClient(

        host="x.x.x.x",

        port=27017,

        username="username",

        password="P@wd"

    )

改为:MongoDB的URL:

mongodbUri = "mongodb://x.x.x.x:27017/"

host = "x.x.x.x",

port = 27017,

username = “username",

password = “pass@word"

时发现,此处密码中带有@字符,和:

http://www.runoob.com/mongodb/mongodb-connections.html

中的:

mongodb://[username:password@]host1[:port1][,host2[:port2],…[,hostN[:portN]]][/[database][?options]]

格式有冲突啊

mongoDB 的URI中密码包含@字符,如何处理?

mongodb password contain @

node.js – MongoDB password with "@" in it – Stack Overflow

@写成%40,然后加上参数uri_decode_auth: true

但是时js的api

此处时Python的api,去看看是否有这个uri_decode_auth(方面的)参数

另外的人说是,换用参数的写法-》就又变成我之前的写法了。

How do I connect if the password contains an @ sign? · Issue #147 · mrvautin/adminMongo

去PyCharm中看MongoClient的源码,找找关于URI的解释:

看到了:

The `host` parameter can be a full `mongodb URI

<http://dochub.mongodb.org/core/connections>`_, in addition to

a simple hostname. It can also be a list of hostnames or

URIs. Any port specified in the host string(s) will override

the `port` parameter. If multiple mongodb URIs containing

database or auth information are passed, the last database,

username, and password present will be used.  For username and

passwords reserved characters like ‘:’, ‘/’, ‘+’ and ‘@’ must be

percent encoded following RFC 2396

中的:

For username and

passwords reserved characters like ‘:’, ‘/’, ‘+’ and ‘@’ must be

percent encoded following RFC 2396

说的就是:

用户名和密码,如果其中有特殊字符:

  • :

  • /

  • +

  • @

必须去编码才行

而具体写法,可以自己去找对于特殊字符的%xx的写法,也可以参考:

    uri = "mongodb://%s:%s@%s" % (

        quote_plus(user), quote_plus(password), host)

    client = MongoClient(uri)

Unix domain sockets are also supported. The socket path must be percent

encoded in the URI::

    uri = "mongodb://%s:%s@%s" % (

        quote_plus(user), quote_plus(password), quote_plus(socket_path))

    client = MongoClient(uri)

所以可以去试试了:

mongodbUri = "mongodb://%s:%s@%s" % (quote_plus("username"), quote_plus("P@wd"), quote_plus("x.x.x.x:27017"))

mongoClient = MongoClient(mongodbUri)

logging.info("mongoClient=%s", mongoClient)

是可以正常连接的:

【总结】

此处Python中处理MongoDB的话,用的driver是pymongo,其中对于:

MongoClient的话,传入的URI的话,密码中如果包含@等特殊字符的话,(用户名的处理方式也是类似的),则需要encode编码后,再传入:

from urllib import quote_plus

# mongodbUri = "mongodb://localhost:27017/"

mongodbUri = "mongodb://%s:%s@%s" % (quote_plus("username"), quote_plus(“xxx@yyy"), quote_plus("x.x.x.x:27017”))

# ‘mongodb://username:xxx%40yyy@x.x.x.x%3A27017′

mongoClient = MongoClient(mongodbUri)

即可正常连接MongoDB了。

【后记】

后来发现:

对于host和port中的冒号,不应该用quote_plus去encode

加上要访问的数据库,更复杂的写法,举例:

MongoHost = "x.x.x.x"

MongoPort = 27017

# with auth

MongoUsername = "gridfs"

MongoPassword = "P@wd"

MongoAuthenticationDatabase = "gridfs"

mongodbUri = "mongodb://%s:%s@%s:%s/%s" % (

    quote_plus(MongoUsername), \

    quote_plus(MongoPassword), \

    MongoHost, \

    MongoPort, \

    MongoAuthenticationDatabase \

)

#’mongodb://gridfs:P%40wd@x.x.x.x:27017/gridfs’

转载请注明:在路上 » 【已解决】MongoDB的用户的密码中包含@如何写URI

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.187 seconds, using 22.12MB memory