Ubuntu 20.04 对应的 MySQL 版本是 8.0,如果要安装 5.7 版本,有以下三种安装方式:

  • 下载 MySQL 二进制压缩包,解压并设置相关的参数即可运行。
  • 通过命令 apt install 进行安装,先下载 MySQL 5.7 对应的源,然后执行安装命令 apt install mysql-server=5.7.37-1ubuntu18.04
  • 通过命令 dpkg -i 的方式安装,下载 MySQL 5.7 对应的 deb 文件,然后执行安装命令即可。

这里只说明第三种方式的安装过程,即通过命令 dpkg -i 的方式。具体步骤如下:

下载

MySQL 官网 下载文件 mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar

1
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar

解压

将下载好的文件解压到相应的目录,这里是解压到目录 /mnt/d/opt/mysql-deb

1
tar xvf mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar -C /mnt/d/opt/mysql-deb

进入解压后的目录 cd /mnt/d/opt/mysql-deb,解压后的文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  mysql-deb ll
total 153M
-rwxrwxrwx 1 root root 2.1M Nov 30 2021 libmysqlclient-dev_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 1.6M Nov 30 2021 libmysqlclient20_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 19M Nov 30 2021 libmysqld-dev_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 69K Nov 30 2021 mysql-client_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 72K Nov 30 2021 mysql-common_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 14M Nov 30 2021 mysql-community-client_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 35M Nov 30 2021 mysql-community-server_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 51M Nov 30 2021 mysql-community-source_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 32M Nov 30 2021 mysql-community-test_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 69K Nov 30 2021 mysql-server_5.7.37-1ubuntu18.04_amd64.deb
-rwxrwxrwx 1 root root 69K Nov 30 2021 mysql-testsuite_5.7.37-1ubuntu18.04_amd64.deb

NOTE:如果不需要安装测试相关的包,可以删除其中两个带 test 名称的 deb 文件:

  • mysql-community-test_5.7.37-1ubuntu18.04_amd64.deb
  • mysql-testsuite_5.7.37-1ubuntu18.04_amd64.deb

删除命令:

1
rm -f mysql-community-test_5.7.37-1ubuntu18.04_amd64.deb mysql-testsuite_5.7.37-1ubuntu18.04_amd64.deb

安装

进入目录 /mnt/d/opt/mysql-deb ,执行 dpkg -i mysql-*.deb 命令,开始安装 MySQL:

1
dpkg -i mysql-*.deb

如果安装过程中,缺少相关依赖包,会导致安装失败。

如以下提示未安装依赖包 libtinfo5libmecab2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
➜  mysql-deb dpkg -i mysql-*.deb
Selecting previously unselected package mysql-client.
(Reading database ... 49086 files and directories currently installed.)
Preparing to unpack mysql-client_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-client (5.7.37-1ubuntu18.04) ...
Selecting previously unselected package mysql-common.
Preparing to unpack mysql-common_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-common (5.7.37-1ubuntu18.04) ...
Selecting previously unselected package mysql-community-client.
Preparing to unpack mysql-community-client_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-community-client (5.7.37-1ubuntu18.04) ...
Selecting previously unselected package mysql-community-server.
Preparing to unpack mysql-community-server_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-community-server (5.7.37-1ubuntu18.04) ...
Selecting previously unselected package mysql-community-source.
Preparing to unpack mysql-community-source_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-community-source (5.7.37-1ubuntu18.04) ...
Selecting previously unselected package mysql-server.
Preparing to unpack mysql-server_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-server (5.7.37-1ubuntu18.04) ...
Setting up mysql-common (5.7.37-1ubuntu18.04) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
dpkg: dependency problems prevent configuration of mysql-community-client:
mysql-community-client depends on libtinfo5 (>= 6); however:
**Package libtinfo5 is not installed.**

dpkg: error processing package mysql-community-client (--install):
dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of mysql-community-server:
mysql-community-server depends on libmecab2; however:
**Package libmecab2 is not installed.**

dpkg: error processing package mysql-community-server (--install):
dependency problems - leaving unconfigured
Setting up mysql-community-source (5.7.37-1ubuntu18.04) ...
dpkg: dependency problems prevent configuration of mysql-server:
mysql-server depends on mysql-community-server (= 5.7.37-1ubuntu18.04); however:
Package mysql-community-server is not configured yet.

dpkg: error processing package mysql-server (--install):
dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of mysql-client:
mysql-client depends on mysql-community-client (= 5.7.37-1ubuntu18.04); however:
Package mysql-community-client is not configured yet.

dpkg: error processing package mysql-client (--install):
dependency problems - leaving unconfigured
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.15) ...
Errors were encountered while processing:
mysql-community-client
mysql-community-server
mysql-server
mysql-client

解决办法是通过命令 apt install 把相关缺失的包装上即可,如:

1
apt install libtinfo5 libmecab2

之后再重新执行 dpkg -i mysql-*.deb 命令,安装过程中,会提示让你输入 root 用户密码。

启动 MySQL

1、使用 service 启动:

1
service mysql start

2、使用 mysqld 脚本启动:

1
/etc/init.d/mysql start

3、使用 safe_mysqld 启动:

1
safe_mysql&

停止 MySQL

1、使用 service 启动:

1
service mysql stop

2、使用 mysqld 脚本启动:

1
/etc/init.d/mysql stop

重启 MySQL

使用 service 启动:

1
service mysql restart

(END)