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:

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(

however, I keep getting the error:

2003 (HY000): Can’t connect to MySQL server on ‘’ (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

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.

Is this problem solved? I encounter the same issue. I could access mysql from my terminal, but just COULD NOT access it using python. I NEED TO USE PYTHON, NOT JUST THE TERMINAL.

“Error: (2003, “Can’t connect to MySQL server on ‘’ (timed out)”)”

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:

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.

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.


Try DigitalOcean for free

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

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

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

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
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