gpt4 book ai didi

python - pymongo - 无法连接到在 EC2 上运行的 mongodb

转载 作者:行者123 更新时间:2023-11-30 23:00:39 30 4
gpt4 key购买 nike

我正在连接到 EC2 上的 mongodb 服务器。 mongo 集合需要身份验证才能连接。

我尝试了所有方法,但出现以下错误,并且似乎无法纠正。

from pymongo import MongoClient

mongo_username = "username"
mongo_password = "password"
ssh_user = "user"
ssh_address = "ec2-**********.amazonaws.com"
ssh_port = 22
private_key = "path/to/key/mykey.pem"

def connect_to_mongo():
try:
client = MongoClient("mongodb://"+mongo_username+":"+mongo_password+"@" + ssh_address, ssl = True, ssl_keyfile = private_key)
db = client.myDB

#Should 'admin' be there or 'myDB'? 'admin' at least get if(auth) passed, while 'myDB' doesn't
auth = client.admin.authenticate(mongo_username,mongo_password)

if(auth):
print "MongoDB connection successful"
col = db.myCollection.count()
else:
print "MongoDB authentication failure: Please check the username or password"

client.close()

except Exception as e:
print "MongoDB connection failure: Please check the connection details"
print e

if __name__ == "__main__":
connect_to_mongo()

输出:

MongoDB connection successful
MongoDB connection failure: Please check the connection details
SSL handshake failed: EOF occurred in violation of protocol (_ssl.c:590)

最佳答案

EC2默认会关闭27017端口。按照描述创建入站规则 herehere .

关于python - pymongo - 无法连接到在 EC2 上运行的 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35206034/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com