身份验证错误pymongo通过docker

问题描述 投票:0回答:1

我正在尝试连接到docker容器内的数据库,然后在家用计算机的远程服务器中连接它。 docker container port 27017绑定到服务器机器的27017端口。

现在,我有一个Python3脚本,其目的是从我的家用计算机连接到这个数据库:

from pymongo import MongoClient
client=MongoClient('mongodb://myserverusername:[email protected]:27017')
database=client["my_collection"]
cursor=database["my_collection"].find({})
print(next(cursor))

如果我执行我的脚本停止在line 4,它工作正常,但当我释放line 5时,我收到以下错误:

Traceback (most recent call last):
  File "testDatabase.py", line 9, in <module>
    print(next(cursor))
  File "[...]/lib/python3.5/site-packages/pymongo/cursor.py", line 1132, in next
    if len(self.__data) or self._refresh():
  File "[...]/lib/python3.5/site-packages/pymongo/cursor.py", line 1055, in _refresh
    self.__collation))
  File "[...]/lib/python3.5/site-packages/pymongo/cursor.py", line 892, in __send_message
    **kwargs)
  File "[...]/lib/python3.5/site-packages/pymongo/mongo_client.py", line 950, in _send_message_with_response
    exhaust)
  File "[...]/lib/python3.5/site-packages/pymongo/mongo_client.py", line 961, in _reset_on_error
    return func(*args, **kwargs)
  File "[...]/lib/python3.5/site-packages/pymongo/server.py", line 99, in send_message_with_response
    with self.get_socket(all_credentials, exhaust) as sock_info:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "[...]/lib/python3.5/site-packages/pymongo/server.py", line 168, in get_socket
    with self.pool.get_socket(all_credentials, checkout) as sock_info:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "[...]/lib/python3.5/site-packages/pymongo/pool.py", line 852, in get_socket
    sock_info.check_auth(all_credentials)
  File "[...]/lib/python3.5/site-packages/pymongo/pool.py", line 570, in check_auth
    auth.authenticate(credentials, self)
  File "[...]/lib/python3.5/site-packages/pymongo/auth.py", line 486, in authenticate
    auth_func(credentials, sock_info)
  File "[...]/lib/python3.5/site-packages/pymongo/auth.py", line 466, in _authenticate_default
    return _authenticate_scram_sha1(credentials, sock_info)
  File "[...]/lib/python3.5/site-packages/pymongo/auth.py", line 209, in _authenticate_scram_sha1
    res = sock_info.command(source, cmd)
  File "[...]/lib/python3.5/site-packages/pymongo/pool.py", line 477, in command
    collation=collation)
  File "[...]/lib/python3.5/site-packages/pymongo/network.py", line 116, in command
    parse_write_concern_error=parse_write_concern_error)
  File "[...]/lib/python3.5/site-packages/pymongo/helpers.py", line 210, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Authentication failed.

我做错了什么?

提前谢谢!

python mongodb docker server pymongo
1个回答
0
投票

我回答我自己的问题,如果有人有同样的问题!

首先,您需要通过ssh连接到服务器,因此,您必须从SSHTunnelForwarder导入sshtunnel,当然还有pymongo

from sshtunnel import SSHTunnelForwarder
import pymongo

接下来,一方面修改服务器连接的参数,另一方面修复mongodb连接的参数

SERVER_HOST = "your.server.host.com"
SERVER_USER = "yourserverusername"
SERVER_PASS = "yourseverpassword"
MONGO_DB = "your_database_name"
MONGO_PORT = 27017

现在您必须通过ssh登录服务器。

ssh_connection = SSHTunnelForwarder(
    SERVER_HOST,
    ssh_username=SERVER_USER,
    ssh_password=SERVER_PASS,
    remote_bind_address=("localhost", MONGO_PORT) #localhost:27017 is mongodb url into server 
)

ssh_connection.start() #Start connection, you now are connected to server via ssh.

通过ssh连接后,您必须创建与mongodb连接到docker的连接。值得一提的是,虽然服务器中的27017端口绑定到docker容器中的27017端口,但仍然可以将您的一个主机端口绑定到27017服务器端口。对象ssh_connection为您提供了一个本地端口,它在MONGO_PORTremote_bind_address参数中将您实现的端口绑定为ssh_connection。这允许您一直解锁到mongodb地址到docker。

print(ssh_connection.local_bind_port) #When you start, mongodb is assigned to a local port for this connection, so this is the port you must use 
client = pymongo.MongoClient('127.0.0.1:{}'.format(ssh_connection.local_bind_port))

现在,您可以随心所欲地使用数据库。在这种情况下,我执行原始帖子的示例的类似行为。

db = client[MONGO_DB]
cursor=db[MONGO_DB].find({})
for r in cursor:
    print(r)
print(cursor.count())

将所有任务完成到数据库后,必须关闭ssh会话。

ssh_connection.stop() #End ssh connection

这就是所有人!

© www.soinside.com 2019 - 2024. All rights reserved.