Django on EC2 using RDS - mysql

I have a Django App running on an EC2 instance and trying to connect to a RDS mysql Database instance.
I have created the RDS database and added a rule to the RDS Security group that allows for EC2 Security Group.
In my settings.py file, I have added the HOST to be the RDS EndPoint and the Port to be 3306 - along with the DB name, user and password.
I have installed libmysqlclient-dev, MySQL-python and mysql-client on the EC2 instance.
When I run ./manage.py syncdb, I get this error:
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")*
It looks like it is trying to connect to a local mysql server (which of course is not there, since I want it to connect to an RDS).
How can I get past this?

You don't set rds in default, or you loading wrong settings file. Python mysql trying to connect with localhost base, through socket (it's when HOST field is empty). You can try print DATABASE['default']['HOST'] in settings, or django shell to check host in settings.

Related

Can't Connect to RDS mysql DB from Wordpress on Amazon linux EC2 Instance

I am getting Error establishing a database connection while trying to connect to mysql on Amazon RDS from my Wordpress instance.
Wordpress is on my AWS Linux EC2 Instance, both are on the same VPC, same Security Group allowing 3306 inbound rule, db credentials in wp-config is also correct.
I am able to connect to my mysql RDS instance while ssh with the following command and it connects fine:
mysql -h "endpoint url" -u "username" -p "password";
However, when accessing the EC2 EIP on the browser to setup my WP final configuration I get the error connecting DB.
Did you check your security group? Because you have to open the MySQL Port 3306.
When you are creating the RDS, it is important to create a new security group, like myRDSgroup, later you have to modify it by open the port 3306 in the source select the security group where your AWS EC2 is located.
Could you send your security group screenshot, please?

"Can't connect to local MySQL Server" using SSH tunnel

Starting an SSH tunnel to MySQL so MySQL Workbench can connect to the remote DB using the following command:
ssh -L 3306:localhost:3306 <username>#<domain> -N
MySQL Workbench is able to connect without issue doing this.
I was also trying to spin up a local copy of the Django application and connect to the remote test DB and I get met with:
django.db.utils.OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/var/run/mysqld/mysqld.sock\' (2 "No such file or directory")'
Looking at the Django settings, everything looks correct. Just seems like Django is ignoring the tunnel despite using the same port. Django is in virtualenv so wonder if something there might be causing it.
Any suggestions?
I had to change the DB connection string in the settings.py to 127.0.0.1 instead of localhost.

Grant access to a EC2 and RDS instances [duplicate]

I'm using AWS for my project and Laravel 5. Just installed my laravel project in the EC2 and set up a DB in RDS. I imported the .sql file using mysql workbench. In my .env I set up the host, the DB name, user and password but when I go to the project in the browser I get an error:
PDOException in Connector.php line 55:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'blue*****.************.us-west-2.rds.amazonaws.com' (4)
Put the * for security reasons. So what could be wrong?
Update:
When I tried to connect to the RDS instance using the command line I got:
ERROR 2003 (HY000): Can't connect to MySQL server on 'blue****.**********.us-west-2.rds.amazonaws.com' (110)
The reason you are not able to connect to the RDS instance from the EC2 instance has to do with security group.
Before your EC2 instance can communicate with the RDS server, you need to configure a security group for the RDS.
You can check this and this for more details.

Can't connect a DB to AWS

I'm using AWS for my project and Laravel 5. Just installed my laravel project in the EC2 and set up a DB in RDS. I imported the .sql file using mysql workbench. In my .env I set up the host, the DB name, user and password but when I go to the project in the browser I get an error:
PDOException in Connector.php line 55:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'blue*****.************.us-west-2.rds.amazonaws.com' (4)
Put the * for security reasons. So what could be wrong?
Update:
When I tried to connect to the RDS instance using the command line I got:
ERROR 2003 (HY000): Can't connect to MySQL server on 'blue****.**********.us-west-2.rds.amazonaws.com' (110)
The reason you are not able to connect to the RDS instance from the EC2 instance has to do with security group.
Before your EC2 instance can communicate with the RDS server, you need to configure a security group for the RDS.
You can check this and this for more details.

Unable to connect to MySQL AWS RDS instance from local MySQL

I have created an MySQL RDS instance with VPC. Now i am trying to connect to that RDS instance from my Ubuntu 12.04 machine using MySQL client by following code:
mysql -u uname -h test.c6tjb4nxvlri.us-west-2.rds.amazonaws.com -P 3306 -p
But i am getting this error:
ERROR 2003 (HY000): Can't connect to MySQL server on 'test.c6tjb4nxvlri.us-west-2.rds.amazonaws.com' (110)
I searched about this error and everywhere solution came out like
Go to the Instances
Find the security group
Change the inbound rules of that security group by
Adding source of user machine public ip or
Set source ip as 0.0.0.0/16
I tried everything but still same error occures. Any explanations?
The problem was in subnet. Subnet that you created must be publicly accessible.
In On-premises MySQL Workbench, use TCP/IP SSH Tunneling option. Make sure you have EC2 instance endpoint and keypair file.
In SSH endpoint - add your EC2 instance endpoint and for SSH password, browse your keypair. Rest of configurations for MySQL. Like MySQL's endpoint, username, password, port and schema name.
Test your connection it will return success. If not, check RDS Security group. In Security group, you open MySQL port for all IP address. Try it! it will work. Once connection was success, all schema are visible in MySQL Workbench.
RDS DB instance need not to be in public subnet and it is not best practice to do. Always keep RDS DB instance in private subnet and open traffic for EC2 instance.
When you use TCP/IP SSH Tunneling, request traffic will send through EC2 instance to RDS DB instance.
HTH.