使用optuna从python成功访问MySQL数据库

2024-06-16 11:12:07 发布

您现在位置:Python中文网/ 问答频道 /正文

由于optuna文档没有说明MySQL需要哪些模块,所以我在Windows10机器上安装了MySQL的所有内容。我在我的PC上查找MySQL(安装过程中不会显示安装所在的文件夹),并将路径变量更新为

C:\Program Files\MySQL\MySQL服务器8.0\bin

我已成功创建了mysqltestexample数据库

enter image description here

使用python SQL连接器,我可以使用以下方法复制输出:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="Start123"
)

print(mydb)


mycursor = mydb.cursor()

mycursor.execute("SHOW DATABASES")

for x in mycursor:
  print(x) 

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="Start123",
  database="mysqltesteexample"
) 

连接到mysqltesteexample不会引发错误-因此一切似乎都很好。但是,optuna无法连接到我的数据库

我的python脚本如下所示。这是来自optuna文档的代码,我刚刚更改了测试数据库的名称

study0 = optuna.create_study(storage="mysql://root@localhost/mysqltesteexample",study_name="distributed-example")
study0 = optuna.create_study(storage="mysql+pymysql://root:Start123@localhost:3306/mysqltesteexample",study_name="distributed-example")

根据https://docs.sqlalchemy.org/en/14/core/engines.html修改URL字符串的所有尝试均失败,错误如下:ImportError: Failed to import DB access module for the specified storage URL. Please install appropriate one. 你能帮我把它做完吗?提前谢谢你,请不要太苛刻


Tags: 文档import数据库localhostconnectormysqlstorageroot
1条回答
网友
1楼 · 发布于 2024-06-16 11:12:07

最后,我成功了。我必须从cmd安装更多的软件包:

py -3.8 -m easy_install mysql-python
py -3.8 -m pip install mysqlclient

Python软件包-有着丰富的文档,因为它们是令人眼花缭乱的

相关问题 更多 >