简体   繁体   English

使用Python从EC2实例连接到RDS

[英]Connect to RDS from EC2 instance with Python

Struggling with connecting to a database through python running on an RDS instance. 努力通过在RDS实例上运行的python连接到数据库。

I've looked around at tutorials and I've tried this piece from amazon 我浏览了教程,并尝试了亚马逊的这篇文章

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'sa',
        'PASSWORD': 'mypwd',
        'HOST': 'mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com',
        'PORT': '3306',
    }
}

obviously I've substituted in my own details, but I'm not sure where to go from there. 显然,我已经替换了自己的详细信息,但是我不确定从那里去哪里。 It also seems to using something called Beanstalk, which unless is there automatically on all AWS instances, I don't have. 它似乎还使用了一种称为Beanstalk的东西,除非所有AWS实例上都没有它,否则我没有。 All I want to do is write some details to a table. 我要做的就是在表中写一些细节。 Is there a tutorial or some advice anyone can give me? 有没有人可以给我的教程或建议?

If you want to access the data in a raw python script, you will have to use a suitable mysql connector class for python, just passing the database details raw into the script won't help. 如果要访问原始python脚本中的数据,则必须为python使用合适的mysql连接器类,仅将原始数据库详细信息传递到脚本中将无济于事。

So, check out MySQLdb and mysql.connector then. 因此,然后检查MySQLdbmysql.connector

Also, do ensure that the RDS instance is iteslf accessible from the EC2 instance, and there is no permission/access discrepancy causing the issue. 另外,请确保可以从EC2实例访问RDS实例,并且没有引起问题的权限/访问差异。

mysql -usa -p -hmydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com mydb
#*sa* is the user in *-usa*

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM