php 无法通过 IP 地址连接到 MySQL 服务器 (10061)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3518845/
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
Can't Connect to MySQL Server on IPAddress (10061)
提问by SidC
I need to connect to a remote MySQL database and have created the following connection code:
我需要连接到远程 MySQL 数据库并创建了以下连接代码:
<?php
/* Set Variables */
$host="myipaddress";
$db="mydbname";
$username="dbuser";
$pass="mypass";
/* Attempt to connect */
$mysqli=new mysqli($host,$username,$pass,$db);
if (mysqli_connect_error()){
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
echo 'Success... ' . $mysqli->host_info . "\n";
$mysqli->close();
}
?>
For security reasons, I've not provided the actual variable values. When I run this on my development system, I receive
出于安全原因,我没有提供实际的变量值。当我在我的开发系统上运行它时,我收到
Connect Error (2003) Can't connect to MySQL server on 'myipaddress' (10061)
My PHP is a bit rusty, can someone identify where my code is faulty? Note that dbuser has select, insert and update privileges on the database name set as the variable.
我的 PHP 有点生疏,有人能找出我的代码有问题的地方吗?请注意,dbuser 对设置为变量的数据库名称具有选择、插入和更新权限。
Thanks, Sid
谢谢,希德
EditI made changes to my.cnf and restarted mysql. I now receive access denied for user 'dbuser'@'mycurrenthostname' (using password YES). When I use mysql -u dbuser -p from command line, I can login. I granted insert, update and select to dbuser with host '%' so that dbuser could connect from anywhere.
编辑我对 my.cnf 进行了更改并重新启动了 mysql。我现在收到用户 'dbuser'@'mycurrenthostname' 的访问被拒绝(使用密码是)。当我从命令行使用 mysql -u dbuser -p 时,我可以登录。我向 dbuser 授予了插入、更新和选择权限,主机为 '%' 以便 dbuser 可以从任何地方连接。
I've read the MySQL Reference guide about this error, but am still stuck. Is there a problem with my code, now that my.cnf has been fixed?
我已经阅读了有关此错误的 MySQL 参考指南,但仍然卡住了。我的代码是否有问题,现在 my.cnf 已修复?
回答by Mike
Check that your firewall is allowing connections through on port 3306.
检查您的防火墙是否允许通过端口 3306 进行连接。
Check the MySQL configuration parameter bind-address
in my.cnf
to ensure that it is allowing remote connections.
检查MySQL的配置参数bind-address
中my.cnf
,以确保它允许远程连接。
There's information and troubleshooting tips here:
此处提供信息和故障排除提示:
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html
回答by amal
According to the MySql documentation , The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the network port you specified is the one configured on the server.
根据 MySql 文档,错误 (2003) Can't connect to MySQL server on 'server' (10061) 表示网络连接已被拒绝。您应该检查是否有 MySQL 服务器正在运行,是否启用了网络连接,以及您指定的网络端口是否是服务器上配置的端口。
I'm not familiar with php , but the problem might not be in your code.
我不熟悉 php ,但问题可能不在您的代码中。
回答by shashikant kuswaha
If your getting the above error even after putting the Mysql port 3306 open access,
如果您在将 Mysql 端口 3306 开放访问后仍收到上述错误,
Just change bind address of mysql file my.cnf which is located at /etc/mysql/my.cnf
Update the bind address as given below
bind-address = 0.0.0.0
只需更改位于/etc/mysql/my.cnf
更新绑定地址的 mysql 文件 my.cnf的绑定地址,如下所示
bind-address = 0.0.0.0
This will allow access to all ips remotely On AWS ec2 it is working.
这将允许远程访问所有 ips 在 AWS ec2 上它正在工作。