简体   繁体   中英

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

I have used SQLalchemy in python3 to connect to local MySQL. However, I got such error: sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

The password is the same as in MySQL workbench, that is "root", I do not know why I got this error.

My code:

engine = create_engine("mysql+pymysql://root:root@localhost/db?host=localhost?port=3308", echo=True)


with engine.connect() as conn, conn.begin():
    book_df.to_sql(name='test', con=conn, if_exists='append', index=False)

Already answered at the top, but if you want connect to RDS and if you have special characters in your password like "@" be sure to use urllib.parse.quote or else you will encounter the same error.

import urllib

user = "user"
password = urllib.parse.quote("p@ssword")
endpoint = "rds_endpoint"
port = "port"
db = "db_name"
engine = create_engine(f"mysql+pymysql://{user}:{password}@{endpoint}:{port}/{db}", echo=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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