Search code examples
pythonmysqldjangoamazon-rds

Amazon RDS: OperationalError: (2003, "Can't connect to MySQL server on rds.amazonaws.com (110)")


I have set up an Amazon Relational Database Service (RDS) with a MySQL database and I'm trying to connect it with my django application but I'm getting the above error. My database settings in settings.py are as follows:

DATABASES = {
'default': {
    'ENGINE': config('db_engine'),
    'NAME': config('db_name'),
    'USER': config('db_user'),
    'PASSWORD': config('db_password'),
    'HOST': config('db_host'),
    'PORT': config('db_port')
}

}

and my database environment variables are as follows:

db_engine=django.db.backends.mysql
db_name=stagingdatabase
db_user=staginguser
db_password=stagingpassword
db_host=somestagingname.somestagingid.us-east-2.rds.amazonaws.com
db_port=3306

I know the port is correct, which is the solution for most cases having these types of errors. All other variables are correct as well.

I can't ping somestagingname.somestagingid.us-east-2.rds.amazonaws.com even though its status is available, so there must be an issue in how I setup the RDS:

enter image description here


Solution

  • This seems to be a network issue, the Django server is not able to reach the database, this can happen for diverse issues:

    • They are in different and unconnected VPNs or Networks.

    • The security group for the EC2 machine where Django is running is not allowing this outbound traffic to this network-port combination.

    • The security group in the RDS is not allowing inbound traffic from the network where the EC2 machine is for the 3306 port.

    If you are in an sceneario where you are using the default networking provided with the AWS Account it is likely to discard the first scenario. Also, by default all outgoing traffic is allowed for EC2 machines.

    So my best suggestion would to start checking on the security group that controls inbound traffic to your RDS.

    For more info: https://docs.aws.amazon.com/en_us/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html