リモートロケーション認証ソリューションpymongo.errors.ServerSelectionTimeoutError:XX。:27017:[Errno111]接続が拒否されました



Remote Location Authentication Solutions Pymongo



mongodbリモート認証サーバーは、次のエラーを報告しました:

Traceback (most recent call last): File 'run_cloudweb.py', line 109, in operMongodb.ResourceThread.init_db(etc.config.MONGO_IP,int(etc.config.MONGO_PORT),etc.config.MONGO_DB,etc.config.MONGO_USER,etc.config.MONGO_PWD) File '/root/web/cloudweb_py3/operMongodb.py', line 281, in init_db db.authenticate(api_key,secret_key,mechanism='SCRAM-SHA-1') File '/usr/local/lib/python3.7/dist-packages/pymongo/database.py', line 1471, in authenticate connect=True) File '/usr/local/lib/python3.7/dist-packages/pymongo/mongo_client.py', line 751, in _cache_credentials writable_preferred_server_selector) File '/usr/local/lib/python3.7/dist-packages/pymongo/topology.py', line 231, in select_server address)) File '/usr/local/lib/python3.7/dist-packages/pymongo/topology.py', line 189, in select_servers selector, server_timeout, address) File '/usr/local/lib/python3.7/dist-packages/pymongo/topology.py', line 205, in _select_servers_loop self._error_message(selector)) pymongo.errors.ServerSelectionTimeoutError: XX.XX.XX.XX:27017: [Errno 111] Connection refused

さまざまなチェックの結果、次のような問題が見つかりました。



1. bind_ipの/etc/mongodb.confリモートサーバー構成を変更します。デフォルトは127.0.0.1で、0.0.0.0に変更されています。またはプラスオープンIP(例:180.77.22.12,80.77.22.13)

bind_ip = 0.0.0.0

次に、サービスmongodbrestartを再起動します



2.「SCRAM-SHA-1」を追加する時間を確認します

from pymongo import MongoClient #ip ip mongo is located, port 27017 by default con = MongoClient(ip, port) #admin mongo is in the account password authentication library db = con['admin'] # Mongo3.0.3 or later, joined the SCRAM-SHA-1 check mode db.authenticate(user, passwd, mechanism='SCRAM-SHA-1')

これでOKです