在设置root账户密码时,可以选择是否允许其从远程访问。默认情况下是不允许的。
在命令行下,可以有两种大同小异的方法来作此设置:
(1)mysql>GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'something' WITH GRANT OPTION;
mysql>GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'something' WITH GRANT OPTION;
第一句增加了一个root用户授权通过本地机(localhost)访问,密码“something”。
第二句则是使用通配符,授与root用户从任何其它主机发起的访问。
(2)亦可直接使用update语句修改user表:使用root用户或其他用户登录mysql,转到mysql数据库
update user set host='localhost' where user='root';
flush privileges;//只允许root在本机登录
update user set host='%' where user='root';
flush privileges;//允许root远程访问
注意,以上一定要加flush语句。
另外,如果要建新用户,则用grant语句比较方便,记住语句grant总是创建新用户。
例如我目前的库:
mysql> select host,user from user;
+-----------+--------+
| host | user |
+-----------+--------+
| % | mytest |
| % | root |
| localhost | mytest |
| localhost | root |
+-----------+--------+
4 rows in set (0.00 sec)
root和mytest都既能本地连,也能远程连。
在命令行下,可以有两种大同小异的方法来作此设置:
(1)mysql>GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'something' WITH GRANT OPTION;
mysql>GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'something' WITH GRANT OPTION;
第一句增加了一个root用户授权通过本地机(localhost)访问,密码“something”。
第二句则是使用通配符,授与root用户从任何其它主机发起的访问。
(2)亦可直接使用update语句修改user表:使用root用户或其他用户登录mysql,转到mysql数据库
update user set host='localhost' where user='root';
flush privileges;//只允许root在本机登录
update user set host='%' where user='root';
flush privileges;//允许root远程访问
注意,以上一定要加flush语句。
另外,如果要建新用户,则用grant语句比较方便,记住语句grant总是创建新用户。
例如我目前的库:
mysql> select host,user from user;
+-----------+--------+
| host | user |
+-----------+--------+
| % | mytest |
| % | root |
| localhost | mytest |
| localhost | root |
+-----------+--------+
4 rows in set (0.00 sec)
root和mytest都既能本地连,也能远程连。
![](http://file.itpub.net/media/45/90/49_298008_1193380475.jpg)