01 在 Ubuntu 22.04 上安装 MySQL

在 Ubuntu 22.04 上安装 MySQL

安装前请切换至 root 账户!

1. 第一步:检查是否已安装 MySQL / MariaDB

1
2
dpkg -l | grep -i mysql
dpkg -l | grep -i mariadb

如果有输出(比如 mysql-common, mariadb-client-core 等),建议清理。

2. 第二步:彻底卸载旧版本(安全执行,无残留)

1
2
3
4
5
6
7
8
9
10
11
12
# 停止服务(如果存在)
sudo systemctl stop mysql mariadb 2>/dev/null

# 卸载所有相关包
sudo apt remove --purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-* mariadb-client mariadb-server mariadb-common -y

# 清理依赖和配置
sudo apt autoremove -y
sudo apt autoclean

# 删除残留数据目录(谨慎!确保无重要数据)
sudo rm -rf /etc/mysql /var/lib/mysql

如果你 100% 确定是全新系统,这步可跳过,但执行也无害(没东西可删)。

3. 第三步:安装 MySQL 8.0(Ubuntu 22.04/24.04 默认就是 8.0.43+)

1
2
sudo apt update
sudo apt install mysql-server -y

Ubuntu 22.04 默认装 8.0.39~8.0.43,24.04 可能更高(如 8.0.39+),都属于 MySQL 8.0 系列,完全符合企业主流。

4. 第四步:安全加固(必须做!)

1
sudo mysql_secure_installation

按提示:

  • 是否启用密码强度验证? → 作为初学者直接选 n。 选 y 就表示要强制高强度密码,如果太弱(如 123456),MySQL 会 拒绝接受
  • 然后依次问是否 (全 y)
    • 删除匿名用户? → 选 y
    • 禁止 root 远程登录? → 选 y
    • 删除 test 数据库? → 选 y
    • 重载权限表? → 选 y

5. 第五步:自定义密码

为什么没让设置 root 密码?

因为 Ubuntu 的 MySQL 包默认使用 auth_socket 插件(也叫 unix_socket)进行认证,而不是密码。auth_socket 是什么? 它允许 系统用户 root 直接通过 sudo mysql 无密码登录 MySQL,前提是你是 Linux 的 root 用户。

所以:MySQL 的 root 用户并没有密码,而是“信任”系统 root。这就是为什么 mysql_secure_installation 里跳过了设密码步骤,并提示:“Skipping password set for root as authentication with auth_socket is used by default.”

如果希望像传统方式那样用 mysql -u root -p 输入密码登录,需要手动改认证方式:

1. 步骤 1:用 socket 方式登录(无需密码)

1
sudo mysql

2. 步骤 2:修改 root 用户为密码认证

1
2
3
4
5
6
7
8
9
10
11
-- 查看当前 root 用户的认证方式
SELECT user, host, plugin FROM mysql.user;

-- 将 root@localhost 改为密码认证(MySQL 8.0 默认插件是 caching_sha2_password)
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的强密码';

-- 刷新权限
FLUSH PRIVILEGES;

-- 退出
EXIT;

'你的强密码' 换成你自己的密码,比如 'MyRootPass123!'

3. 步骤 3:验证密码登录

1
mysql -u root -p

输入刚设的密码,就能成功登录。

6. 第六步:配置

修改文件:/etc/mysql/mysql.conf.d/mysqld.cnf 内容(这是主配置文件),下面给出文件全部配置,可直接进行覆盖:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#
# The MySQL database server configuration file.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld]
#
# * Basic Settings
#
user = mysql
# pid-file = /var/run/mysqld/mysqld.pid
# socket = /var/run/mysqld/mysqld.sock
# port = 3306
# datadir = /var/lib/mysql


# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
# tmpdir = /tmp
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1

# ===== 自定义扩展配置 =====
# 设置默认字符集为 utf8mb4,防止中文和 emoji 乱码
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci

# 启用远程访问(默认仅本机),保持仅本地访问(通过 Xshell 登录服务器再操作)可将 0.0.0.0 设置为 127.0.0.1
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0

# 使用传统认证插件,兼容 Navicat、旧版客户端
default-authentication-plugin = mysql_native_password

# 可选:增加连接稳定性
# max_connections = 200
# wait_timeout = 600
# interactive_timeout = 600

#
# * Fine Tuning
#
key_buffer_size = 16M
# max_allowed_packet = 64M
# thread_stack = 256K

# thread_cache_size = -1

# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options = BACKUP

# max_connections = 151

# table_open_cache = 4000

#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
#
# Log all queries
# Be aware that this log type is a performance killer.
# general_log_file = /var/log/mysql/query.log
# general_log = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
# slow_query_log = 1
# slow_query_log_file = /var/log/mysql/mysql-slow.log
# long_query_time = 2
# log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
# server-id = 1
# log_bin = /var/log/mysql/mysql-bin.log
# binlog_expire_logs_seconds = 2592000
max_binlog_size = 100M
# binlog_do_db = include_database_name
# binlog_ignore_db = include_database_name

保存后重启服务让配置生效:

1
2
sudo systemctl restart mysql
sudo systemctl status mysql