Mac brew安装的MySQL忘记密码

本文详细介绍了如何在Mac上通过Homebrew管理MySQL服务,包括查找配置文件、临时禁用权限检查、修改root用户密码以及应对validate_password插件的密码策略限制。通过调整密码策略和更新root用户的主机与密码,成功重置了MySQL的root账户。
摘要由CSDN通过智能技术生成

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值