一、存储引擎(了解)
1. 体系结构和引擎简介
先把 MySQL 的整体结构过一眼,从下往上分四层:

- 连接层:客户端连接器(Native C API、JDBC、ODBC、.NET、PHP、Perl、Python、Ruby、Cobol)进来后进连接池,连接池管认证、线程复用、连接数限制、内存检查这些事
- 服务层:SQL 接口、解析器、查询优化器、缓存,还有系统管理和控制工具(备份恢复、安全、复制、集群、管理、配置、迁移)
- 引擎层:可插拔存储引擎,InnoDB、MyISAM、NDB、Archive、Federated、Memory、Merge、Partner、Community、Custom 都能插进来
- 存储层:落到系统文件(NTFS、ufs、ext2/3、NFS、SAN、NAS)和文件日志(Redo、Undo、Data、Index、Binary、Error、Query、Slow)
存储引擎是 MySQL 里负责数据存储和提取的核心组件。不同引擎提供不同的存储机制、索引技巧、锁定水平。
建表的时候可以指定引擎,默认是 InnoDB:
CREATE TABLE `emp` (
`id` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
`name` varchar(50) NOT NULL COMMENT '姓名',
`age` int DEFAULT NULL COMMENT '年龄',
`job` varchar(20) DEFAULT NULL COMMENT '职位',
`salary` int DEFAULT NULL COMMENT '薪资',
`entrydate` date DEFAULT NULL COMMENT '入职时间',
`managerid` int DEFAULT NULL COMMENT '直属领导ID',
`dept_id` int DEFAULT NULL COMMENT '部门ID',
PRIMARY KEY (`id`),
KEY `fk_emp_dept_id` (`dept_id`),
CONSTRAINT `fk_emp_dept_id` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='员工表'
# 存储引擎默认 InnoDB
# 在创建表时,指定存储引擎
create table 表名(
字段1 字段1类型 [comment 字段1注释],
…….
字段n 字段n类型 [comment 字段n注释]
) engine = innodb [comment 表注释];
# 查看当前数据库引擎
show engines;
# 建表时创建存储引擎
create table my_myisam (
id int,
name varchar(10)
) engine=MyISAM;
create table my_memory (
id int,
name varchar(10)
) engine=Memory;
最后那句建 Memory 表,原笔记表名又写了一遍my_myisam,两个表不能重名,这里改成了my_memory。
用图形工具看引擎:Navicat 里选中表右键 → 导航 → 转到 DDL,就能把建表语句连引擎带字符集一起翻出来。

2. InnoDB、MyISAM、Memory 对比
三个引擎里 InnoDB 用得最多,默认也是它。

| 特点 | InnoDB | MyISAM | Memory |
|---|---|---|---|
| 存储限制 | 64TB | 有 | 有 |
| 事务安全 | 支持 | - | - |
| 锁机制 | 行锁 | 表锁 | 表锁 |
| B+tree 索引 | 支持 | 支持 | 支持 |
| Hash 索引 | - | - | 支持 |
| 全文索引 | 支持(5.6 版本之后) | 支持 | - |
| 空间使用 | 高 | 低 | N/A |
| 内存使用 | 高 | 低 | 中等 |
| 批量插入速度 | 低 | 高 | 高 |
| 支持外键 | 支持 | - | - |

一句话概括:InnoDB 和 MyISAM 的差别主要在事务、外键、行级锁这三样,InnoDB 全有,MyISAM 全没有。
所以应用的场景也分得开:
- InnoDB:业务系统里对事务、数据完整性要求高的核心数据
- MyISAM:业务系统里的非核心事务

二、Linux 安装 MySQL
从运维篇开始用 Linux 装 MySQL,后面其他章节都改用 docker 装了。
整个流程就四步:看内存够不够用 -- 下载包解压安装 -- 启动服务查看存活 -- 初始化配置。
root 用户直接输入 mysql 就能连上数据库,不用密码。
包从 https://downloads.mysql.com/archives/community/ 下。比起 zip,msi 是不需要手动配置的(那是 Windows 上的事,这里装 Linux)。
先确认系统版本:
root@VM-4-16-ubuntu:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.4 LTS
Release: 24.04
Codename: noble
去官网选 9.7.1 + Ubuntu Linux + 24.04 x86 64-bit,下 DEB Bundle 那个 474.6M 的包:

1. 传包、看内存、解压
把包上传到 /root 目录再操作:
# 先看下内存够不够了
root@VM-4-16-ubuntu:~# free -h
total used free shared buff/cache available
Mem: 1.9Gi 467Mi 169Mi 2.5Mi 1.5Gi 1.5Gi
Swap: 1.9Gi 524Ki 1.9Gi
# available 还剩 1.5 Gi,学习环境够用了
mkdir mysql
tar -xf mysql-server_9.7.1-1ubuntu24.04_amd64.deb-bundle.tar -C /root/mysql
root@VM-4-16-ubuntu:~# cd mysql
root@VM-4-16-ubuntu:~/mysql# ll
total 486028
drwxr-xr-x 2 root root 4096 Aug 26 12:41 ./
drwx------ 7 root root 4096 Aug 26 12:39 ../
-rw-r--r-- 1 7155 31415 1526446 Jun 3 21:57 libmysqlclient24_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 31405410 Jun 3 21:57 libmysqlclient-dev_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 58352 Jun 3 21:57 mysql-client_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 59660 Jun 3 21:57 mysql-common_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 2345130 Jun 3 21:57 mysql-community-client_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 1840816 Jun 3 21:57 mysql-community-client-core_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 2030252 Jun 3 21:57 mysql-community-client-plugins_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 67968 Jun 3 21:58 mysql-community-server_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 48654750 Jun 3 21:57 mysql-community-server-core_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 59869162 Jun 3 21:58 mysql-community-server-debug_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 335483212 Jun 3 21:58 mysql-community-test_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 14189724 Jun 3 21:58 mysql-community-test-debug_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 58340 Jun 3 21:58 mysql-server_9.7.1-1ubuntu24.04_amd64.deb
-rw-r--r-- 1 7155 31415 58352 Jun 3 21:58 mysql-testsuite_9.7.1-1ubuntu24.04_amd64.deb
顺手记一下存储单位:Mi 是 Mebibyte,二进制,1 Mi = 1024 KiB;MB 是 Megabyte,十进制,1 MB = 1000 KB。所以 free -h 出来的容量和硬盘标称容量对不上的时候,先想想是哪套进制。

2. 用 dpkg 装
# 1. 进入解压目录
cd /root/mysql
# 2. 确认 .deb 文件都在
ls -lh
# 3. 安装(按顺序)
sudo dpkg -i mysql-common_*.deb
sudo dpkg -i mysql-community-client-plugins_*.deb
sudo dpkg -i mysql-community-client-core_*.deb
sudo dpkg -i mysql-community-client_*.deb
sudo dpkg -i mysql-community-server-core_*.deb
sudo dpkg -i mysql-community-server_*.deb
sudo dpkg -i mysql-server_*.deb
dpkg -i 和 apt install 的区别:
| 对比 | dpkg -i | apt install |
|---|---|---|
| 安装来源 | 本地 .deb 文件 | 在线软件源 |
| 自动解决依赖 | ❌ 不会 | ✅ 会 |
| 适用场景 | 离线安装、装特定版本 | 日常安装 |
rpm -- .rpm 和 dpkg -- .deb 相当于 win 的 exe 安装程序,而 apt/yum 就是从网上一键下载安装。
rpm 和 yum/apt/dnf 的区别:-ivh 其实是 -i -v -h 的简写,就是"装这个包、打印详细信息、显示 ### 进度条"。rpm 继承了古老的 Unix 风格,短选项能连在一起写;dpkg 虽然也能连写,但习惯上还是分开写。rpm 不像 dpkg 那样能写成 rpm install。这套拆法对 dpkg -i 也适用,记住规律就行。
3. 初始化配置
# 查看包安装情况
root@iZ2ze8uighngnzvz4mscv9Z:~# dpkg -l | grep mysql
ii mysql-common 9.7.1-1ubuntu24.04 amd64 Common files shared between packages
iU mysql-community-client 9.7.1-1ubuntu24.04 amd64 MySQL Client
iU mysql-community-client-core 9.7.1-1ubuntu24.04 amd64 MySQL Client Core Binaries
iU mysql-community-client-plugins 9.7.1-1ubuntu24.04 amd64 MySQL Client plugin
iU mysql-community-server 9.7.1-1ubuntu24.04 amd64 MySQL Server
iU mysql-community-server-core 9.7.1-1ubuntu24.04 amd64 MySQL Server Core Binaries
iU mysql-server 9.7.1-1ubuntu24.04 amd64 MySQL Server meta package depending on latest version
# 启动
root@VM-4-16-ubuntu:~/mysql# systemctl enable --now mysql
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /usr/lib/systemd/system/mysql.service.
# 注册并启动了服务
# 确认端口和服务存活
root@VM-4-16-ubuntu:~/mysql# ss -tunl | grep 3306
tcp LISTEN 0 70 *:33060 *:*
tcp LISTEN 0 4096 *:3306 *:*
root@VM-4-16-ubuntu:~/mysql# ps -ef | grep mysql
mysql 2423847 1 3 12:42 ? 00:00:00 /usr/sbin/mysqld
root 2424002 2422352 0 12:43 pts/3 00:00:00 grep --color=auto mysql
# 查询生成的密码
cat /var/log/mysql.log | grep 'temporary password'
# 没有生成密码,直接登录
root@VM-4-16-ubuntu:/var/log/mysql# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 9.7.1 MySQL Community Server - GPL
Copyright (c) 2000, 2026, 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.
mysql>
# 配置密码&刷新权限缓存
alter user 'root'@'localhost' identified by '密码';
flush privileges;
# 密码太简单可能报错,强制设置:不推荐
set global validate_password.policy = 0;
set global validate_password.length = 4;
# 退出尝试连接(交互式)
root@VM-4-16-ubuntu:~# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 9.7.1 MySQL Community Server - GPL
Copyright (c) 2000, 2026, 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.
mysql>
dpkg -l | grep mysql 里状态是 iU(installed,unpacked,未配置),这是正常的,服务起得来就行。33060 是 MySQL X Protocol 的端口,3306 才是平时连的那个。
改密码这一步单独说一下:装完 root 密码是自动生成的,不想记就直接改成自己熟的。

流程是:先 ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';,会报密码太简单;然后 set global validate_password.policy = 0; 和 set global validate_password.length = 4; 把校验规则降下来,再执行一次改密码的语句就成了。
4. 远程登录
# 默认的root用户只能当前节点localhost访问,是无法远程访问的
# 还需要创建一个root账户,用户远程访问
create user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
# 给root用户分配权限
grant all on *.* to 'root'@'%';
默认那个 root@localhost 只认本机,要远程连就得单独再建一个 root@'%' 并授权,光改密码是不够的。