简体   繁体   English

如何从EC2实例对RDS实例进行MySQL查询?

[英]How do I do MySQL queries on an RDS instance from an EC2 instance?

I've been trying to set up a system where my database will be in an AWS RDS instance and my programs in a EC2 instance (seemingly very straightforward) 我一直在尝试建立一个系统,其中我的数据库位于AWS RDS实例中,而我的程序位于EC2实例中(看似非常简单)

For some reason following the results here have been pretty unhelpful. 由于某种原因,遵循此处的结果一直没有帮助。

To clarify, what I'm looking for is some sample code to connect to a db and run mysql queries. 为了澄清,我正在寻找的是一些示例代码,用于连接到数据库并运行mysql查询。 So far my every attempt ends with my program trying to connect with my localhost mysql on my ec2 instance despite specifying the rds url. 到目前为止,尽管指定了rds URL,但我的每次尝试都以我的程序尝试与我的ec2实例上的localhost mysql连接而结束。

Here's my script, 这是我的剧本,

<?php
$servername = "maindb.gibberish.us-east-1.rds.amazonaws.com";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (mysqli_error($conn)) {
    die("Connection failed: " . mysqli_error($conn));
}else{
        echo "connected successfully";    
}
?>

You can use the code in my answer to this question which also explains what each line of code does. 您可以在我对这个问题的回答中使用代码,该代码还解释了每一行代码的作用。 You would type your amazon RDS endpoint in the $servername variable. 您可以在$servername变量中键入Amazon RDS 端点 and also change your query in the $sql variable. 并在$sql变量中更改查询。

In terms of ensuring that your RDS can be contacted by your EC2 Server. 在确保EC2服务器可以联系RDS方面。 you need to ensure that the security group is correctly set to allow connections from your server. 您需要确保安全组已正确设置为允许来自服务器的连接。 like so: 像这样:

1. In your EC2 Management Console, select 'Security Groups' from the left side navigation. 1.在您的EC2管理控制台中,从左侧导航中选择“安全组”。 图。1

2. Select the security group for your RDS server . 2.选择RDS服务器的安全组 nb If you didn't name this something specific when you set it up then the name is likely to look like rds-launch-wizard created xxxxxxx . nb如果您在设置时没有命名此特定名称,则该名称很可能类似于rds-launch-wizard created xxxxxxx 图2

Click the edit button. 单击编辑按钮。 It is likely that the IP address which is in there, is the one which you are working from. 其中的IP地址很可能就是您正在使用的IP地址。 (Or the one which you were on when you setup the server). (或您在设置服务器时使用的服务器)。 You need to change this to either ANY, or just your EC2 server's public IP address. 您需要将此更改为ANY,或者仅更改为EC2服务器的公共IP地址。

EDIT: after looking at your code I think you are testing the connection incorrectly. 编辑:查看您的代码后,我认为您正在错误地测试连接。

<?php
$servername = "maindb.gibberish.us-east-1.rds.amazonaws.com";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {

    die("Connection failed: " . mysqli_error($conn));

}else{
        echo "connected successfully";

}


?>

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

相关问题 如何在AWS Amazon Linux EC2实例上从PHP 7连接到SQL Server实例? - How to do I connect to a SQL Server instance from PHP 7 on an AWS Amazon Linux EC2 instance? 如果第一个 ec2 实例关闭,如何从一个 ec2 实例重定向到另一个 ec2 实例? - How to redirect from one ec2 instance to another ec2 instance if first ec2 instance is down? 使用PHP从EC2通过VPC连接到RDS实例 - Connecting to RDS instance through VPC from EC2 using PHP 如何在Amazon EC2 Linux AMI实例上安装Zend Optimizer? - How do I install Zend Optimizer on an Amazon EC2 Linux AMI instance? 我的EC2服务器实例在PHPU中无限循环运行。 如何停止? - My EC2 server instance is running on a infinite loop in PHPU. How do I stop it? 无法通过AWS EC2实例使用PHP连接到AWS MYSQL RDS实例 - Unable to connect to AWS MYSQL RDS instance using PHP via AWS EC2 Instance 使用 PHP 通过 SSL 将 AWS EC2 实例连接到 RDS MySQL - Connect AWS EC2 instance to RDS MySQL through SSL using PHP 如何从ec2实例部署PHP应用程序? - How to deploy a PHP application from ec2 instance? 如何从s3中的.zip文件部署到EC2实例 - How to deploy to EC2 instance , from a .zip file in s3 如何创建亚马逊MySQL RDS实例的镜像并连接到它? - How can I create Mirror of amazon MySQL RDS instance and connect to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM