使用通用二进制文件在Ubuntu上安装MySQL

版本

  • Ubuntu 24.04.3 LTS
  • MySQL Community Server 8.4.6 LTS

下载

下载地址:https://dev.mysql.com/downloads/mysql/

安装

MySQL 依赖 libaio 库,使用以下命令安装:

1
2
3
4
$ apt-cache search libaio
libaio-dev - Linux kernel AIO access library - development files
libaio1t64 - Linux kernel AIO access library - shared library
$ sudo apt-get install libaio1t64 libaio-dev

在安装位置解压下载的二进制包,我的安装位置在 /opt。为了后续访问方便,加压后创建目录软连接。需使用 root 权限进行安装,所以先切换至 root 后进行后续的安装操作。

1
2
3
4
5
6
7
8
9
10
$ sudo -s
# xz -d mysql-8.4.6-linux-glibc2.28-x86_64.tar.xz
# tar xvf mysql-8.4.6-linux-glibc2.28-x86_64.tar
# ln -s mysql-8.4.6-linux-glibc2.28-x86_64 mysql
# ll
总计 16
drwxr-xr-x 4 root root 4096 9月 29 17:08 ./
drwxr-xr-x 23 root root 4096 9月 4 13:25 ../
lrwxrwxrwx 1 root root 34 9月 29 17:08 mysql -> mysql-8.4.6-linux-glibc2.28-x86_64/
drwxrwxr-x 9 root root 4096 9月 29 17:03 mysql-8.4.6-linux-glibc2.28-x86_64/

执行以下类似的命令,安装和使用 MySQL:

1
2
3
4
5
6
7
8
# cd /opt/mysql
# groupadd mysql
# useradd -r -g mysql -s /bin/false mysql
# mkdir mysql-files
# chown mysql:mysql mysql-files
# chmod 750 mysql-files
# bin/mysqld --initialize --user=mysql
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

在执行初始化时出现上面的错误,这是因为 libaio 库安装位置的问题。通过以下方式查找 libaio 库的位置,并创建软连接:

1
2
3
4
5
6
7
# find / -name "libaio.so*" 2>/dev/null
/usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2
/usr/lib/x86_64-linux-gnu/libaio.so.1t64
/usr/lib/x86_64-linux-gnu/libaio.so

# cd /usr/lib/
# ln -s x86_64-linux-gnu/libaio.so libaio.so.1

再次执行初始化成功:

1
2
3
4
5
6
7
# bin/mysqld --initialize --user=mysql
2025-09-29T09:31:28.376933Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start.
2025-09-29T09:31:28.378185Z 0 [System] [MY-013169] [Server] /opt/mysql-8.4.6-linux-glibc2.28-x86_64/bin/mysqld (mysqld 8.4.6) initializing of server in progress as process 35181
2025-09-29T09:31:28.386632Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2025-09-29T09:31:28.651250Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2025-09-29T09:31:30.673240Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: fy-ktrkZp8aq
2025-09-29T09:31:32.971427Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end.

注意保存好在初始化过程中会生成 root 用户的随机密码。登陆 MySQL 服务后,可以使用以下命令修改初始密码:

1
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';

启动 MySQL:

1
2
3
4
5
6
7
8
# bin/mysqld_safe --user=mysql &
[1] 5419
root@Ubuntu:/opt/mysql-8.4.6-linux-glibc2.28-x86_64# Logging to '/opt/mysql-8.4.6-linux-glibc2.28-x86_64/data/Ubuntu.err'.
2025-09-30T02:27:16.882553Z mysqld_safe Starting mysqld daemon with databases from /opt/mysql-8.4.6-linux-glibc2.28-x86_64/data
# ps -ef|grep mysql
root 5419 5411 0 10:27 pts/2 00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql 5488 5419 4 10:27 pts/2 00:00:00 /opt/mysql-8.4.6-linux-glibc2.28-x86_64/bin/mysqld --basedir=/opt/mysql-8.4.6-linux-glibc2.28-x86_64 --datadir=/opt/mysql-8.4.6-linux-glibc2.28-x86_64/data --plugin-dir=/opt/mysql-8.4.6-linux-glibc2.28-x86_64/lib/plugin --user=mysql --log-error=Ubuntu.err --pid-file=Ubuntu.pid
root 5539 5411 0 10:27 pts/2 00:00:00 grep --color=auto mysql

安装后设置

设置 Socket 文件

默认的 Socket 文件在 /tmp 目录下,该目录下的文件会被自动清理。如果不修改在后续使用时会出现问题,见我的另外一篇博文:无法连接本地MySQL

在 /etc/mysql/my.cnf 配置文件中添加以下内容:

1
2
[mysqld]
socket = /var/run/mysqld/mysqld.sock

设置字段内容大小写敏感

参见我的另外一篇博文:MySQL设置字段内容大小写敏感

使用 systemd 管理 MySQL 服务

参见我的另外一篇博文: 使用 systemd 管理 MySQL 服务