Amazon EC2 Linux RH cannt connect from php script to RDS MYSQL - php

I have an amazon EC2 instance (redhat linux ) and a RDS (mysql 5.5 ). After stop /start EC2 instance. EC2's ip was changed and cannt connect RDS anymore. I can successfully connect RDS instance from command line. However, it is unable to connect from php script and give me this warning.
PHP Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on 'applicantdb.xr3x2xcale.eu-west-1.rds' (13)
It is working fine from my localhost and I can connect to localhost. It is really annoying and if somebody knows the solution. Please help me and I do really appreciate it.

Thanks layke , the problem solved
The solution was
It restricts Apache remote database connection as default. So our scripts work in command line (# php sqltest.php), and mysql can connect to server but when Apache is involved (calling a php script as web page) then it doesn't work.
Setting:
setsebool -P httpd_can_network_connect=1

You need to in your RDS Security group make sure that your Amazon EC2 security profile has access.

Related

Database error:No connection could be made because the target machine actively refused it using EC2 + Linux + XAMPP

I've got an EC2 instance running with a Linux server, and on it I've installed XAMPP.
I have a chrome extension which needs to be able to connect to the DB created through phpMyAdmin, but when trying to connect I get the following error: Database error:No connection could be made because the target machine actively refused it.
How can I allow an external server access to the database?
Did you give this a try? It's unfortunate, but the only way I was able to find a chrome extension to allow CORS was with this extension.
Hope this helps.

AWS, ec2 and RDS mysqli::real_connect(): Connection timed out

I have set Ec2 Linux server with only PHP is installed in it not mysql.
I have separately configured RDS
Installed workbench and logged in using endpoint of RDS and username and password
In workbench DB connection done. I imported my local DB to RDS
Now In my Ec2 I onlu have PHP not mysql as my db is in RDS
In my ec2 php is running In check it with simple hello world echo file.
But mysqli_connect script is giving connection time out error. I used the same credentials as I used to connect to workbench. And I am also using the right script of mysqli_connect.
My doubt is Is it because of I did not installed mysql in my ec2 linux server. But Why I need mysql in ec2 if I have successfully setup DB in RDS.
First thing is that to install mysql-client and try that again. There can be several issues if it doesn't work even after trying to connect with mysql-client and consider those as well.
The state of the RDS instance should be 'available'. Otherwise it won't connect to any.
Check whether your EC2 instance IAM role has the permission which needs to access RDS instance. If your IAM role doesn't have permission then it won't get permission to connect and it will timeout after a while.
*It's better if you can post some code here.

AWS RDS: How to access/connect to RDS MySql DB From localhost

I am very new to Amazon Web Services, I have created Mysql DB on RDS and stored some data.
Now i want to connect to RDS db from localhost(which is in running on my desktop) ,Tried as fallow in my PHP connection.
$con = mysql_connect("XXXX.XXXXX.XX-XX-X.rds.amazonaws.com:3306", "XXX", "XXX");
But getting error as fallows
Could not connect: Can't connect to MySQL server on 'awsniti.XXXX.rds.amazonaws.com'
Also tried giving my local IP address to Inbounds of Security Group. But couldn't connect.
googled a lot, but not able to find right answer.
Please help me with this issue.
Adding the internet IP of your local network/machine on the security group associated with your RDS database should work. But I prefer to create a SSH tunnel through a server that I already gave access to the database. This way you don't have to care about the security group configuration every time you need to connect from a different IP address.
If you are on Linux or OSX you can run locally
ssh -f user#your-web-server.com -L 3306:XXXX.XXXXX.XX-XX-X.rds.amazonaws.com:3306 -N and then connect localhost:3306 from PHP to access the RDS database. If you are on Windows it's also possible to create a tunnel using Putty (http://bit.ly/1GQmaYo)

SQLSTATE[HY000] [2003] Can't connect to MySQL server

I am just now migrating an application from a standard dedicated server that I have onto AWS EC2.
I have also moved the database (MySQL) onto Amazon RDS.
Now I have an issue that I am pretty sure has to do with either PHP or Apache configuration.
On my dedicated server I can now reach the Amazon RDS database without a problem, but on the EC2-instance, using the exact same code I get this message:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on ....................eu-west-1.rds.amazonaws.com
I can reach the db-server both from Sequel Pro on my laptop, and from PHP on my other server.
I tried to put in the IP-address for the MySQL db on my other server, but same error message, so I am thinking there is some configuration either in Apache or PHP that I need to do?
It is a EC2 instance running Apache and PHP on CentOS.
Suggestions as to what configuration could be blocking this connection?
To anyone else who might have the same problem, run this in SSH, it worked for me:
setsebool -P httpd_can_network_connect=1
Tried successfully
setsebool -P httpd_can_network_connect=1
on RHEL image on EC2 and it was able to connect with AWS-RDS in the same VPC.
For more information see. This is a MUST step on RHEL
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/sect-Security-Enhanced_Linux-Booleans-Configuring_Booleans.html

Amazon AWS server with database on localhost, how do I ssh so that my php webservices on localhost can run on it?

Server newbie here!
I have access to an amazon AWS server, and I have a php webservice on my localhost. The php is working properly on the webservice. The MySQL is set up on the server. However, I cannot connect to my server. I set up everything on the amazon AWS server by SSH-ing in to it and working from there. Is there a way to allow me to ssh into the server with my webservice?
Just to reiterate:
-My php script is at localhost/ ...
-My server is accessed by ssh and then on my localhost, it is hosted by amazon aws (default port 3306)
I attempted the process suggested by one user which was to enter the following into my terminal:
ssh -L 8080:127.0.0.1:3306 -f -C -q -N -i ./password.pem user#host.com
and then when I used mysqli connect I entered:
new mysqli('127.0.0.1:8080','root','mypassword');
Thanks so much for any assistance!
It sounds like you are trying to connect to your remote database from your local server (if I understand correctly). You want to double check your security groups in AWS that they are allowing your computers ip address to connect to the server instance / that port 3306 is open to your pc. If that is all set, then you should be able to connect to the instance using the public dns or elastic ip if it is an EC2 instance, or the endpoint if it is an RDS instance.

Categories