brew install mysql 无法设置 root 密码



我通过brew安装了MySQL,但我没有让mysql_secure_installation之后设置根密码,而是得到提示输入根密码。

% brew install mysql
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/manifests/8.0.27
Already downloaded: /Users/msd/Library/Caches/Homebrew/downloads/c521714052ea597d404fc2862523de823536b9d9c0f4be0bcefc34b8e280a2cf--mysql-8.0.27.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/blobs/sha256:dfd1d6855666
Already downloaded: /Users/msd/Library/Caches/Homebrew/downloads/9a51cb7727b7e2d85fd2a1e02469666dff8e9d816b4d96ac68568577f741dea2--mysql--8.0.27.arm64_monterey.bottle.tar.gz
==> Pouring mysql--8.0.27.arm64_monterey.bottle.tar.gz
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
mysql -uroot
To restart mysql after an upgrade:
brew services restart mysql
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
==> Summary
🍺  /opt/homebrew/Cellar/mysql/8.0.27: 304 files, 294MB
==> Running `brew cleanup mysql`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
% mysql_secure_installation
Enter password for user root: 
Error: Access denied for user 'root'@'localhost' (using password: YES)

然后我尝试% mysqld --skip-grant-tables重置密码,但出现另一个错误:

2021-12-05T18:28:46.319410Z 0 [System] [MY-010116] [Server] /opt/homebrew/Cellar/mysql/8.0.27/bin/mysqld (mysqld 8.0.27) starting as process 3482
2021-12-05T18:28:46.320508Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /opt/homebrew/var/mysql/ is case insensitive
2021-12-05T18:28:46.323138Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-12-05T18:28:46.328311Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 35
2021-12-05T18:28:47.333722Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 35
2021-12-05T18:28:48.339275Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 ```

我已经使用这些步骤卸载了MySQL,在重新安装之前,我执行了brew remove mysql,这导致了Error: No such keg: /opt/homebrew/Cellar/mysql。所以我很确定MySQL已经不再安装了。

那么,有没有办法重置root密码(或者如果没有,重新安装MySQL(?

我也遇到了同样的问题。升级过程中的某些操作损坏了ibdata1文件,因此无法启动。我想出的最好的解决方案是卸载MySQL

brew uninstall mysql

然后删除数据目录:

rm -rf /usr/local/var/mysql

和自定义conf

rm /usr/local/etc/my.cnf

然后重新安装MySQL

brew install mysql

最新更新