您的位置:首页 > 数据库 > MySQL

MySQL的用户权限设置问题(如何解决Access denied)

2008-10-20 21:00 701 查看
有一次使用MySQL的时候,莫名其妙地出现root用户Access denied,后来查了相关资料,是用户权限的问题,所以顺便就熟悉了一下SQL的grant语法。

环境:

Server: ser1(with MySQL Server)

Client: cli1(with MySQL Client)

问题:

cli1#mysql -h ser1 -u root -p

Enter password:

ERROR 1045: Access denied for user: 'root@ser1' (Using password: NO)

原因:

在cli1上的用户usr没有连接ser1上MySQL的权限,可以通过如下的方式确认:

cli1#mysql -h ser1 -u root -p (默认的root用户没有设置密码)

Enter password: (直接Enter)

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 4 to server version: 4.0.20a-debug

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use mysql; (此DB存放MySQL的各种配置信息)

Database changed

mysql> select host,user from user; (查看用户的权限情况)

+-------------+-------+

| host | user |

+-------------+-------+

| build | |

| build | root |

| localhost | |

| localhost | root |

+-------------+-------+

6 rows in set (0.02 sec)

由此可以看出,ser1上的root用户只能从localhost(ser1)上连接,而对于cli1,只能增加cli1上的root访问权限。

解决方法:

mysql> Grant all privileges on *.* to 'root'@'%' identified by '' with grant option;

Query OK, 0 rows affected (0.02 sec) (%表示是所有的外部机器,如果指定某一台机,就将%改为相应的机器名)

mysql> flush privileges; (运行为句才生效,或者重启MySQL)

Query OK, 0 rows affected (0.03 sec)

mysql>exit

现在再试试:

cli1#mysql -h ser1 -u root -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 9 to server version: 4.0.20a-debug

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

就成功连接上了。

补充:

若删除用户的权限,用如下的语法:

mysql> show grants for 'user_name';

mysql> revoke all privileges on *.* from 'user_name';

mysql> revoke grant option on *.* from 'user_name';

(4.1版本后的可以用drop user语句,更方便。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: