How to connect to remote MongoDB with mongo-connector?

时间:2016-10-20 18:52:15

标签: mongodb elasticsearch mongo-collection

How can I connect to a MongoDB cluster on Mongo Atlas using mongo-connector?

I have tried to connector to my cluster with the following commands:

First attempt

sudo mongo-connector -m "mongodb://g******:*********@rest-api-data-shard-00-00-xemv3.mongodb.net:27017,rest-api-data-shard-00-01-xemv3.mongodb.net:27017,rest-api-data-shard-00-02-xemv3.mongodb.net:27017/admin?ssl =true&replicaSet=rest-api-data-shard-0&authSource=admin" -a g****** -p "***********" -t http://localhost:9200 -d elastic2_doc_manager

Response:

Logging to mongo-connector.log. Exception in thread Thread-1: Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "/usr/local/lib/python2.7/site-packages/mongo_connector/util.py", line 90, in wrapped func(*args, **kwargs) File "/usr/local/lib/python2.7/site-packages/mongo_connector/connector.py", line 263, in run main_conn['admin'].authenticate(self.auth_username, self.auth_key) File "/usr/local/lib/python2.7/site-packages/pymongo/database.py", line 1018, in authenticate connect=True) File "/usr/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 434, in _cache_credentials raise OperationFailure('Another user is already authenticated ' OperationFailure: Another user is already authenticated to this database. You must logout first.

Second attempt:

sudo mongo-connector -m "mongodb://rest-api-data-shard-00-00-xemv3.mongodb.net:27017,rest-api-data-shard-00-01-xemv3.mongodb.net:27017,rest-api-data-shard-00-02-xemv3.mongodb.net:27017/admin?replicaSet=rest-api-data-shard-0" -a g********* -p "********" -t http://localhost:9200 -d elastic2_doc_manager

Response:

Logging to mongo-connector.log. Exception in thread Thread-1: Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "/usr/local/lib/python2.7/site-packages/mongo_connector/util.py", line 90, in wrapped func(*args, **kwargs) File "/usr/local/lib/python2.7/site-packages/mongo_connector/connector.py", line 263, in run main_conn['admin'].authenticate(self.auth_username, self.auth_key) File "/usr/local/lib/python2.7/site-packages/pymongo/database.py", line 1018, in authenticate connect=True) File "/usr/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 439, in _cache_credentials writable_preferred_server_selector) File "/usr/local/lib/python2.7/site-packages/pymongo/topology.py", line 210, in select_server address)) File "/usr/local/lib/python2.7/site-packages/pymongo/topology.py", line 186, in select_servers self._error_message(selector)) ServerSelectionTimeoutError: rest-api-data-shard-00-02-xemv3.mongodb.net:27017: [Errno 54] Connection reset by peer,rest-api-data-shard-00-00-xemv3.mongodb.net:27017: [Errno 54] Connection reset by peer,rest-api-data-shard-00-01-xemv3.mongodb.net:27017: [Errno 54] Connection reset by peer

2 个答案:

答案 0 :(得分:4)

github issue上回答。解决方案:

  

在您第一次尝试时,问题是您要为MongoDB指定两次用户名和密码。删除-a g ****** -p" ***********"它应该工作正常。如果需要对Elasticsearch进行身份验证,则需要使用mongo-connector配置文件并为Python Elasticsearch客户端设置正确的身份验证选项,例如:

     

{ "mainAddress": "mongodb://user:pass@mongodb:27017,mongodb:27018,mongodb:27019/admin?ssl=true&replicaSet=name&authSource=admin", "verbosity": 1, "docManagers": [ { "docManager": "elastic2_doc_manager", "targetURL": "http://localhost:9200", "args": { "clientOptions": { "http_auth": ["user", "secret"], "use_ssl": true } } } ] }

     

在您的第二次尝试中,问题是您忘记将ssl = true添加到MongoDB连接字符串。这就是为什么您通过对等错误重置连接的原因。

答案 1 :(得分:0)

该异常表示您正在尝试对同一数据库上的两个独立用户进行身份验证。驱动程序不允许这样做,以避免特权升级。您可以通过调用Database.logout()注销第一个用户。然后,您将能够成功验证第二个用户。

https://api.mongodb.org/python/current/api/pymongo/database.html#pymongo.database.Database.logout