UserX
UserX

Reputation: 1337

Can't connect to MySQL server (110) (AWS RDS Instance)

I have this code to connect to a rds instance:

import MySQLdb

USERNAME = 'root'
PASSWORD = 'pass'
DB_NAME = 'databasetest2'

print "Connecting to RDS instance"

conn = MySQLdb.connect ( host = 'mysql-db-instance-database-test2.code.us-east-1.rds.amazonaws.com', user = USERNAME, passwd = PASSWORD, db = DB_NAME, port = 3306)

print "Connected to RDS instance"

But Im having this error:

 Connecting to RDS instance
    Traceback (most recent call last):
      File "mysql.py", line 10, in <module>
        conn = MySQLdb.connect ( host = 'mysql-db-instance-database-test2.code.us-east-1.rds.amazonaws.com', user = USERNAME, passwd = PASSWORD, db = DB_NAME, port = 3306)
      File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
        return Connection(*args, **kwargs)
      File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
        super(Connection, self).__init__(*args, **kwargs2)
    _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'mysql-db-instance-database-test2.code.us-east-1.rds.amazonaws.com' (110)")

Do you see why Im having this error?

Upvotes: 6

Views: 21643

Answers (2)

Keith
Keith

Reputation: 77

The inability to connect to an Amazon RDS DB instance can have a number of root causes. Here are a few of the most common reasons :

  1. The RDS DB instance is in a state other than available, so it can't accept connections.

  2. The source you use to connect to the DB instance is missing from the sources authorized to access the DB instance in your security group, network access control lists (ACLs), or local firewalls.

  3. The wrong DNS name or endpoint was used to connect to the DB instance.

  4. The Multi-AZ DB instance failed over, and the secondary DB instance uses a subnet or route table that doesn't allow inbound connections.

  5. The user authentication is incorrect.

Resolution :

  1. Be sure that your DB instance is in the available state, If your DB instance is in the failed state,see : https://aws.amazon.com/premiumsupport/knowledge-center/rds-instance-failed-state/
  2. Be sure that your DB instance allows connections.
  3. Troubleshoot potential DNS name or endpoint issues
  4. Check the route tables associated with your Multi-AZ deployment
  5. Verify the connectivity

For a more detailed guideline refer to this aws guide : https://aws.amazon.com/premiumsupport/knowledge-center/rds-cannot-connect/ You can also use the AWSSupport-TroubleshootConnectivityToRDS to troubleshoot your issue :https://docs.aws.amazon.com/systems-manager-automation-runbooks/latest/userguide/automation-awssupport-troubleshootconnectivitytords.html

Hopefully This will help you resolve your issue 😊

Upvotes: 0

Evgeniy Kuzmin
Evgeniy Kuzmin

Reputation: 2462

YOu should check two points:

  1. EC2 Security Group assigned to your RDS. Check that incoming port 3306 is open for your RDS SG
  2. RDS VPC - you probably have several and check that it belongs to the same VPC where your EC2 instance is located

Upvotes: 8

Related Questions