Question

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

I was following this tutorial (https://www.digitalocean.com/community/tutorials/how-to-connect-to-managed-database-ubuntu-18-04#connecting-to-a-managed-mysql-database) 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/connection_cext.py", line 232, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on 'discordbotdatabase....com:3306' (4)

During handling of the above exception, another exception occurred:

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

This is the code I use to connect it

import mysql.connector

lmiibot_db = mysql.connector.connect(
    host="discordbotdatabase...com",
    user="doadmin",
    passwd="password",
    database="lmiibot",
    auth_plugin="mysql_native_password",
)

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 'discordbotdatabase....com:3306' 

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

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