今天遠程訪問Ubuntu上的MySQL時出現錯誤: ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.49' (60)
mysql
因此先登陸服務器,用命令netstat -tuln
查看一下:nginx
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN ...
能夠看出,MySQL配置應該沒問題的,固然也確保MySQL用戶密碼等都是對的。sql
查了半天,懷疑是否是iptables問題(但記不得以前曾經配置過iptables,因此一直沒想這塊): sudo iptables -L
shell
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- anywhere anywhere tcp dpt:http ACCEPT tcp -- localhost anywhere tcp dpt:mysql DROP tcp -- anywhere anywhere tcp dpt:mysql ACCEPT tcp -- anywhere anywhere tcp dpt:mysql ACCEPT tcp -- 192.168.1.0/24 anywhere tcp dpt:mysql Chain FORWARD (policy ACCEPT) target prot opt source destination DOCKER all -- anywhere anywhere ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain DOCKER (1 references) target prot opt source destination
很奇怪這邊怎麼會有一個對mysql訪問的DROP規則,但仍是先刪爲敬!
此次再iptables命令多加個參數: sudo iptables -L -n --line-number
服務器
Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 4 ACCEPT tcp -- 127.0.0.1 0.0.0.0/0 tcp dpt:3306 5 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 6 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 7 ACCEPT tcp -- 192.168.1.0/24 0.0.0.0/0 tcp dpt:3306 Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 DOCKER all -- 0.0.0.0/0 0.0.0.0/0 2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
這下每條規則前都有了序號,因此能夠根據序號直接來修改或刪除:tcp
sudo optables -D INPUT 5
把INPUT的第五條規則刪除,而後去客戶端再次登陸MySQL,成功!spa
雖然這個命令沒有效果code
我使用了server
iptables -F
注意!清除規則是恢復到默認規則,若是默認規則爲ACCEPT那就全是ACCEPT,若是默認規則是DROP的話,那就是全DROP了。