暂无图片
暂无图片
5
暂无图片
暂无图片
暂无图片

Ubuntu上的MySQL 8.4.5安装:一行命令背后的系统级操作实录 | 不只是apt install那么简单

原创 听见风的声音 2025-07-10
746

1 前言

MySQL数据库的最常用的方式是用二进制包,这种方式下数据库软件的安装位置、数据库数据文件的位置都是可控的,这种方式稍微复杂一点,安装时要创建用户、创建数据文件目录、解决软件依赖、手动初始化数据库等。使用rpm或者apt包安装就简单多了,可是对DBA来说又不太透明,很多细节不容易搞清楚,本文除了介绍软件包安装步骤外,主要时说明白这种安装方式隐藏的细节。

2 安装MySQL

2.1 下载MySQL官方APT仓库包

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

root@myserver:~# wget https://repo.mysql.com//mysql-apt-config_0.8.34-1_all.deb root@myserver:~# ls -l total 20 -rw-r--r-- 1 root root 18108 Apr 2 16:51 mysql-apt-config_0.8.34-1_all.deb
2.2 安装的APT仓库包、更新APT仓库

安装时指定下载包的全路径或者用当前路径指定,只写文件名默认会从Ubuntu官网安装,官网没有指定包时会报错。

root@myserver:~# apt install ./mysql-apt-config_0.8.34-1_all.deb Note, selecting 'mysql-apt-config' instead of './mysql-apt-config_0.8.34-1_all.deb' The following packages were automatically installed and are no longer required: linux-headers-6.11.0-8 linux-modules-6.11.0-8-generic linux-tools-6.11.0-8 linux-headers-6.11.0-8-generic linux-modules-extra-6.11.0-8-generic linux-tools-6.11.0-8-generic Use 'apt autoremove' to remove them. Installing: mysql-apt-config Summary: Upgrading: 0, Installing: 1, Removing: 0, Not Upgrading: 189 Download size: 0 B / 18.1 kB Space needed: 35.8 kB / 50.3 GB available # 更新本地APT仓库 root@myserver:~# apt-get update Get:1 file:/cdrom oracular InRelease Ign:1 file:/cdrom oracular InRelease Get:2 file:/cdrom oracular Release Err:2 file:/cdrom oracular Release File not found - /cdrom/dists/oracular/Release (2: No such file or directory) Get:3 http://repo.mysql.com/apt/ubuntu oracular InRelease [22.7 kB] #省略多行 Get:39 http://mirrors.tuna.tsinghua.edu.cn/ubuntu oracular-backports/multiverse amd64 Components [216 B] Reading package lists... Done E: The repository 'file:/cdrom oracular Release' no longer has a Release file. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details.
2.3 安装MySQL数据库,不指定版本会默认安装当前APT库的最新版本
root@myserver:~# apt-get install mysql-server Reading package lists... Done Building dependency tree... Done Reading state information... Done #系统上下面的包已经不在需要了,可以删除 The following packages were automatically installed and are no longer required: linux-headers-6.11.0-8 linux-headers-6.11.0-8-generic linux-modules-6.11.0-8-generic linux-modules-extra-6.11.0-8-generic linux-tools-6.11.0-8 linux-tools-6.11.0-8-generic Use 'apt autoremove' to remove them. # 将要安装的另外的包 The following additional packages will be installed: libmecab2 mecab-ipadic mecab-ipadic-utf8 mecab-utils mysql-client mysql-common mysql-community-client mysql-community-client-core mysql-community-client-plugins mysql-community-server mysql-community-server-core #将要安装的新的包 The following NEW packages will be installed: libmecab2 mecab-ipadic mecab-ipadic-utf8 mecab-utils mysql-client mysql-common mysql-community-client mysql-community-client-core mysql-community-client-plugins mysql-community-server mysql-community-server-core mysql-server 0 upgraded, 12 newly installed, 0 to remove and 189 not upgraded. Need to get 44.2 MB of archives. After this operation, 292 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://repo.mysql.com/apt/ubuntu oracular/mysql-8.4-lts amd64 mysql-common amd64 8.4.5-1ubuntu24.10 [59.5 kB] #省略多行 Get:12 http://repo.mysql.com/apt/ubuntu oracular/mysql-8.4-lts amd64 mysql-server amd64 8.4.5-1ubuntu24.10 [58.2 kB] Fetched 44.2 MB in 32s (1,395 kB/s) Preconfiguring packages ... Selecting previously unselected package mysql-common. (Reading database ... 126484 files and directories currently installed.) Preparing to unpack .../00-mysql-common_8.4.5-1ubuntu24.10_amd64.deb ... Unpacking mysql-common (8.4.5-1ubuntu24.10) ... #省略多行 reading /usr/share/mecab/dic/ipadic/Noun.csv ... 60477 emitting double-array: 100% |###########################################| reading /usr/share/mecab/dic/ipadic/matrix.def ... 1316x1316 emitting matrix : 100% |###########################################| done! update-alternatives: using /var/lib/mecab/dic/ipadic to provide /var/lib/mecab/dic/debian (mecab-dictionary) in auto mode Setting up mysql-community-client-core (8.4.5-1ubuntu24.10) ... #省略多行 No VM guests are running outdated hypervisor (qemu) binaries on this host.

数据库的安装过程中会弹出菜单,提示输入root密码。

3 APT安装解析

3.1 安装程序创建的系统服务
# 查看安装程序创建的服务 root@myserver:~# systemctl status mysql ○ mysql.service - MySQL Community Server Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled) Active: inactive (dead) Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html # 查看服务的配置文件 root@myserver:~# cat /usr/lib/systemd/system/mysql.service # MySQL systemd service file [Unit] Description=MySQL Community Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network-online.target Wants=network-online.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql Type=notify ExecStartPre=+/usr/share/mysql-8.4/mysql-systemd-start pre ExecStart=/usr/sbin/mysqld TimeoutSec=0 LimitNOFILE = 10000 Restart=on-failure RestartPreventExitStatus=1 # Always restart when mysqld exits with exit code of 16. This special exit code # is used by mysqld for RESTART SQL. RestartForceExitStatus=16 # Set enviroment variable MYSQLD_PARENT_PID. This is required for restart. Environment=MYSQLD_PARENT_PID=1

上面文件中值得注意的是ExecStartPre和ExecStart两行,前者执行每个启动服务上系统执行的准备脚本,后者执行服务启动时执行的命令。服务执行的命令使用的是默认的配置,检查一下默认配置下数据文件的路径

root@myserver:~# mysqld --verbose --help|grep -i datadir -h, --datadir=name Path to the database root directory 'innodb-data-home-dir;innodb-undo-directory;datadir' datadir /var/lib/mysql/

看一下这个目录下的文件

root@myserver:~# ls -l /var/lib/mysql/ total 91212 -rw-r----- 1 mysql mysql 56 Jul 8 02:22 auto.cnf -rw-r----- 1 mysql mysql 506 Jul 8 02:22 binlog.000001 -rw-r----- 1 mysql mysql 16 Jul 8 02:22 binlog.index -rw------- 1 mysql mysql 1705 Jul 8 02:22 ca-key.pem -rw-r--r-- 1 mysql mysql 1108 Jul 8 02:22 ca.pem -rw-r--r-- 1 mysql mysql 1108 Jul 8 02:22 client-cert.pem -rw------- 1 mysql mysql 1709 Jul 8 02:22 client-key.pem -rw-r----- 1 mysql mysql 6291456 Jul 8 02:22 '#ib_16384_0.dblwr' -rw-r----- 1 mysql mysql 14680064 Jul 8 02:22 '#ib_16384_1.dblwr' -rw-r----- 1 mysql mysql 3599 Jul 8 02:22 ib_buffer_pool -rw-r----- 1 mysql mysql 12582912 Jul 8 02:22 ibdata1 drwxr-x--- 2 mysql mysql 4096 Jul 8 02:22 '#innodb_redo' drwxr-x--- 2 mysql mysql 4096 Jul 8 02:22 '#innodb_temp' drwxr-x--- 2 mysql mysql 4096 Jul 8 02:22 mysql -rw-r----- 1 mysql mysql 26214400 Jul 8 02:22 mysql.ibd -rw-r----- 1 mysql mysql 124 Jul 8 02:22 mysql_upgrade_history drwxr-x--- 2 mysql mysql 4096 Jul 8 02:22 performance_schema -rw------- 1 mysql mysql 1709 Jul 8 02:22 private_key.pem -rw-r--r-- 1 mysql mysql 452 Jul 8 02:22 public_key.pem -rw-r--r-- 1 mysql mysql 1108 Jul 8 02:22 server-cert.pem -rw------- 1 mysql mysql 1705 Jul 8 02:22 server-key.pem drwxr-x--- 2 mysql mysql 4096 Jul 8 02:22 sys -rw-r----- 1 mysql mysql 16777216 Jul 8 02:22 undo_001 -rw-r----- 1 mysql mysql 16777216 Jul 8 02:22 undo_002

启动数据库并登录

root@myserver:~# systemctl start mysql root@myserver:~# mysql -uroot -p******** mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.4.5 MySQL Community Server - GPL Copyright (c) 2000, 2025, 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.
3.2 MySQL各类文件的安装位置

不同于二进制安装方式,使用安装包安装MySQL的文件被分散到不同的位置,下面简单介绍一下
客户端文件安装到/bin目录下

root@myserver:/bin# ls -l /bin/mysql* -rwxr-xr-x 1 root root 7676776 Mar 31 08:33 /bin/mysql -rwxr-xr-x 1 root root 7333976 Mar 31 08:33 /bin/mysqladmin -rwxr-xr-x 1 root root 7745864 Mar 31 08:33 /bin/mysqlbinlog -rwxr-xr-x 1 root root 7347096 Mar 31 08:33 /bin/mysqlcheck -rwxr-xr-x 1 root root 169064 Mar 31 08:33 /bin/mysql_config_editor -rwxr-xr-x 1 root root 27399 Mar 31 08:33 /bin/mysqld_multi -rwxr-xr-x 1 root root 29137 Mar 31 08:33 /bin/mysqld_safe -rwxr-xr-x 1 root root 7431320 Mar 31 08:33 /bin/mysqldump -rwxr-xr-x 1 root root 7719 Mar 31 08:33 /bin/mysqldumpslow -rwxr-xr-x 1 root root 7317880 Mar 31 08:33 /bin/mysqlimport -rwxr-xr-x 1 root root 7389120 Mar 31 08:33 /bin/mysql_migrate_keyring -rwxr-xr-x 1 root root 7310008 Mar 31 08:33 /bin/mysql_secure_installation -rwxr-xr-x 1 root root 7324696 Mar 31 08:33 /bin/mysqlshow -rwxr-xr-x 1 root root 7335480 Mar 31 08:33 /bin/mysqlslap -rwxr-xr-x 1 root root 80880 Mar 31 08:33 /bin/mysql_tzinfo_to_sql--

服务器文件安装到/sbin目录下

root@myserver:/bin# ls -l /sbin/mysql* -rwxr-xr-x 1 root root 64341688 Mar 31 08:33 /sbin/mysqld -rwxr-xr-x 1 root root 3125 Sep 18 2024 /sbin/mysqld_qslower-bpfcc

库文件和plugin安装到/lib/mysql

root@myserver:/lib/mysql/private# ls -l /lib/mysql total 8 drwxr-xr-x 2 root root 4096 Jul 8 02:22 plugin drwxr-xr-x 3 root root 4096 Jul 8 02:22 private

配置文件放到/etc/mysql目录下

root@myserver:~# ls -l /etc/mysql total 16 drwxr-xr-x 2 root root 4096 Jul 8 02:22 conf.d lrwxrwxrwx 1 root root 24 Jul 8 02:22 my.cnf -> /etc/alternatives/my.cnf -rw-r--r-- 1 root root 1498 Mar 31 08:33 my.cnf.fallback -rw-r--r-- 1 root root 1591 Jul 9 02:14 mysql.cnf drwxr-xr-x 2 root root 4096 Jul 9 02:14 mysql.conf.d

错误日志在/var/log/msyql目录下

root@myserver:~# ls -l /var/log/mysql total 12 -rw-r----- 1 mysql adm 8338 Jul 9 02:15 error.log
3.3 更改数据文件目录

关闭数据库后移动数据文件目录至新位置

root@myserver:/# mv /var/lib/mysql/ /opt #检查一下目录权限 root@myserver:/# ls -l /opt/mysql total 103504 -rw-r----- 1 mysql mysql 56 Jul 8 02:22 auto.cnf -rw-r----- 1 mysql mysql 506 Jul 8 02:22 binlog.000001 -rw-r----- 1 mysql mysql 158 Jul 8 02:30 binlog.000002 ........ -rw-r----- 1 mysql mysql 16777216 Jul 8 02:32 undo_002

进行到这里,有两种方法可以更改数据库的数据文件路径,常用的方式通过my.cnf来改,这里介绍另一种方式,通过设置systemctl的环境变量来改,和MySQL服务相关的变量名称是MYSQLD_OPTS,步骤如下:

root@myserver:/var/lib# systemctl set-environment MYSQLD_OPTS="--data_dir=/opt/mysql" root@myserver:/var/lib# systemctl start mysql --登录数据库

数据库成功启动了。但是,这种方式有一个弊端如下:

--登录数据库查看数据文件路径 mysql> show variables like 'data%'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | datadir | /var/lib/mysql/ | +---------------+-----------------+ 1 row in set (0.00 sec)

这里的路径显示的还是MySQL的默认设置,同实际使用的不符。

4 MySQL的自动适应性配置参数

自动适应性配置参数(Auto Adapting Configuration Parameters)是MySQL 8.4的新特性。激活这个特性后,MySQL的配置参数会根据运行MySQL的服务器的配置来决定。这个特性是否开启由参数innodb_dedicated_server来决定。为了向后保持兼容,这个特定默认是关闭的,innodb_dedicated_server的设置为off,在这种情况下,数据库缓冲池的相关设置如下:

mysql> show variables like '%pool%'; +-------------------------------------+----------------+ | Variable_name | Value | +-------------------------------------+----------------+ | innodb_buffer_pool_chunk_size | 134217728 | | innodb_buffer_pool_dump_at_shutdown | ON | | innodb_buffer_pool_dump_now | OFF | | innodb_buffer_pool_dump_pct | 25 | | innodb_buffer_pool_filename | ib_buffer_pool | | innodb_buffer_pool_in_core_file | OFF | | innodb_buffer_pool_instances | 1 | | innodb_buffer_pool_load_abort | OFF | | innodb_buffer_pool_load_at_startup | ON | | innodb_buffer_pool_load_now | OFF | --缓冲的大小时0.125GB,和chunk_size相等,这台机器的物理内存是8G,这个设置显然太小 | innodb_buffer_pool_size | 134217728 | +-------------------------------------+----------------+ 11 rows in set (0.00 sec)

启动这个特性,只需要设置innodb_dedicated_server为on即可,编辑一下mysqld进程配置文件搜索路径下任意my.cnf文件,加入一下内容

root@myserver:/etc/mysql# cat /etc/alternatives/my.cnf !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/ [mysqld] max_connections = 1000 #这个参数也建议设置一下 innodb_dedicated_server = ON

重启数据库,检查一下缓冲池的设置

mysql> show variables like '%pool%'; +-------------------------------------+----------------+ | Variable_name | Value | +-------------------------------------+----------------+ | innodb_buffer_pool_chunk_size | 134217728 | | innodb_buffer_pool_dump_at_shutdown | ON | | innodb_buffer_pool_dump_now | OFF | | innodb_buffer_pool_dump_pct | 25 | | innodb_buffer_pool_filename | ib_buffer_pool | | innodb_buffer_pool_in_core_file | OFF | | innodb_buffer_pool_instances | 1 | | innodb_buffer_pool_load_abort | OFF | | innodb_buffer_pool_load_at_startup | ON | | innodb_buffer_pool_load_now | OFF | --缓冲池设置为5.25GBytes,这台机器的内存为8GB | innodb_buffer_pool_size | 5637144576 | +-------------------------------------+----------------+ 11 rows in set (0.00 sec)

innodb缓冲池设置的公式如下:
M指服务器的物理内存
128 MB if M < 1 GB
M * 0.50 GB if 1 GB ≤ M ≤ 4 GB
M * 0.75 GB if M > 4 GB

5 后记

这是第一次在Ubuntu上安装MySQL数据库,整个安装过程很简单,安装完之后也可以更改数据文件路径,设置数据库配置参数,只要明白了安装背后的细节,这种安装方式也是可以控制的,而且非常方面,和windows下安装差不多。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论