2017-12-20 5 views
0

docker 컨테이너 내부의 데이터베이스에 연결하려고하며 다시 가정용 컴퓨터의 원격 서버에 연결하려고합니다. 도커 컨테이너 포트 27017은 서버 시스템의 포트 27017에 바인드됩니다.docker를 통한 인증 오류 pymongo

지금, 나는 누구의 목표는 내 집에있는 컴퓨터에서이 데이터베이스에 연결되는 Python3 스크립트가 : 나는 line 4에 중지 내 스크립트를 실행하면

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

을, 그것을 잘 작동,하지만 난 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. 

내가 뭘 잘못하고있어?

Thans in advance!

답변

0

누구나 같은 문제가 있다면 제 질문에 답해드립니다!

먼저 서버에 ssh을 통해 연결해야하므로 SSHTunnelForwardersshtunnel에서 가져오고 물론 pymongo에서 가져와야합니다.

from sshtunnel import SSHTunnelForwarder 
import pymongo 

다음, ssh을 통해 서버에 mongodb 연결 이제

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

당신이 로그인해야합니다 위해, 다른 손에, 한 손 서버 연결에 대한 매개 변수를 수정합니다.

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에 대한 연결을 작성해야합니다. 서버의 27017 포트가 도커 컨테이너의 27017 포트에 바인드되어 있지만, 가정용 시스템 포트 중 하나를 27017 서버 포트에 바인드 할 수 있습니다. 객체 ssh_connection은 수행 한 포트를 MONGO_PORTremote_bind_address 인수가 ssh_connection 인 바인드하는 로컬 포트를 제공합니다. 이렇게하면 도커로 주소 mongodb의 모든 길을 열 수 있습니다.

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 

그게 다야!