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

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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel