mysql grant identified导致的Access denied解决办法

421 阅读1分钟

本机连接开发环境mysql服务报错

 woo:~ woo$ mysql -h172.17.8.26 -P3001 -uums -p umsEnter password:ERROR 1045 (28000): Access denied for user 'ums'@'10.224.36.162' (using password: YES)

比对过账号密码均正确,同事主机用相同指令可以登陆。

登陆到服务器root登陆mysql检查数据权限情况

show databases;
use mysql;
select a.user,a.host,b.user,b.host,b.db from user a left join db b on a.user=b.user and a.host=b.host order by a.user;

| ums | % | ums | % | ums |
| ums | localhost | ums | localhost | ums |
| ums | 127.0.0.1 | ums | 127.0.0.1 | ums |
| ums | 10.224.36.153 | ums | 10.224.36.153 | ums |
| ums | 10.224.36.234 | ums | 10.224.36.234 | ums |
| ums | 10.224.36.140 | ums | 10.224.36.140 | ums |

同事的主机有单独授权的记录,同时也有所有来源授权的记录%。

看起来都是正常,但是为什么不能登陆呢。通过grant指定ip后可以登陆。

grant all privileges on ums.* to ums@10.224.36.162 identified by '****';

这种方式虽然可以解决,但是换一台设备用其它ip就又不行了,太难受了。

根本解决 root登陆mysql,切换到mysql库,删除&重建记录

use mysql;
delete from user where User='ums';
delete from db where user='ums' and db='ums';
flush privileges;

drop user 'ums'@'%';
drop user 'ums'@'localhost';
drop user 'ums'@'127.0.0.1';

CREATE USER 'ums'@'%' IDENTIFIED BY '******';;
CREATE USER 'ums'@'localhost' IDENTIFIED BY '******';
CREATE USER 'ums'@'127.0.0.1' IDENTIFIED BY '******';;

grant all privileges on ums.* to ums@'%'; 
grant all privileges on ums.* to ums@'localhost'; 
grant all privileges on ums.* to ums@'127.0.0.1'; 
flush privileges;

注意要点是先创建用户,然后授权,分两步执行

不要用grant identified 这种

grant all privileges on ums.* to ums@% identified by '****';

这样任何机器都能访问了。

woo:~ woo$ mysql -h172.17.8.26 -P3001 -uums  -pEnter password: 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

PS: 真实原因有待商榷,可能是默认mysql初始化脚本创建了空用户名导致。下一次遇到了再排查具体原因再更新本文。如果你正好遇到这个问题,也正在排查,不妨查一查user表,看是否有空用户名的记录(清理掉),如果有解决欢迎在评论里回复。thks