怎样让MySQL能被远程访问?

如果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)

你可能感兴趣的:(怎样让MySQL能被远程访问?)