在MySQL中我用如下语句创建了一个新的用户
CREATE USER 'KevinYin'@'%' IDENTIFIED BY 'password'
GRANT ALL PRIVILEGES ON *.* TO 'KevinYin'@‘%'FLUSH PRIVILEGES;
紧接着我用新创建的用户进行登录:
mysql -u KevinYin -pmySQL报错,信息如下:
ERROR 1045 (28000): Access denied for user 'KevinYin'@'localhost' (using password: YES)但是,我如果这样登录:
mysql -u KevinYin 即可正常登录
不清楚是什么造成这样的故障的,在网上找了一阵发现了原因:
“When multiple matches are possible, the server must determine which of them to use. It resolves this issue as follows: (...)
When a client attempts to connect, the server looks through the rows [of table mysql.user] in sorted order.
The server uses the first row that matches the client host name and user name.
(...) The server uses sorting rules that order rows with the most-specific Host values first. Literal host names [such as 'localhost'] and IP addresses are the most specific.”这是由系统中的匿名用户及MySQL的认证机制造成的。解决办法就是删除系统中的匿名用户:
DROP USER ''@'localhost'即可解决。
详情参考:MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost'
(using password: YES)