How to link my discord bot (.py) to my managed MySQL database. (Ubuntu 20.04)

I was following this tutorial ( and despite this when I start my bot it gives me this error.

Traceback (most recent call last):
  File "/home/peppe/.venvs/discord/lib/python3.8/site-packages/mysql/connector/", line 232, in _open_connection
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on '' (4)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 29, in <module>
    lmiibot_db = mysql.connector.connect(
  File "/home/peppe/.venvs/discord/lib/python3.8/site-packages/mysql/connector/", line 272, in connect
    return CMySQLConnection(*args, **kwargs)
  File "/home/peppe/.venvs/discord/lib/python3.8/site-packages/mysql/connector/", line 85, in __init__
  File "/home/peppe/.venvs/discord/lib/python3.8/site-packages/mysql/connector/", line 1003, in connect
  File "/home/peppe/.venvs/discord/lib/python3.8/site-packages/mysql/connector/", line 232, in _open_connection

This is the code I use to connect it

import mysql.connector

lmiibot_db = mysql.connector.connect(

How could I do? I’m going crazy

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
May 10, 2021
Accepted Answer

Hi there,

As far as I can see from the error you have not specified the correct Managed Database port and your python application is trying to connect on the default one:

Can't connect to MySQL server on '' 

To fix that, you need to specify the Managed MySQL database port.

Let me know how it goes! Regards, Bobby