繁体   English   中英

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

[英]Successfully access MySQL database from python using optuna

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

C:\Program Files\MySQL\MySQL Server 8.0\bin

我已经成功创建了mysqltestexample数据库。

在此处输入图像描述

使用 python SQL 连接器,我可以重现 output 使用:

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")

All attempts to modify the URL string according to https://docs.sqlalchemy.org/en/14/core/engines.html failed with the following error: ImportError: Failed to import DB access module for the specified storage URL. Please install appropriate one. ImportError: Failed to import DB access module for the specified storage URL. Please install appropriate one. 你能帮我完成它吗? 提前谢谢你,请不要太苛刻。

最后,我做到了。 我必须从 cmd 安装一些进一步的软件包:

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

Python包 - 有据可查

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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