简体   繁体   中英

Connect to Amazon RDS with PHP

I am trying to connect my RDS Instance with my PHP connection file.

This is what I have in my file:

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'User Name');
define('DB_PASSWORD', 'Password');
define('DB_DATABASE', 'DATABASE');

$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());

I replaced localhost with my endpoint (rds thing) url, username and password with my RDS Instance user and pass and database name the one I've set when I created the instance. But it doesn't seem to work.

Is there anything else I have to do that I am not aware of or should it work?

RDS instances do not have a static IP address attached to them, you always use the endpoint for the host. For example:

database1.jlsdfjhgsdf.us-east-1.rds.amazonaws.com

If you are connecting to the instance with a username other than the root database account, you will need to grant the user privileges.

Check security group settings. If you are connecting from another EC2 instance on Amazon you will need to attach that security group. If you are trying to connect from outside AWS, you will need to whitelist the IP address you are accessing from.

Some ideas:

  • Try using the actual IP of the instance, then it should work.
  • Did you authorized access to your DB instance?
  • You may want to have a look at Get Started with Amazon RDS to properly setup your RDS instance

I was facing a similar issue whilst trying to connect an EC2 Apache server using PHP to the RDS MySQL instance. Weirdly I could establish a connection via CLI - once in mysql running status will tell you which user youre logged in with, plus the port, server name etc. Turned out some AMI images have SELinux enforcement - meaning the apache server cant send network requests as pointed out by this gentlemen ( http://www.filonov.com/2009/08/07/sqlstatehy000-2003-cant-connect-to-mysql-server-on-xxx-xxx-xxx-xxx-13/ )

Other points:

  • Make sure inbound ports are set for your RDS DB
  • In MySQL make sure the host is set to '%' as opposed to localhost
  • Always use the endpoint string to connect as the RDS IP changes

I was recently having a lot of trouble with this also but was able to fix it. I made sure my security groups (for the RDS and for EC2) were allowing each other. I was able to run my script from the terminal and connect to my database also from the terminal, but I couldn't get the script to run/connect to MySQL from a browser. It turns out I did not have mysql-server installed-- once I installed that and restarted httpd and mysqld it worked like a charm.

This article is what led me to installing mysql-server and the service starts/restarts. Hope it helps! -- http://www.rndmr.com/amazon-aws-ec2-easy-web-serverset-up-guide-with-a-mac-and-coda-2-256/

单击菜单上的(橙色)。

Just accepts all incoming connections.

I also had the connection problem between the ec2 (apache + php server) and the RDS (Mysql server) when following the tutorial at http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateDBInstance.html .

I solved it by using the double quote when specifying the connection value while the guideline is using single quote.

define('DB_SERVER', "localhost");
define('DB_USERNAME', "User Name");
define('DB_PASSWORD', "Password");
define('DB_DATABASE', "DATABASE");

I was trying to connect to my DB instance using node-mysql. I found that I the endpoint that RDS provided me with did a DNS lookup. Followed that up and changed the URL to that one. I was only able to connect with mysql via command line until then. When I changed it to the resulting endpoint after the lookup, node-mysql was finally able to connect.

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