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

有一次使用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语句,更方便。)

转载于:https://www.cnblogs.com/starspace/archive/2008/10/20/1315417.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值