连接到在Ubuntu AWS实例中创建的MySQL数据库

时间:2022-06-25 02:46:41

Ok so I created an ubuntu instance from AWS and downloaded LAMP on it as you would in a regular Ubuntu OS. When I try to connect to the db from my php script it's rejecting the connection. saying "Connection failed: Connection refused". So I guess my question is this: Does amazon not allow you to connect to a db without using their RDS database service or am I putting something wrong here? (I've hidden some of the data for security purposes, the ... are numbers of my instance). When I put "ec2-34-...-..-64.compute-1.amazonaws.com" into the browser the apache message comes up so I don't see why this is not working as a server name?

好的,我从AWS创建了一个ubuntu实例,并像在普通的Ubuntu操作系统中一样下载了LAMP。当我尝试从我的PHP脚本连接到数据库时,它拒绝连接。说“连接失败:连接被拒绝”。所以我想我的问题是:亚马逊不允许你在没有使用他们的RDS数据库服务的情况下连接到数据库,或者我在这里做错了吗? (为了安全起见,我隐藏了一些数据,......是我实例的数字)。当我将“ec2-34 -... -..- 64.compute-1.amazonaws.com”放入浏览器时,会出现apache消息,所以我不明白为什么这不能用作服务器名称?

$servername = "ec2-34-...-..-64.compute-1.amazonaws.com";

$username = "root";

$password = “hidden"; 

$dbname = "questions87";


// Create connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn) {

  die("Connection failed: " . mysqli_connect_error());

}

1 个解决方案

#1


1  

You need to change your EC2 Security Group Inbound Rules related with 3306, you can find that on the AWS FAQ. Then, you need to be sure that your user@YOURIP have all permissions on your MySQL database. For your Inbound Rules it is recommended to use a Custom Rule to your IP, not All Traffic.

您需要更改与3306相关的EC2安全组入站规则,您可以在AWS FAQ上找到该规则。然后,您需要确保您的用户@ YOURIP拥有MySQL数据库的所有权限。对于您的入站规则,建议您对IP使用自定义规则,而不是所有流量。

Reference and further reading:

参考和进一步阅读:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

#1


1  

You need to change your EC2 Security Group Inbound Rules related with 3306, you can find that on the AWS FAQ. Then, you need to be sure that your user@YOURIP have all permissions on your MySQL database. For your Inbound Rules it is recommended to use a Custom Rule to your IP, not All Traffic.

您需要更改与3306相关的EC2安全组入站规则,您可以在AWS FAQ上找到该规则。然后,您需要确保您的用户@ YOURIP拥有MySQL数据库的所有权限。对于您的入站规则,建议您对IP使用自定义规则,而不是所有流量。

Reference and further reading:

参考和进一步阅读:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html