How do I get sqlalchemy.create_engine with mysqlconnector to connect using mysql_native_password?

萝らか妹 提交于 2021-01-29 17:28:41

问题


I'm working with pandas and sqlalchemy, and would like to load a DataFrame into a MySQL database. I'm currently using this code snippet:

db_connection = sqlalchemy.create_engine('mysql+mysqlconnector://user:pwd@hostname/db_name')

some_data_ref.to_sql(con=db_connection, name='db_table_name', if_exists='replace')

sqlalchemy, pandas have been imported prior to this.

My MySQL backend is 8.x, which I know uses caching_sha2_password. If I were to connect to the database using mysql.connector.connect and I want to use the mysql_native_password method, I know that I should specify auth_plugin = mysql_native_password like so:

mysql.connector.connect(user=user, password=pw, host=host, database=db, auth_plugin='mysql_native_password')

My question: Is there a way to force mysql_native_password authentication with sqlalchemy.create_engine('mysql+mysqlconnector://...)?

Any advice on this would be much appreciated...


回答1:


You could use connect_args:

db_connection = sqlalchemy.create_engine(
    'mysql+mysqlconnector://user:pwd@hostname/db_name',
    connect_args={'auth_plugin': 'mysql_native_password'})

or the URL query:

db_connection = sqlalchemy.create_engine(
    'mysql+mysqlconnector://user:pwd@hostname/db_name?auth_plugin=mysql_native_password')


来源:https://stackoverflow.com/questions/57459623/does-sqlalchemy-support-caching-sha2-password-if-yes-then-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!