1、查找brew 安装MySQL配置文件
使用命令查找 MySQL 的配置启动文件列表:
mysqld --help --verbose | less
依次查找,发现文件:/usr/local/etc/my.cnf 存在!
2、修改配置文件
vim /usr/local/etc/my.cnf
[mysqld]下添加
skip-grant-tables
brew services restart mysql
Stopping `mysql`... (might take a while)
==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
3、进入修改
mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.31 Homebrew
Copyright (c) 2000, 2022, 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> 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
### 设置root账户为空密码
mysql> UPDATE user SET authentication_string='' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
### 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit;
3、重新编辑配置文件,将skip-grant-tables去除后重启MySQL
4、设置新密码
mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.31 Homebrew
Copyright (c) 2000, 2022, 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> 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
### 提示报错
### 原因:validate_password密码校验插件,导致要修改的密码不符合密码策略的要求。
mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
### 查看当前的密码策略是:
mysql> show variables like 'validate%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM |
| validate_password.special_char_count | 1 |
+--------------------------------------+--------+
### 字典描述
validate-password=ON/OFF/FORCE/FORCE_PLUS_PERMANENT: 决定是否使用该插件(及强制/永久强制使用)。
validate_password.dictionary_file:插件用于验证密码强度的字典文件路径。
validate_password.length:密码最小长度。
validate_password.mixed_case_count:密码至少要包含的小写字母个数和大写字母个数。
validate_password.number_count:密码至少要包含的数字个数。
validate_password.policy:密码强度检查等级,0/LOW、1/MEDIUM、2/STRONG。
validate_password.special_char_count:密码至少要包含的特殊字符数。
其中,关于validate_password.policy:密码强度检查等级:
0/LOW:只检查长度。
1/MEDIUM:检查长度、数字、大小写、特殊字符。
2/STRONG:检查长度、数字、大小写、特殊字符字典文件。
### 修改密码策略规则
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'validate%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 1 |
+--------------------------------------+-------+
7 rows in set (0.01 sec)
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> ALTER USER 'root'@'%' IDENTIFIED BY 'smile@123';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'%'
mysql> select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
4 rows in set (0.00 sec)
mysql> update user set host = '%' where host = 'localhost' and user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
# Default Homebrew MySQL server config
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+------------------+
4 rows in set (0.01 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED BY 'smile@123';
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye