gpt4 book ai didi

python - 如何让 sqlalchemy.create_engine 与 mysqlconnector 使用 mysql_native_password 进行连接?

转载 作者:行者123 更新时间:2023-11-29 15:42:29 24 4
gpt4 key购买 nike

我正在使用 pandas 和 sqlalchemy,并且希望将 DataFrame 加载到 MySQL 数据库中。我目前正在使用此代码片段:

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在此之前已导入。

我的 MySQL 后端是 8.x,我知道它使用 caching_sha2_password 。如果我使用 mysql.connector.connect 连接到数据库我想使用 mysql_native_password方法,我知道我应该指定 auth_plugin = mysql_native_password像这样:

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

我的问题:有没有办法强制mysql_native_password使用 sqlalchemy.create_engine('mysql+mysqlconnector://...) 进行身份验证?

对此的任何建议将不胜感激......

最佳答案

您可以使用connect_args :

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

或 URL query :

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

关于python - 如何让 sqlalchemy.create_engine 与 mysqlconnector 使用 mysql_native_password 进行连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57459623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com