在MySQL中我用如下语句创建了一个新的用户
<span style="font-size:18px;">CREATE USER 'KevinYin'@'%' IDENTIFIED BY 'password'
GRANT ALL PRIVILEGES ON *.* TO 'KevinYin'@‘%'</span>
<span style="font-size:18px;">FLUSH PRIVILEGES;</span>
<span style="font-size:18px;">mysql -u KevinYin -p</span>
mySQL报错,信息如下:
<span style="font-size:18px;">ERROR 1045 (28000): Access denied for user 'KevinYin'@'localhost' (using password: YES)</span>
但是,我如果这样登录:
<span style="font-size:18px;">mysql -u KevinYin </span>
即可正常登录
不清楚是什么造成这样的故障的,在网上找了一阵发现了原因:
“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)