我正在連接到EC2上的mongodb服務器。 mongo集合需要認證才能連接。pymongo - 無法連接到在EC2上運行的mongodb
我嘗試了一切,但我得到以下錯誤,似乎無法糾正它。
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)
我的合作開發者之一是通過Java接口訪問mongodb,他能夠做到這一點。 你還認爲這可能是原因嗎? – Shivendra
這個Java實例是否在同一個VPC下運行?你是否從EC2的VPC之外訪問? – user3
我不知道第一個問題的答案,但我從家庭網絡訪問EC2。 – Shivendra