Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Successfully access MySQL database from python using optuna

Tags:

python

mysql

Since the optuna documentation does not address which modules are required from MySQL, I installed everything of MySQL on my Windows 10 machine. I looked for MySQL on my PC (in which folder the installation takes place is not revealed during installation) and updated the Path variables to

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

I have successfully created the mysqltestexample database.

enter image description here

Using python SQL connectors, I can reproduce the output using:

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

Connection to the mysqltesteexample does not raise an error - so everything seems to be fine. However, optuna is not able to connect to my database

My python script looks like this. It is the code from the optuna documentation, I just altered the name of the test database.

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. Can you please help me to get it done? Thank you in advance, please don't be too harsh.

like image 398
StephanH Avatar asked Oct 21 '25 22:10

StephanH


1 Answers

Finally, I made it. I have to install some further packages from the cmd:

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

Python packages - as well documented as they are eyes rolling

like image 154
StephanH Avatar answered Oct 23 '25 10:10

StephanH