pymongo-无法连接到在EC2上运行的mongodb [英] pymongo - Unable to connect to mongodb running on EC2

查看:115
本文介绍了pymongo-无法连接到在EC2上运行的mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am connecting to a mongodb server on EC2. The mongo collections require authentication to connect.

我尝试了所有操作,但出现以下错误,似乎无法纠正.

I tried everything but I am getting the following error and can't seem to correct it.

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)

推荐答案

我尝试了所有选项,终于奏效了.

I tried all the options and finally this worked.

client = MongoClient("mongodb://" + ssh_address+":27017") # No private key passing 
auth = client.myDB.authenticate(mongo_username,mongo_password) # Authenticate to myDB and not admin
db = client.myDB

因此,基本上,我不需要传递私钥(在将ssh传递到EC2时需要),因为该端口已经为所有传入IP打开了(我想这是我应该知道的一个重要事实并张贴在问题中.)

So basically I don't need to pass a private key (which is required when doing ssh over to the EC2) since the port was already opened for all incoming IPs ( I guess this was an important fact that I should have known and posted in the question).

我还试图通过admin DB进行身份验证,我不应该这样做,因为我只能访问myDB.

Also I was trying to authenticate via the admin DB, which I shouldn't have done because I was given access to myDB only.

这篇关于pymongo-无法连接到在EC2上运行的mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆