鏈接數據庫時忽然遇到一個問題。Mac Navicat鏈接時報錯Can’t connect to MySQL server on ‘xx.xx.xx.xx’ (61)。
PS. win版Navicat 報錯Can’t connect to MySQL server on ‘xx.xx.xx.xx’ (10038)
其中xx.xx.xx.xx是ip地址。
1、查看該用戶是否有遠程登錄的權限
===
mysql> SELECT * FROM mysql.user;
+-----------+-----------+
| User | Host |
+-----------+-----------+
| M | % |
| mysql.sys | localhost |
| root | localhost |
| tommy | % |
| showhilllee | % |
+-----------+-----------+
5 rows in set (0.00 sec)
很顯然,M是允許從其它服務器登陸的。
2、查看 MySQL Server 是不是監聽了 3306 端口
===
執行命令netstat -tulpen
netstat -tulpen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 16801 1507/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 0 17222 1970/master
tcp6 0 0 :::3306 :::* LISTEN 27 46396 22054/mysqld
tcp6 0 0 :::22 :::* LISTEN 0 16803 1507/sshd
tcp6 0 0 ::1:25 :::* LISTEN 0 17223 1970/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 0 49600 22999/dhclient
udp 0 0 0.0.0.0:43504 0.0.0.0:* 0 48850 22999/dhclient
udp6 0 0 :::47875 :::* 0 48851 22999/dhclient
我在這里也沒有問題。
這里如果沒有監聽3306端口或者只監聽了localhost(0.0.0.0表示監聽所有),則在my.cnf添加下面這一行
bind-address = 0.0.0.0
3、如果服務器是 CentOS7,將 MySQL 服務加入防火牆
===
執行命令
sudo firewall-cmd --zone=public --permanent --add-service=mysql
返回success。然后繼續執行
sudo systemctl restart firewalld
我的重啟防火牆之后就能正常訪問了。
