【教程】Homebrew安装MySQL 5.7(Apple Silicon适用)


?MySQL 5.7 macOS支持情况

在 MySQL 官方 Archive 中

可以发现 MySQL 5.7(5.7.32~5.7.36)都没提供macOS的安装

而 MySQL 5.7.31

提供了安装 但是没有支持ARM的

(Apple Silicon采用ARM架构 虽然可以通过Rosetta运行x86架构Application)


MySQL 8提供 x86架构 与 ARM架构的macOS安装

如果需要安装MySQL 8 点击前往下载


?Hombrew MySQL

推荐阅读:


Homebrew MySQL => mysql — Homebrew Formulae

HomeBrew 的 MySQL 5.7 支持 Apple Silicon


?Hombrew 安装 MySQL 5.7

安装

brew install mysql@5.7

注意查看brew的提示信息

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

mysql@5.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have mysql@5.7 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"


To restart mysql@5.7 after an upgrade:
  brew services restart mysql@5.7
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/mysql@5.7/bin/mysqld_safe --datadir=/usr/local/var/mysql
==> Summary
??  /usr/local/Cellar/mysql@5.7/5.7.37: 320 files, 235.1MB
==> Running `brew cleanup mysql@5.7`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

brew安装的MySQL默认情况下没有root密码

我们可以先开启服务测试连接

# 列出当前可用服务
brew services list
# 开启服务
brew services start mysql@5.7 
# 终止服务
brew services stop mysql@5.7 
# 终止所有服务
brew services stop --all

此时通过mysql -uroot是无法连接的(如果brew安装的是MySQL 8不会遇到这个问题)

通过 Navicat 使用 root用户 密码为空 可以连接上


设置root用户密码

brew安装完MySQL后 有如下提示信息

We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation


此时修改密码有两种方式

  • root用户连接MySQL修改密码
  • mysql_secure_installation

方式一

brew链接MySQL

brew link --force mysql@5.7

brew链接后可以通过如下命令开启/关闭MySQL

mysql.server start
mysql.server stop

开启服务后 设置root用户密码

mysql -uroot
set password for 'root'@'localhost' = password('root');
# set password for 'root'@'localhost' = password('需要设置的密码');

方式二

使用mysql_secure_installation修改设置root密码一定要先让brew链接MySQL

(如果安装的是MySQL 8则无需手动链接)

brew link --force mysql@5.7
mysql_secure_installation

后续请提示自行设置

?  ~ mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 

此方式会开启MySQL密码验证插件

由上述提示可知最低密码策略的密码长度也需要8位

不建议通过SET GLOBAL validate_password_length=4;修改密码验证插件相关参数


如需卸载MySQL密码验证插件可参考

How do I turn off the mysql password validation? - Stack Overflow


?参考及引用

https://stackoverflow.com/questions/66618600/how-to-install-and-start-mysql-5-7-on-macos-bigsur-apple-silicon-with-homebrew


?转载请注明出处

本文作者:双份浓缩馥芮白

原文链接:

版权所有,如需转载请注明出处。