Python connection to rds mysql server

0

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 0.0.0.0/0. The code is giving the error Error: (2003, "Can't connect to MySQL server on 'database-2.cgsvu7bg7jl5.us-east-2.rds.amazonaws.com' (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 = 'database-2.extra_info.rds.amazonaws.com' 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:
        print(row)

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

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

1 Answer
0

Please check the contents of the document below.
https://repost.aws/knowledge-center/rds-connectivity-instance-subnet-vpc

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
EXPERT
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