实现MySQL允许远程连接

以下的文章主要介绍的是实现MySQL远程连接的实际操作流程,本文主要是由其相关的实际应用代码来引出MySQL远程连接的具体操作步骤,以下就是文章的主要内容描述,希望会给你带来一些帮助在此方面。

1、进入MySQL,创建一个新用户root,密码为root: 格式:grant 权限 on 数据库名.表名 to 用户@登录主机 identified by "用户密码";

1. grant select,update,insert,delete on *.* to root@192.168.1.12 identified by "root";

原先数据表结构

1. MySQL> use MySQL;

2. Database changed

3. MySQL> select host,user,password from user;

4. +-----------+------+-------------------------------------------+

5. | host | user | password |

6. +-----------+------+-------------------------------------------+

7. | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

8. +-----------+------+-------------------------------------------+

执行上述语句后结果

1. MySQL> use MySQL;

2. Database changed

3. MySQL> select host,user,password from user;

4. +--------------+------+-------------------------------------------+

5. | host | user | password |

6. +--------------+------+-------------------------------------------+

7. | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

8. | 192.168.1.12 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

9. +--------------+------+-------------------------------------------+

10. 2 rows in set (0.00 sec) 可以看到在user表中已有刚才创建的root用户。host字段表示登录的主机,其值可以用IP,也可用主机名, (1)有时想用本地IP登录,那么可以将以上的Host值改为自己的Ip即可。

2、实现MySQL远程连接(授权法) 将host字段的值改为%就表示在任何客户端机器上能以root用户登录到MySQL服务器,建议在开发时设为%。

1. update user set host = ’%’ where user = ’root’; 将权限改为ALL PRIVILEGES 1. MySQL> use MySQL;

2. Database changed

3. MySQL> grant all privileges on *.* to root@'%' identified by "root";

4. Query OK, 0 rows affected (0.00 sec) 5. MySQL> select host,user,password from user;

6. +--------------+------+-------------------------------------------+

7. | host | user | password |

8. +--------------+------+-------------------------------------------+

9. | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

10. | 192.168.1.12 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

11. | % | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

12. +--------------+------+-------------------------------------------+

3 rows in set (0.00 sec) 这样机器就可以以用户名root密码root远程访问该机器上的MySQL.

3、实现MySQL远程连接(改表法)

1. use MySQL;

2. update user set host = '%' where user = 'root'; 这样在远端就可以通过root用户访问MySQL.

posted @ 2010-11-17 20:26  loveyakamoz  阅读(17020)  评论(0编辑  收藏  举报