Python connection to rds mysql server


Here is my python code to connect to an rds mysql instance. I have ensured that connections are publicly accessible and have a security group named default_ that accepts all traffic from source The code is giving the error Error: (2003, "Can't connect to MySQL server on '' (timed out)") I do not know what to do from here because I think it should be configured. Let me know if there are any steps I can take to debug.

import pymysql

Replace with your RDS instance details

host = '' user = 'admin' password = 'my_password' database = 'test'

try: # Establish a connection to the database connection = pymysql.connect(host=host, user=user, password=password, database=database)

# Create a cursor object to interact with the database
with connection.cursor() as cursor:
    # Execute SQL queries or database operations here

    # Example: Execute a SELECT query
    cursor.execute("SELECT * FROM your_table_name")
    results = cursor.fetchall()
    for row in results:

except Exception as e: print("Error: {}".format(e))

finally: # Close the database connection when done connection.close()

1 Answer

Please check the contents of the document below.

Since the error is a timeout error, the VPC network ACL settings or security group settings may be incorrect, or RDS public access may not be enabled.

profile picture
answered 8 months ago
  • How would I connect an existing vpc that has an internet gateway (currently using for an ec2 environment) to the rds?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions