Mac brew安装的MySQL忘记密码

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


### 如何通过 Homebrew 安装特定版本的 MySQL (8.0) 可以通过 Homebrew 的 `homebrew-core` 和第三方仓库来安装指定版本的软件包。以下是关于如何安装 MySQL 版本 8.0 的方法。 #### 方法一:使用官方支持的旧版 Formula 如果目标版本仍在 Homebrew 中被维护,则可以直接调用对应的 Formula 名称: ```bash $ brew search mysql ``` 此命令会列出所有可用的 MySQL 相关 Formula,其中包括可能存在的具体版本(如 `mysql@5.7`, `mysql@8.0`)。如果发现有 `mysql@8.0` 可供安装,则执行以下命令完成安装: ```bash $ brew install mysql@8.0 ``` 需要注意的是,在 macOS 上,默认情况下 Homebrew 不会在 `/usr/local/bin` 或其他常用路径中链接这些旧版本的二进制文件[^1]。因此需要手动创建符号链接以便于全局访问: ```bash $ brew link --force --overwrite mysql@8.0 ``` 或者每次运行时都指明完整路径,例如启动服务时可以这样操作: ```bash $ /usr/local/opt/mysql@8.0/bin/mysqld_safe & ``` #### 方法二:借助 taps 自定义源下载历史版本 当所需的具体版本未直接提供时,可尝试从社区贡献者那里寻找解决方案。比如利用 **Linuxbrew/homebrew-versions** 虽然已经废弃但仍有一些镜像站点保留着类似的资源结构;现在推荐转向个人开发者维护的 tap 库去探索更多可能性。 假设找到合适的 tap 后添加它到本地环境并按照提示更新索引后再进行安装过程类似于常规流程只是多了前缀声明部分而已: ```bash $ brew tap homebrew/versions $ brew install mysql@8.0 ``` 不过要注意这种方法的成功率取决于所选tap的质量以及其是否持续同步最新依赖关系表等等因素影响最终结果可能会有所差异所以建议优先考虑第一种方式即查看当前核心存储库内是否存在满足需求的目标产物再做决定. 最后提醒一点,无论采用哪种途径获得所需的MySQL实例之后都需要依据实际情况调整相应的初始化参数设置诸如数据目录位置变更、端口映射设定之类的细节内容确保能够正常工作无误. --- ### 配置 my.cnf 文件 对于通过 Homebrew 安装MySQL 实例来说,它的主要配置文件通常位于 `/usr/local/etc/my.cnf` 下面[^2]。可以根据实际项目的需求编辑这个文档加入必要的自定义项比如说字符集编码形式啊或者是性能优化方面的改动啦之类的东西哦! 如果你发现自己忘记 root 用户登录密码的话也不要慌张因为这里也给出了相应解决办法那就是先停止正在运转的服务进程接着绕过授权验证机制重新加载新的认证信息从而达到更改初始状态的效果[^3]: ```bash sudo /usr/local/mysql/support-files/mysql.server stop cd /usr/local/mysql/bin sudo su ./mysqld_safe --skip-grant-tables & mysql -u root FLUSH PRIVILEGES; SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password'); EXIT; ``` 另外还有一点值得注意的就是默认情况下只有来自同一台机器内部才能连接上的情况所以我们得修改绑定地址允许外部网络进来才行[^4]: ```bash vi /usr/local/etc/my.cnf # 注释掉 bind-address 行 #bind-address = 127.0.0.1 mysql.server restart ``` 以上便是整个基于 Mac OS X 平台上运用 Homebrew 工具链部署定制化 MySQL 数据库引擎全过程概述希望对你有所帮助! ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值