這兩天遇到了一個令人頭大的一個問題,在安裝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>
就可以使用啦~