主要思路是去mysql数据库中修改user表的密码加密类型,然后再修改密码。
- 首先找到mysql配置文件my.cnf配置跳过认证,skip-grant-tables;
- 登陆mysql查看root用户的密码加密类型,如果是caching_sha2_password就需要修改一下,然后再修改密码。
mysql配置文件my.cnf配置跳过认证
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
skip-grant-tables # 主要修改这句配置
bind-address = 127.0.0.1
port=3306
mysqlx-bind-address = 127.0.0.1
然后重启mysql服务,然后重新登陆mysql就不需要密码了。
mysql.server restart
下一步查看当前root用户的密码类型,plugin列如果是caching_sha2_password,需要修改加密类型。
# 修改root用户密码加密类型
update user set plugin = 'mysql_native_password' where user = 'root';
➜ etc git:(stable) mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.1.0 Homebrew
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
mysql>
mysql>
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root | *DEBB004B48E4F9A8AFBF31E01A47F19BA77C5DDA | mysql_native_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
4 rows in set (0.00 sec)
mysql>
然后修改密码。重启mysql服务。
# 密码你自己定义
ALTER USER 'root'@'localhost' IDENTIFIED BY '你定义密码';
mysql.server restart