如果MySQL禁止遠程訪問,則連接此MySQL時會報類似如下的錯誤
Can't connect to MySQL Server on "IP" (10060)錯誤
那怎樣可以讓MySQL能被遠程訪問呢?只需要以下兩步操作即可
1.在MySQL中授權讓其它服務器遠程訪問,%表示允許所有的服務器訪問,也可以改為指定服務器的ip
a.可以直接用update操作,將user表中的某記錄記錄中的host改為%或指定的ip即可
b.或者用grant來進行授權,如grant select,insert,update,delete on *.* to root@”%” IdentifIEd by “develop”;
然後執行flush privileges;讓剛才的修改生效。
MySQL> select host,user,passWord from user;
+-----------+------+-------------------------------------------+
| host | user | passWord |
+-----------+------+-------------------------------------------+
| localhost | root | *8D20E4B2CA17D3AAEC1680514E4694C9A19005E5 |
| % | root | *8D20E4B2CA17D3AAEC1680514E4694C9A19005E5 |
| 127.0.0.1 | root | *8D20E4B2CA17D3AAEC1680514E4694C9A19005E5 |
| localhost | | |
| % | | |
+-----------+------+-------------------------------------------+
5 rows in set (0.01 sec)
2.同時要在防火牆裡面要開放MySQL的端口,也就是在防火牆裡面開放3306端口
service iptables stop
vi /etc/sysconfig/iptables,加入一行代碼
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
[root@CentOS_Test_Server ~]# /etc/init.d/iptables save
將當前規則保存到 /etc/sysconfig/iptables: [確定]
[root@CentOS_Test_Server ~]# service iptables start
清除防火牆規則: [確定]
把 chains 設置為 ACCEPT 策略:filter [確定]
正在卸載 Iiptables 模塊: [確定]
應用 iptables 防火牆規則: [確定]
載入額外 iptables 模塊:ip_conntrack_netbiOS_ns ip_conntrac[確定]
在了上述的兩步操作以後,以其它的電腦上就可以遠程連接上述的MySQL了,如果還是報錯,可以試著把MySQL重啟試試看。
連接服務器192.168.1.111上面的MySQL了,下面的操作在另一台服務器192.168.1.100上面執行
E:\www\mysql\bin>MySQL -h 192.168.1.111 -u root -p
Enter passWord: *******
ERROR 2003 (HY000): Can't connect to MySQL Server on '192.168.1.111' (10060)
E:\www\mysql\bin>MySQL -h 192.168.1.111 -u root -p
Enter passWord: *******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 5.1.33-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use MySQL;
Database changed
MySQL> select host,user,passWord from user;
+-----------+------+-------------------------------------------+
| host | user | passWord |
+-----------+------+-------------------------------------------+
| localhost | root | *8D20E4B2CA17D3AAEC1680514E4694C9A19005E5 |
| % | root | *8D20E4B2CA17D3AAEC1680514E4694C9A19005E5 |
| 127.0.0.1 | root | *8D20E4B2CA17D3AAEC1680514E4694C9A19005E5 |
| localhost | | |
| % | | |
+-----------+------+-------------------------------------------+
5 rows in set (0.00 sec)