若显示类似 Homebrew 4.6.10,则说明 Homebrew 已安装。若提示 command not found: brew,需先安装 Homebrew。
搜索可用版本
在安装 MySQL 之前,建议先搜索可用的版本。通过执行以下命令可以查看 Homebrew 仓库提供的 MySQL 相关软件包:
brew search mysql
这个命令会列出所有可用的 MySQL 版本以及相关工具。需要注意的是,Homebrew 通常会提供多个 MySQL 版本,包括最新的稳定版和长期支持版本,用户可以根据自己的需求选择合适的版本进行安装。
检查是否已安装 MySQL
执行命令排查是否有旧版 MySQL:
brew list | grep mysql
若终端无输出,说明未安装 MySQL,可继续下一步。若输出 mysql 或 mysql@xx,说明已安装,需先卸载(执行 brew uninstall mysql),避免冲突。
第二章:安装 MySQL
Homebrew 会自动下载 MySQL 及依赖(如 abseil、icu4c 等),无需手动处理。
# 清理旧缓存
brew cleanup
# 更新 Homebrew 到最新版本,确保能够安装最新的软件包
brew update
# 安装 MySQL
brew install mysql
这个命令会触发 Homebrew 的安装流程,包括下载软件包、解析依赖关系、编译安装等步骤。安装过程可能需要一些时间,具体取决于网络速度和系统性能。在安装完成后,Homebrew 会输出一些重要的提示信息,包括 MySQL 的初始配置和安全建议。
第三章:基本管理
启动 MySQL 服务
MySQL 安装后不会自动启动,需手动启动服务(支持后台自启)。使用 Homebrew 管理服务非常方便,可以通过以下命令启动 MySQL:
brew services start mysql
检查服务状态
确认 MySQL 服务是否正常运行,可以通过以下两种方式验证:
# 查看所有 brew 服务状态(推荐)
brew services list
# 查找 MySQL 进程
ps aux | grep mysql
终端显示 mysql 一行的 status 为 started,这说明 MySQL 服务已成功启动。若 status 为 error,可尝试重启服务。Homebrew 安装的 MySQL 默认情况下没有 root 密码,不填写密码的情况下直接可以通过 Navicat 连接(需要先开启 MySQL 服务)。
验证 MySQL 版本
执行命令查看安装的版本:
mysql --version
首次登录 MySQL
Homebrew 安装的 MySQL 默认无 root 密码,执行一下命令就可以直接登录:
mysql -u root
如果一切正常,将看到 MySQL 的欢迎信息和命令提示符,表明已成功连接到 MySQL 服务器。
MySQL 初始化
MySQL 首次安装后无密码,这显然存在安全风险。为了增强安全性,就需通过官方工具 mysql_secure_installation 配置安全选项,在终端(非 MySQL 命令行)执行:
mysql_secure_installation
这个交互式脚本会引导我们完成一系列安全配置,包括:
刷新权限:重新加载权限表将确保到目前为止所做的所有更改立即生效。
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success. All done!
By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database... Success.
- Removing privileges on test database... Success.
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
删除匿名用户:默认情况下,MySQL 安装有一个匿名用户,这使得任何人都可以登录 MySQL 而无需创建用户帐户。这仅用于测试目的,并且可以使安装过程更加顺畅。在进入生产环境之前,应该删除这些匿名用户。
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
设置 root 账号密码:
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: 1
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security.
Would you like to setup VALIDATE PASSWORD component?
第四章:基本配置
MySQL 用户管理
MySQL 8.0 在用户管理方面做出了重大改进,采用了新的认证插件并修改了授权机制,这显著提高了数据库的安全性,但也使用的用户管理流程与早期版本有所不同。在 MySQL 8.0 中,创建用户和授权操作必须要分开执行。创建新用户的命令语法如下所示: