使用 PHP 连接到 Amazon RDS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9582706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:11:16  来源:igfitidea点击:

Connect to Amazon RDS with PHP

phpmysqlamazon-web-servicesamazon-rds

提问by jQuerybeast

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. But it doesn't seem to work.

我用我的端点(rds 东西)url、用户名和密码替换了 localhost 和我的 RDS 实例用户,并传递了我在创建实例时设置的数据库名称。但它似乎不起作用。

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

还有什么我必须做的我不知道或应该工作的吗?

回答by jaredstenquist

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

RDS 实例没有附加静态 IP 地址,您始终使用主机的端点。例如:

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. If you are trying to connect from outside AWS, you will need to whitelist the IP address you are accessing from.

检查安全组设置。如果您从 Amazon 上的另一个 EC2 实例进行连接,则需要附加该安全组。如果您尝试从 AWS 外部进行连接,则需要将您正在访问的 IP 地址列入白名单。

回答by dom

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 RDSto properly setup your RDS instance
  • 尝试使用实例的实际 IP,然后它应该可以工作。
  • 您是否授权访问您的数据库实例?
  • 您可能需要查看Amazon RDS 入门以正确设置您的 RDS 实例

回答by user1137619

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/)

我在尝试使用 PHP 将 EC2 Apache 服务器连接到 RDS MySQL 实例时遇到了类似的问题。奇怪的是,我可以通过 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
  • 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
  • 确保为您的 RDS 数据库设置了入站端口
  • 在 MySQL 中确保主机设置为 '%' 而不是 localhost
  • 当 RDS IP 更改时,始终使用端点字符串进行连接

回答by Konstantin XFlash Stratigenas

Click here on the Menu (orange).

单击此处的菜单(橙色)。

Just accepts all incoming connections.

只接受所有传入的连接。

回答by Minh Chau

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");

回答by trismi

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.

我最近也遇到了很多麻烦,但能够解决它。我确保我的安全组(用于 RDS 和 EC2)相互允许。我能够从终端运行我的脚本并从终端连接到我的数据库,但我无法从浏览器运行/连接到 MySQL 的脚本。事实证明我没有安装 mysql-server——一旦我安装了它并重新启动了 httpd 和 mysqld,它就像一个魅力。

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/

这篇文章是我安装 mysql-server 和服务启动/重启的原因。希望能帮助到你!-- http://www.rndmr.com/amazon-aws-ec2-easy-web-serverset-up-guide-with-a-mac-and-coda-2-256/

回答by lmerino

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.

我试图使用 node-mysql 连接到我的数据库实例。我发现 RDS 为我提供的端点进行了 DNS 查找。跟进并将URL更改为该URL。在那之前,我只能通过命令行与 mysql 连接。当我在查找后将其更改为结果端点时,node-mysql 终于能够连接了。