# 19.4 MySQL 8.x 系列

## 安装 MySQL 8.x

MySQL 8.x 是 MySQL 数据库的主流版本系列，作为 Oracle 公司主导开发的开源关系型数据库管理系统（Relational Database Management System, RDBMS），MySQL 在产业界应用广泛。

MySQL 8.0/8.4 都是长期支持版本，具有稳定的性能和广泛的兼容性。

本节将系统介绍如何在 FreeBSD 操作系统环境下部署 MySQL 8.x。

### MySQL 8.0 LTS

使用 pkg 安装：

```sh
# pkg install mysql80-server
```

或使用 ports 编译安装：

```sh
# cd /usr/ports/databases/mysql80-server/ 
# make install clean
```

### MySQL 8.4 LTS

使用 pkg 安装：

```sh
# pkg install mysql84-server
```

或使用 ports 编译安装：

```sh
# cd /usr/ports/databases/mysql84-server/ 
# make install clean
```

查看安装后的说明（MySQL 8.0 类似）：

```sh
# pkg info -D mysql84-server
mysql84-server-8.4.3_1:
On install:
There is no initial password for first time use of MySQL. # 即无密码
首次使用 MySQL 时没有初始密码，即默认无密码
Keep in mind to reset it to a secure password.
请记得将其重置为安全密码

MySQL 8.4 has a default /usr/local/etc/mysql/my.cnf,
remember to replace it with your own
or set `mysql_optfile="$YOUR_CNF_FILE"` in rc.conf.
MySQL 8.4 有一个默认的 /usr/local/etc/mysql/my.cnf，
记得用你自己的文件替换它，
或者在 rc.conf 中设置 `mysql_optfile="$YOUR_CNF_FILE"`。

On upgrade:
As of MySQL 8.0.16, the MySQL server performs the upgrade tasks previously
handled by mysql_upgrade. Consequently, mysql_upgrade is unneeded and is
deprecated as of that version, and will be removed in a future MySQL version.
从 MySQL 8.0.16 开始，MySQL 服务器执行以前由 mysql_upgrade 处理的升级任务。
因此，mysql_upgrade 不再需要，并且从该版本开始被弃用，
将在未来的 MySQL 版本中移除。

Because mysql_upgrade no longer performs upgrade tasks,
it exits with status 0 unconditionally.
因为 mysql_upgrade 不再执行升级任务，
它无条件地以状态 0 退出。
```

## 启动服务

安装完成后，需要启动 MySQL 服务才能正常使用。以下命令用于设置服务开机自启并立即启动服务：

```sh
# service mysql-server enable   # 设置 MySQL 服务开机自启
# service mysql-server start    # 启动 MySQL 服务
```

## 登录

服务启动后，可以使用命令行客户端登录 MySQL。根据安装说明，MySQL 8.0 和 8.4 的默认密码为空，登录时直接按回车即可。

使用 root 用户登录 MySQL，提示输入密码：

```sql
# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.27 Source distribution

Copyright (c) 2000, 2021, 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.
```

## 修改密码

为了数据库安全，首次登录后建议立即修改 root 用户密码。以下命令将数据库的 root 用户密码设置为 `SecurePass123!`，然后刷新权限，最后退出 MySQL。

```sh
root@localhost [(none)]> alter user 'root'@'localhost' identified by 'SecurePass123!';  -- 修改 root 用户在 localhost 的密码
Query OK, 0 rows affected (0.02 sec)

root@localhost [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

root@localhost [(none)]> quit;  -- 退出 MySQL 命令行客户端
Bye
```

重新登录：

```sql
# mysql -uroot -p  # 使用 root 用户登录 MySQL，并按提示输入密码
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 9
Server version: 8.0.27 Source distribution

Copyright (c) 2000, 2021, 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.

root@localhost [(none)]> show databases;  -- 显示当前 MySQL 实例中的所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
```

## MySQL 配置文件

```sh
/
└── usr/
    ├── local/
    │   └── etc/
    │       └── mysql/
    │           ├── my.cnf        # MySQL 配置文件
    │           └── my.cnf.sample # MySQL 配置文件示例
    └── ports/
        └── databases/
            ├── mysql80-server/  # MySQL 8.0 Server 源码端口
            └── mysql84-server/  # MySQL 8.4 Server 源码端口
```

如需进一步配置 MySQL，可以修改其配置文件。配置文件可以调整数据库的各种参数，如内存使用、连接数等。

MySQL 默认的配置文件模板位于 `/usr/local/etc/mysql/my.cnf.sample`，实际的默认配置文件理论上位于 `/usr/local/etc/mysql/my.cnf`（默认内容为空）。

复制 MySQL 示例配置文件为正式配置文件：

```sh
# cp /usr/local/etc/mysql/my.cnf.sample /usr/local/etc/mysql/my.cnf
```

然后根据需要修改 `/usr/local/etc/mysql/my.cnf` 即可。

如需使用自定义的配置文件路径，在 `/etc/rc.conf` 文件中，写入：

```ini
mysql_optfile="/abc/xyz.cnf"
```

这样即可指定 MySQL 使用的配置文件路径。

## 课后习题

1. 分别安装 MySQL 8.0 和 8.4 两个 LTS 版本，创建相同的测试数据库并执行相同的 SQL 查询，对比两个版本在性能与默认配置上的差异。
2. 查找 FreeBSD MySQL rc 脚本源码，分析其配置文件加载机制，修改默认配置文件路径，实现从 /usr/local/etc/mysql84.conf 加载配置。
3. 修改 MySQL 8.x 的默认认证方式从 caching\_sha2\_password 改为 mysql\_native\_password，验证其兼容性变化。
