Connect to Amazon RDS with PHP

我只是一个虾纸丫 提交于 2019-12-03 16:47:54

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!