##遠程連接mysql數據庫失敗:Can’t connect to MySQL server on ‘root'(10038)的解決辦法
下面是一些解決方案 在我使用的最新LNMP環境包的時候遇到的情況是防火牆iptables 阻止了外網IP訪問3306
我的解決方案是清除iptables規則 解放3306
清除已有iptables規則
iptables -F
iptables -X
(我本人執行到這一步就可以了)
1、設置用戶權限
修改用戶遠程登錄權限:
比如想給用戶root(密碼:123456)設置從任何主機連接到mysql服務器:
連接linux,登錄數據庫:# mysql -uroot -p# Enter password:******MySQL [(none)]> use use mysql;MySQL [(mysql)]> GRANT ALL PRIVILEGES ON *.* TO ’root‘@'%' IDENTIFIED BY ‘123456’ WITH GRANT OPTION;MySQL [(mysql)]> flush privileges;MySQL [(mysql)]> quit;
報這個錯誤的話 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by "123456"' at line 1
用下面這句
create user root@'%' identified by '123456';
grant all privileges on *.* to root@'%' with grant option;
重啟MySQL:# service mysqld restart
2、檢查防火牆有沒有屏蔽掉3306端口。
Centos系統的話,編輯 /etc/sysconfig/iptables:# vim /etc/sysconfig/iptables
加入一行:-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
重啟iptables:# service iptables restart
