2017-12-20 6 views
0

ドッキング用のコンテナ内のデータベースに接続しようとしていますが、これは自宅のコンピュータのリモートサーバー内にあります。ドッカーコンテナポート27017は、サーバマシンのポート27017にバインドされています。認証エラーpymongo through docker

さて、私は目的の私の自宅のコンピュータからこのデータベースに接続されたの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. 

私は間違っていますか?

Thans in advance!

答えて

0

誰かが同じ問題を抱えていると私は自分の質問に答えます!

まず第一に、あなたはsshtunnelからSSHTunnelForwarderをインポートすると、当然のことながら、pymongoしなければならない、sshを介してサーバに接続する必要があり、そう。

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介して接続された

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. 

、あなたはドッキングウィンドウにmongodbへの接続を作成する必要があります。サーバーの27017ポートはドッカーコンテナの27017ポートにバインドされていますが、ホームマシンポートの1つを27017サーバーポートにバインドするように残ります。オブジェクトssh_connectionは、実行したポートをMONGO_PORTとしてremote_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 

それはすべての人々です!