pymysql 是否完全支持 MySQL 身份验证? [英] Does pymysql fully support MySQL authentication?
问题描述
我正在尝试使用通过 AWS Lambda 运行的函数连接到托管在 AWS RDS 上的 MySQL (5.7.22) 数据库,但由于身份验证无法使用 pymysql
连接到数据库错误:<代码>拒绝访问'<用户>'@'<主机>'(使用密码:YES)".奇怪的是,在我一直用于调试问题的 AWS 实例上,我发现我能够使用与 mysqlclient
相同的参数连接到数据库.
I am trying to connect to a MySQL (5.7.22) database hosted on AWS RDS using a function run through AWS Lambda, but am unable to connect to the database using pymysql
due to an authentication error: "Access denied for '<user>'@'<host>' (using password: YES)"
. Curiously, on an AWS instance that I've been using to debug the issue, I find that I am able to connect to the database using the same parameters with mysqlclient
.
如果相关,我的密码只包含字母数字字符.
In case it's relevant, my password only contains alphanumeric characters.
这是我使用 records
库(使用裸库产生相同结果)连接到带有两个包的数据库的最小示例:
Here is a minimal example I've used to connect to the database with the two packages using the records
library (using bare libraries yields the same results):
import pymysql
import MySQLdb
config = {} # Dictionary with database credentials.
MySQLdb.connect(
hostname=config['host'],
port=config["port"],
user=config['username'],
passwd=config['password'],
db=config['database'],
connect_timeout=5,
)
# Works fine
pymysql.connect(
hostname=config['host'],
port=config["port"],
user=config['username'],
passwd=config['password'],
db=config['database'],
connect_timeout=5,
)
# raises pymysql.err.OperationalError: (1045, "Access denied for user '<user>'@'<host>' (using password: YES)")
是否存在 pymysql
不支持的数据库配置或需要其他选项?
Are there database configurations that are not supported by pymysql
or require additional options?
推荐答案
原来问题是我需要指定一个 SSL CA(可以找到的 RDS 根证书 此处:
It turns out that the problem was that I needed to specify an SSL CA (the RDS root certificate that can be found here):
pymysql.connect(
hostname=config['host'],
port=config["port"],
user=config['username'],
passwd=config['password'],
db=config['database'],
connect_timeout=5,
ssl={"ca": "./rds-ca-2015-root.pem"},
)
我不完全确定 MySQLdb
包是如何避免这一步的.
I'm not entirely sure how the MySQLdb
package avoids this step.
这篇关于pymysql 是否完全支持 MySQL 身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!