# 19.3 MySQL 5.x 系列

> **警告**
>
> **MySQL 5.x 系列已停止官方维护，使用该版本软件需自行承担安全与兼容性风险。**

目前 FreeBSD 官方软件源中已不再提供 MySQL 5.5/5.6/5.7 等版本的预编译包，相关软件包的历史状态可参见：mysql55-server\[EB/OL]. \[2026-03-26]. <https://www.freshports.org/databases/mysql55-server>、mysql56-server\[EB/OL]. \[2026-03-26]. <https://www.freshports.org/databases/mysql56-server> 和 mysql57-server\[EB/OL]. \[2026-03-26]. <https://www.freshports.org/databases/mysql57-server>。

## 项目结构

```sh
/
├── root/
│   └── .mysql_secret  # MySQL 5.7 root 密码文件
└── usr/
    └── ports/
        └── databases/
            ├── mysql55-server/  # MySQL 5.5 Server Port
            ├── mysql56-server/  # MySQL 5.6 Server Port
            └── mysql57-server/  # MySQL 5.7 Server Port
```

## MySQL 5.5/5.6

### 安装

可以拉取旧版的 ports 编译安装。以下 commit 来自 FreeBSD ports 仓库历史，是各 MySQL 5.x 版本在官方源中停止支持前的最后提交。使用旧版本需注意其已停止官方维护，存在安全与兼容性风险。

MySQL 5.5：

```sh
# git clone https://github.com/freebsd/freebsd-ports /usr/ports   # 克隆 FreeBSD ports 仓库到 /usr/ports
# cd /usr/ports && git checkout 9f0ff92f6deaa2187cdb33a27f5aebbdd2c0d71d      # 进入 ports 目录并切换到 MySQL 5.5 的最后提交
# cd /usr/ports/databases/mysql55-server/ && make install clean   # 进入 MySQL 5.5 Server 目录并编译安装，同时清理临时文件
```

MySQL 5.6：

```sh
# git clone https://github.com/freebsd/freebsd-ports /usr/ports   # 克隆 FreeBSD ports 仓库到 /usr/ports
# cd /usr/ports && git checkout 7cab9ba40f8fd39656074d22b474ed7408e1368e      # 进入 ports 目录并切换到 MySQL 5.6 的最后提交
# cd /usr/ports/databases/mysql56-server/ && make install clean   # 进入 MySQL 5.6 Server 目录并编译安装，同时清理临时文件
```

### 启动服务

安装完成后，需要配置并启动 MySQL 服务。

```sh
# sysrc mysql_enable="YES"         # 设置 MySQL 服务开机自动启动
# service mysql-server start       # 启动 MySQL 服务实例
```

### 配置

服务启动后，可以运行 MySQL 安全配置向导，设置 root 密码和安全选项：

```sh
# mysql_secure_installation
```

输出：

```sql
# mysql_secure_installation


NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.


Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


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.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... 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? [Y/n] n
 ... skipping.

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? [Y/n] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...
```

### 使用

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

```sh
# mysql -u root -p
```

示例输出：

```sql
# mysql -u root -p      # 登录到 MySQL
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 12
Server version: 5.6.51 Source distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

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> show databases;       -- 列出当前 MySQL 实例中的所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
```

## MySQL 5.7

如果是从旧版本升级，请先执行 `mysql_upgrade` 命令。

### 安装

MySQL 5.7 的安装方式与 5.5/5.6 类似，也需要拉取旧版的 ports 编译安装。以下 commit 是 MySQL 5.7 在官方源中停止支持前的最后提交。

```sh
# git clone https://github.com/freebsd/freebsd-ports /usr/ports   # 克隆 FreeBSD ports 仓库到 /usr/ports
# cd /usr/ports && git checkout 69e2e87fa56b54e267429b326f7f6188a7baaa71      # 进入 ports 目录并切换到指定的最后的 MySQL 5.7 提交
# cd /usr/ports/databases/mysql57-server/ && make install clean   # 进入 MySQL 5.7 Server 目录并编译安装，同时清理临时文件
```

### 启动服务

```sh
# sysrc mysql_enable=YES         # 设置 MySQL 服务开机自启
# service mysql-server start     # 启动 MySQL 服务
```

示例输出（可在 `/root/.mysql_secret` 文件中查看生成的 root 密码，例如 `q(<p2ZZ>lX/:`）：

```shell
# sysrc mysql_enable=YES      # 设置 MySQL 服务开机自启
mysql_enable:  -> YES
# service mysql-server start      # 启动 MySQL 服务
Starting mysql.
# cat /root/.mysql_secret      # 打印 MySQL root 的密码
# Password set for user 'root@localhost' at 2021-12-13 00:21:02
q(<p2ZZ>lX/:
```

### 尝试登录

登录出现报错，提示需要修改密码。

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

```sql
# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.36-log

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 实例中的所有数据库
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
-- 注意：请使用 ALTER USER 语句修改密码，例如：ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
root@localhost [(none)]>
```

### 修改密码

按照提示将密码修改为 `your_new_password`，然后刷新权限。

```sql
root@localhost [(none)]> SET PASSWORD = PASSWORD('your_new_password');      -- 为当前 MySQL 用户设置新密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
root@localhost [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
```

密码修改完成后，可以正常登录：

```sql
# mysql -u root -p      # 使用 root 用户登录 MySQL，提示输入密码
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.36-log 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.00 sec)
```

## 课后习题

1. 在最新的 FreeBSD RELEASE 中拉取 MySQL 5.7 对应版本的 FreeBSD ports 源码，构建 MySQL 5.7 并在 QEMU 中启动，并贡献至本文。
2. 使 MySQL 5.7 配置现代化。
