Question

How to use Python to connect to DigitalOcean's mysql database

Hi everyone, hope someone could help. I am looking to use python to remotely connect to the DO’s MySQL database using the guide here:

https://www.digitalocean.com/community/tutorials/how-to-store-and-retrieve-data-in-mariadb-using-python-on-ubuntu-18-04

Python code used:

import os

import mysql.connector as database

username = os.environ.get("username")

password = os.environ.get("password")

host_db = os.environ.get("host_db")


db = os.environ.get("db")

connection = database.connect(
    user=username,
    password=password,
    host=host_db,
    database=db)

however, I keep getting the error:

2003 (HY000): Can’t connect to MySQL server on ‘db-mysql-nyc1-16578-do-user-13436704-0.b.db.ondigitalocean.com:3306’ (10060)

One thing I am guessing (need help to verify) is that I enforced SSl (a ca certificate). Do I need to include the ca cert information in the connection?

Hope someone could help!

Cheers, Derek


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.

KFSys
Site Moderator
Site Moderator badge
December 28, 2022

Hi @derekchong,

You need to allow your connection on your Managed Database. Check the following tutorial, I think it will be of help to you:

https://docs.digitalocean.com/products/databases/mysql/how-to/connect/

You can connect to DigitalOcean Managed Databases using command line tools and other third-party clients. This guide explains where to find your MySQL database’s connection details and how to use them to configure tools and clients.

alexdo
Site Moderator
Site Moderator badge
December 27, 2022

Hello @derekchong

Can you verify that the credentials are correct, including the port?

You can find the logs of your managed database cluster by following these steps:

  • Go to your DigitalOcean Control Panel
  • From the menu on the left, click on “Databases”
  • Then chose your database cluster
  • After that click on “Logs & Queries”
  • In there you can see your logs in real-time

Each managed database comes with an SSL certificate. You can use this SSL certificate to encrypt connections between your client applications and the database

When you configure your client applications, you can use the certificate’s location on your local system. Each client application is configured differently, so check the documentation for the tool you’re using for more detail on setting up SSL connections.

Regards