【问题标题】:failed to connect remote mysql server连接远程mysql服务器失败
【发布时间】:2014-09-29 03:08:20
【问题描述】:

连接远程mysql服务器(CentOS)失败,尝试了很多方法都没有帮助。

我可以ssh成功远程主机,并且端口3306正在监听:

[root@fabulous ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      9979/mysqld

我已经关闭了 iptables:

[root@fabulous ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

远程服务器上的Mysql客户端工作正常,每台主机都允许访问:

mysql> select user,host from user;
+-------+--------------------+
| user  | host               |
+-------+--------------------+
| root  | %                  |
| root  | 127.0.0.1          |
| root  | fabulous.ma2oo.com |
| root  | localhost          |
+-------+--------------------+
4 rows in set (0.00 sec)

mysql的全局配置:

[root@fabulous ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql]
default-character-set=utf8

但是,当我在 Windows 笔记本电脑上使用 Navicat 访问 mysql 服务器时,错误消息是:

2003 - Can't connect to MySql server on 'XX.XX.XX.XX' (10038)

当我使用 telnet 访问该服务器上的相应端口时,错误消息是:

C:\Users\shijunji>telnet 107.170.239.240 3306
Connecting To 107.170.239.240...Could not open connection to the host, on port 3306: Connect failed

有没有人知道或者让我知道如何找到有用的日志? 提前致谢。

【问题讨论】:

  • 你解决了吗?我现在也有同样的问题!

标签: mysql linux ssh centos iptables


【解决方案1】:

我认为你的 Navicat 或你的 windows 操作系统不正常。

在我的本地主机中,

songguo@songuo:~$ telnet 107.170.239.240 3306
Trying 107.170.239.240...
Connected to 107.170.239.240.
Escape character is '^]'.
4
5.1.73
^^=Vxyw1P"Ue}ffL+.G
Connection closed by foreign host.
songguo@songuo:~$ mysql -uroot -h107.170.239.240 -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'211.151.238.51' (using      password: YES)

所以,如果我有正确的密码,我可以连接到mysql服务器。

【讨论】:

  • 看来你是对的,我使用的是我们办公室的桌面,可能官方指南不允许,稍后在家尝试
  • @jjs 你解决了吗?我现在也有同样的问题!
【解决方案2】:

3306 端口似乎被防火墙或其他网络阻止 你和你的 CentOS 服务器之间的设备。

我建议您通过 SSH 连接连接到 MySQL 服务器,以便 您以 localhost 用户身份连接到服务器。使用 SSH 应该允许 您可以从办公室进行连接,同时还提供加密连接和 防止潜在的中间人攻击。我没用过 Navicat 但是 MySQL Workbench 的连接设置使使用 SSH 连接变得容易。

为了安全起见,您还应该在您的服务器上重新启用 iptables 并阻止外部网络访问端口 3306,以便只有 localhost 用户 (包括那些使用 SSH 隧道的)可以连接。

顺便说一句,您还可以启用 SSL 以安全地连接到服务器,但 创建、签名和分发私钥和公钥的物流 服务器和所有客户端的证书使其更难实现。还, 通过 SSL 连接使用相同的 TCP 端口(3306),这无济于事 到该端口的流量被阻止的位置。

【讨论】:

    【解决方案3】:

    检查您的 /etc/mysql/my.cnf 文件,并在 bind-address=127.0.0.1 前添加 #

    【讨论】:

    • 这条路径/etc/mysql/my.cnf在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多