简体   繁体   中英

Unable to connect to AWS RDS MySQL from php

I am new to AWS and have set up a mysql RDS instance.

I am able to connect from my laptop using MySQLWorkbench but when I try to connect with the following code from one of my servers (not an aws server) I get the following error:

Warning: mysqli::mysqli(): (HY000/2002): Connection timed out in /home/asmserverco/public_html/sms/login.php on line 28 Connection failed: Connection timed out

Here is the php connection code:

$servername = "****.us-west-2.rds.amazonaws.com";
$username = "awsasm";
$password = "****";
$dbname = "sms";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

I have set all traffic on the instance's security group to be allowed from any IP.

why can I connect from my laptop but not my web sever?

Considering you are running CentOS with SELinux enabled it's always good idea to check audit file.

/var/log/audit/audit.log

That being said I'm certain the problem is with missing network flag. Run below as root.

setsebool -P httpd_can_network_connect 1

And try to access again.

Assuming you did not modify NACLs, check that RDS allows connection to port 3306 from your server's IP. If you want to open it for all (which is not secure therefore not recommended) it should be 0.0.0.0/0.

Also, make sure 'Publicly Accessible' is set to 'yes'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM