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.

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

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.