简体   繁体   English

使用PHP连接到Amazon RDS

[英]Connect to Amazon RDS with PHP

I am trying to connect my RDS Instance with my PHP connection file. 我正在尝试将我的RDS实例与我的PHP连接文件连接起来。

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. 我用我的RDS Instance用户替换了localhost和我的端点(rds thing)url,用户名和密码,以及我在创建实例时设置的传递和数据库名称。 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. RDS实例没有附加静态IP地址,您始终使用端点作为主机。 For example: 例如:

database1.jlsdfjhgsdf.us-east-1.rds.amazonaws.com 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. 如果使用除root数据库帐户以外的用户名连接到实例,则需要授予用户权限。

Check security group settings. 检查安全组设置。 If you are connecting from another EC2 instance on Amazon you will need to attach that security group. 如果要从Amazon上的另一个EC2实例进行连接,则需要附加该安全组。 If you are trying to connect from outside AWS, you will need to whitelist the IP address you are accessing from. 如果您尝试从外部AWS连接,则需要将要访问的IP地址列入白名单。

Some ideas: 一些想法:

  • Try using the actual IP of the instance, then it should work. 尝试使用实例的实际IP,然后它应该工作。
  • 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 您可能需要查看Amazon RDS入门以正确设置RDS实例

I was facing a similar issue whilst trying to connect an EC2 Apache server using PHP to the RDS MySQL instance. 在尝试使用PHP将EC2 Apache服务器连接到RDS MySQL实例时,我遇到了类似的问题。 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/ ) 奇怪的是,我可以通过CLI建立连接 - 一旦在mysql运行状态将告诉你你登录了哪个用户,加上端口,服务器名称等。关闭了一些AMI图像有SELinux强制执行 - 这意味着apache服务器无法发送网络请求这位先生指出( 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 确保为RDS DB设置了入站端口
  • In MySQL make sure the host is set to '%' as opposed to localhost 在MySQL中,确保将主机设置为'%'而不是localhost
  • Always use the endpoint string to connect as the RDS IP changes 始终使用端点字符串在RDS IP更改时进行连接

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. 我确保我的安全组(对于RDS和EC2)互相允许。 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. 我能够从终端运行我的脚本并从终端连接到我的数据库,但我无法让脚本从浏览器运行/连接到MySQL。 It turns out I did not have mysql-server installed-- once I installed that and restarted httpd and mysqld it worked like a charm. 事实证明我没有安装mysql-server--一旦我安装了它并重新启动httpd和mysqld就像魅力一样。

This article is what led me to installing mysql-server and the service starts/restarts. 本文是导致我安装mysql-server和服务启动/重启的原因。 Hope it helps! 希望能帮助到你! -- http://www.rndmr.com/amazon-aws-ec2-easy-web-serverset-up-guide-with-a-mac-and-coda-2-256/ - 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 . 当我按照http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateDBInstance上的教程时,我也遇到了ec2(apache + php服务器)和RDS(Mysql服务器)之间的连接问题。 .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. 我试图使用node-mysql连接到我的数据库实例。 I found that I the endpoint that RDS provided me with did a DNS lookup. 我发现我为RDS提供给我的端点进行了DNS查找。 Followed that up and changed the URL to that one. 接下来,将URL更改为该URL。 I was only able to connect with mysql via command line until then. 直到那时我才能通过命令行连接mysql。 When I changed it to the resulting endpoint after the lookup, node-mysql was finally able to connect. 当我在查找后将其更改为结果端点时,node-mysql终于能够连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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