mysql - 错误 2003 (HY000) : Can't connect to MySQL s

这个问题与以下问题有关:

  • Can't connect to MySQL server error 111
  • Trying to connect to remote MySQL host (error 2003)

我正在本地机器上配置一个新的 MySQL (5.1) 服务器。我需要提供对数据库的远程访问。我做了以下步骤:

  1. 在my.cnf中注释bind-address:

    # bind-address      = 192.168.1.3
    
  2. 授予权限:

    GRANT ALL PRIVILEGES ON *.* TO 'nickruiz'@'%' IDENTIFIED BY PASSWORD 'xxxx';
    
  3. 在路由器上设置端口转发(TCP 和 UDP,端口 3306、192.168.1.3)
  4. 为防火墙配置 iptables

    sudo iptables -I INPUT -p udp --dport 3306 -j ACCEPT
    
    sudo iptables -I INPUT -p tcp --dport 3306 --syn -j ACCEPT
    
    sudo iptables-save
    
  5. 重启mysql服务器sudo/etc/init.d/mysql restart

在测试时,我得到以下信息:

局域网:

mysql -h 192.168.1.3 -u nickruiz -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 95
Server version: 5.1.63-0ubuntu0.11.04.1 (Ubuntu)

远程:

mysql -h 1xx.xx.4.136 -u nickruiz -p
ERROR 2003 (HY000): Can't connect to MySQL server on '1xx.xx.4.136' (111)

显然有什么问题让我无法使用我的全局 IP 地址。

注意事项:

  • 我试过在同一台机器上测试远程连接 通过远程机器上的 SSH。
  • 我不确定我的 ISP 是否给了我一个静态 IP。

有什么想法吗?

更新: telnet 似乎无法正常工作。

telnet 192.168.1.3 3306
Trying 192.168.1.3...
Connected to 192.168.1.3.
Escape character is '^]'.
E
5.1.63-0ubuntu0.11.04.1,0g8!:@pX;]DyY0#\)SIConnection closed by foreign host.

最佳答案

请检查您的监听端口:

netstat -nat |grep :3306

如果显示

 tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN 

远程连接没问题。

但在这种情况下,我认为你有

tcp        0     192.168.1.3:3306            0.0.0.0:*               LISTEN 

您的远程连接没问题。 您还应该检查您的防火墙(如果您使用 centos/redhat,则为 iptables)

services iptables stop

用于测试或使用:

iptables -A input -p tcp -i eth0 --dport 3306 -m state NEW,ESTABLISHED -j ACCEPT
iptables -A output -p tcp -i eth0 --sport 3306 -m state NEW,ESTABLISHED -j ACCEPT

还有一件事是检查您对远程连接的授予权限:

GRANT ALL ON *.* TO remoteUser@'remoteIpadress' IDENTIFIED BY 'my_password';

关于mysql - 错误 2003 (HY000) : Can't connect to MySQL server (111),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11758339/

相关文章:

linux - 如何将 bash 别名定义为多个命令的序列?

linux - 查找在日期范围之间创建的文件

linux - 使用 ssh 检查远程主机上是否存在文件

linux - 从标准输出中获取输出的最后 4 个字符

c++ - 通过两次链接同一个库来解决循环依赖?

linux - 将时间跨度(以秒为单位)转换为 shell 中的格式化时间

linux - 分别对每个文件进行 GZip

linux - 无法在 Ubuntu 16.04 中启动 Docker 服务

linux - 将标准输出作为命令行工具的文件名传递?

java - 无法创建Java虚拟机