0

I'm running MySQL in a Docker container, and in my jupyter lab I have the following cell:

# Importing module
import mysql.connector

# Creating connection object
mydb = mysql.connector.connect(
    host = "localhost",
    port = 3307,
    user = "bob",
    password = "1234",
    database = 'testDB'
)

# Printing the connection object
print(mydb)

Which prints: <mysql.connector.connection_cext.CMySQLConnection object at 0x7fc633769dd0>

But I want to connect using ipython-sql, so I load the extension with %load_ext sql, but when I try:

%sql mysql://bob:1234@localhost:3307/testDB

I get the error:

Connection info needed in SQLAlchemy format, example:
               postgresql://username:password@hostname/dbname
               or an existing connection: dict_keys([])
No module named 'MySQLdb'
Connection info needed in SQLAlchemy format, example:
               postgresql://username:password@hostname/dbname
               or an existing connection: dict_keys([])
Bobby Wan-Kenobi
  • 885
  • 9
  • 18

1 Answers1

0

It looks like you're missing the MySQL driver in your Jupyter Lab? Sometimes the lab is running on a different kernel than your environment.

Try running: %pip install mysqlclient --quiet or via conda %conda install mysqlclient -c conda-forge --quiet

You can also follow https://jupysql.ploomber.io/en/latest/integrations/mysql.html?highlight=mysql which takes you step by step on the exact use case you're asking about (connecting to a docker Mysql via Jupyter lab).

Jupysql is an ipython-sql fork, hence it has backward compatibility, so this guide should be pretty useful for you!

Just to compare the same thing, you can try what you did with the SQLAlchemy engine, but pass the connection string instead of passing each parameter separately.

Ido Michael
  • 109
  • 7