这两天遇到了一个令人头大的一个问题,在安装MySQL5.7后,出现了MySQL默认的root用户无法在虚拟机本机上登录数据库:
[root@localhost bin]# mysql -uroot -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
提示拒绝了localhost上的root用户的使用密码登录。
令人不得其解,刚刚安装的MySQL怎么就登录不上了呢,密码是正确的,在物理机上通过navicat连接数据库也可以正常连接:
证明密码和用户名是正确的,怎么会连接不上了呢?
通过百度后,尝试使用不使用密码登录的方式,修改root用户的密码后再进行登录,依然存在该问题,证明不是密码的问题;
在看到 MySQL普通用户无法本地登录的解决方法及MySQL的用户认证算法 及 MySQL密码正确却无法本地登录怎么办 的文章后,发现可能是用户名及其对应允许的host出现的问题:
查询mysql.user表:
select Host,user,authentication_string from mysql.`user`;
可以看到root用户的允许host被我在安装时修改为‘192.168.137.%’,这直接导致mysql在我使用root用户在localhost上登录时禁止登录,解决的方式也很简单,直接修改root用户的Host字段为‘%’就可以解决这个问题。
UPDATE mysql.user set Host = '%' WHERE user = 'root';
然后再次登录:
[root@localhost bin]# mysql -h127.0.0.1 -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.29 MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
就可以使用啦~