问题
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.
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)")
Are there database configurations that are not supported by pymysql or require additional options?
回答1:
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"},
)
I'm not entirely sure how the MySQLdb package avoids this step.
来源:https://stackoverflow.com/questions/59331500/does-pymysql-fully-support-mysql-authentication