简体   繁体   English

使用 pymongo 连接到 aws 时出现 ServerSelectionTimeoutError

[英]ServerSelectionTimeoutError when connecting to aws with pymongo

I am attempting to connect to MongoDB hosted on an AWS instance with a key file.我正在尝试使用密钥文件连接到托管在 AWS 实例上的 MongoDB。 I am able to ssh into the instance and connect to the database with no issues.我能够通过 ssh 进入实例并连接到数据库,没有任何问题。 When I try to connect to the database from a remote location with pymongo I receive this error: ServerSelectionTimeoutError: SSL handshake failed: EOF occurred in violation of protocol当我尝试使用 pymongo 从远程位置连接到数据库时,我收到此错误: ServerSelectionTimeoutError: SSL handshake failed: EOF occurred in violation of protocol

Port 27017 is open and the source is set to 0.0.0.0/0.端口 27017 已打开,源设置为 0.0.0.0/0。

from pymongo import MongoClient

client = MongoClient('mongodb://ec2-123-45-678-910.compute-1.amazonaws.com', 
                     27017, 
                     ssl=True, 
                     ssl_keyfile='/path_to/mykey.pem')

db = client.test
coll = db.foo
coll.insert_many(records)

ServerSelectionTimeoutError: SSL handshake failed: EOF occurred in violation of protocol (_ssl.c:645)

This question is nearly identical to mine, however the error is different and the solution posted there does not apply to my issue. 这个问题与我的几乎相同,但是错误不同,并且在那里发布的解决方案不适用于我的问题。

The address and key here have been changed, I have been going in circles on this for hours with no luck, any help would be appreciated.这里的地址和密钥已更改,我已经在这个问题上绕了几个小时没有运气,任何帮助将不胜感激。

This issue can cause because of following issue:由于以下问题,可能会导致此问题:

  1. version of pymongo (suggest to use 3.3.0, which worked for me) pymongo 版本(建议使用 3.3.0,这对我有用)

  2. It can be a DNS issue, etc, in fact you could check for a DNS issue using:这可能是 DNS 问题等,实际上您可以使用以下方法检查 DNS 问题:

telnet xx.xx.xx.xx port telnet xx.xx.xx.xx 端口

  1. can be a firewall issue可能是防火墙问题

  2. Can be an issue with ssl key.可能是 ssl 密钥的问题。 Try the following to test:尝试以下方法进行测试:

    import os
    
    import pymongo
    
    import ssl
    
    URL="url:port/db?ssl=true"
    
    client = pymongo.MongoClient(URL, ssl_cert_reqs=ssl.CERT_NONE)
    
    db = client.get_default_database()
    
    print db
    
    print db.collection_names()

I had the same problem (SSL handshake) with Pymongo module to connect to DocumentDB Azure (Data Base).我在使用 Pymongo 模块连接到 DocumentDB Azure(数据库)时遇到了同样的问题(SSL 握手)。

The error :错误:

ServerSelectionTimeoutError: SSL handshake failed: EOF occurred in violation of protocol (_ssl.c:590)

I was using pymongo==3.4.0我正在使用 pymongo==3.4.0

To resolve this :要解决此问题:

Change the version of pymongo by installing the 3.3.0 version通过安装 3.3.0 版本更改 pymongo 的版本

pip install pymongo==3.3.0

Try:尝试:

import pymongo pymongo.__version__

For me, the problem was that my Python setup only supported TLS 1.0 – not TLS 1.1 or above.对我来说,问题是我的 Python 设置只支持 TLS 1.0——不支持 TLS 1.1 或更高版本。

You can check it like this:你可以这样检查:

Python 3蟒蛇 3

> from urllib.request import urlopen
> urlopen('https://www.howsmyssl.com/a/check').read()

Python 2蟒蛇 2

> from urllib2 import urlopen
> urlopen('https://www.howsmyssl.com/a/check').read()

Check the output for the key tls_version .检查密钥tls_version的输出。 If it says TLS 1.0 and not TLS 1.1 or TLS 1.2 that could be the problem.如果它说的是TLS 1.0而不是TLS 1.1TLS 1.2 ,那可能是问题所在。

If you're using a virtualenv, be sure to run the command inside.如果您使用的是 virtualenv,请确保在其中运行命令。

Solution: Install Python with a newer version of OpenSSL解决方案:使用较新版本的 OpenSSL 安装 Python

In order support TLS 1.1 or above, you may need to install a newer version of OpenSSL, and install Python again afterwards.为了支持 TLS 1.1 或更高版本,您可能需要安装更新版本的 OpenSSL,然后再安装 Python。 This should give you a Python that supports TLS 1.1.这应该会给你一个支持 TLS 1.1 的 Python。

The process depends on your operating system – here's a guide for OS X .该过程取决于您的操作系统 - 这是OS X指南。

virtualenv users虚拟环境用户
For me, the Python outside of my virtualenv had TLS 1.2 support, so just I removed my old virtualenv, and created a new one with the same packages and then it worked.对我来说,virtualenv 之外的 Python 支持 TLS 1.2,所以我删除了旧的 virtualenv,并使用相同的包创建了一个新的,然后它就可以工作了。 Easy peasy!轻轻松松!

See also:另见:

  • The warning about TLS 1.0 in the Python 3 section in the PyMongo documenation. PyMongo 文档中 Python 3 部分中有关 TLS 1.0 的警告 Although it's under the Python 3 section it also applies to Python 2尽管它属于 Python 3 部分,但它也适用于 Python 2

I had the same issue and talked for 30 minutes with the Mongo Atlas support which deployed over AWS.我遇到了同样的问题,并与部署在 AWS 上的 Mongo Atlas 支持进行了 30 分钟的讨论。 I run the next terminal command:我运行下一个终端命令:

/Applications/Python\ 3.6/Install\ Certificates.command

I had the same issue.我有同样的问题。 Please check if you are connected via VPN.请检查您是否通过 VPN 连接。 when I disconnected it resolved my problem.当我断开连接时,它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 pymongo 连接到 Atlas MongoDb - ServerSelectionTimeoutError - Connecting to Atlas MongoDb using pymongo - ServerSelectionTimeoutError ServerSelectionTimeoutError使用PyMongo和x509 SSL证书连接到MongoDB数据库时 - ServerSelectionTimeoutError When connecting to MongoDB Database with PyMongo and x509 SSL Certificate ServerSelectionTimeoutError Pymongo - ServerSelectionTimeoutError Pymongo 连接到 mongo 图集给出 pymongo.errors.ServerSelectionTimeoutError: localhost:27017 - Connecting to mongo atlas gives pymongo.errors.ServerSelectionTimeoutError: localhost:27017 Pymongo pymongo.errors.ServerSelectionTimeoutError 使用示例代码时 - Pymongo pymongo.errors.ServerSelectionTimeoutError when using example code 尝试连接到 CosmosDB 数据库时出现 pymongo.errors.ServerSelectionTimeoutError - Getting pymongo.errors.ServerSelectionTimeoutError when attempting to connect to a CosmosDB database 为什么 PyMongo 3 给出 ServerSelectionTimeoutError? - Why is PyMongo 3 giving ServerSelectionTimeoutError? pymongo.errors.ServerSelectionTimeoutError - pymongo.errors.ServerSelectionTimeoutError 在 aws lambda 中通过 pymongo 连接 documentdb 时任务超时 - Task timed out when connecting documentdb through pymongo in aws lambda 升级到PyMongo 3.0导致ServerSelectionTimeoutError - Upgrade to PyMongo 3.0 Resulting in ServerSelectionTimeoutError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM