可行,需先用debconf-set-selections预设root密码再安装;否则会阻塞。Ubuntu默认不支持纯静默安装,必须预埋mysql-server/root_password和root_password_again两个key,且推荐配置MySQL官方APT源以确保版本与debconf兼容。

非交互式安装 mysql-server 本身是否可行
直接运行 sudo apt-get install -y mysql-server 是「半自动」的:它跳过确认提示,但遇到 mysql-server 的 debconf 配置项(如 root 密码)时仍会阻塞,除非提前预设。Ubuntu 默认不提供 root 密码预填机制,所以单纯加 -y 不足以真正静默。
真正可行的路径是两步走:先用 debconf-set-selections 预埋配置,再执行安装。否则脚本会在终端卡住,或 fallback 到空密码(MySQL 5.7+ 默认禁用空密码登录)。
- 必须提前设置
mysql-server/root_password和mysql-server/root_password_again两个 debconf key - 若系统已装过 MySQL,需先清理残留:
sudo apt-get purge mysql-server mysql-client mysql-common+sudo rm -rf /etc/mysql /var/lib/mysql - Ubuntu 22.04+ 默认安装 MySQL 8.0,其默认认证插件是
caching_sha2_password,旧客户端可能连不上——这点在自动化后常被忽略
如何预设 root 密码避免安装时卡住
debconf 是 Ubuntu/Debian 管理软件包交互式配置的底层工具。要跳过密码输入环节,得用 debconf-set-selections 注入值。注意 key 名和格式必须严格匹配,否则无效。
执行以下命令前,请替换 your_strong_password 为实际密码:
echo "mysql-server mysql-server/root_password password your_strong_password" | sudo debconf-set-selections echo "mysql-server mysql-server/root_password_again password your_strong_password" | sudo debconf-set-selections
- 这两行必须在
apt-get install之前运行,且不能颠倒顺序 - key 名中的斜杠
/是分隔符,不可写成下划线或空格 - 如果密码含特殊字符(如
$、!),建议用单引号包裹整个 echo 字符串,或对字符转义 - Ubuntu 20.04 及以后版本中,该方法对 MySQL 8.0 有效;MySQL 5.7 同样适用,但需确保 APT 源已正确配置(见下一条)
为什么必须先配置 MySQL 官方 APT 源
Ubuntu 自带仓库里的 mysql-server 版本老旧(如 20.04 自带 8.0.33,但 22.04/24.04 已升级),且不包含最新安全补丁。更重要的是:官方源才提供完整 debconf 支持,社区打包的版本可能缺失预设项支持,导致 debconf-set-selections 失效。
面向 CRM 系统的 MySQL 8 数据库模式设计。当用户需要为客户关系管理(CRM)系统设计、评审、优化或生成数据库模式时,应使用此项技能。
- 下载并安装官方 APT 配置包:
wget https://repo.mysql.com/mysql-apt-config_0.8.30-1_all.deb && sudo dpkg -i mysql-apt-config_0.8.30-1_all.deb - 安装过程中会弹出 ncurses 界面,按 Tab 键选中
Ok后回车即可(只需一次,后续可静默) - 之后务必运行
sudo apt update,否则apt install mysql-server仍会拉取系统自带旧包 - 若想锁定 MySQL 5.7,需在安装
mysql-apt-config时手动选择版本,或安装后改/etc/apt/sources.list.d/mysql.list中的dist行为xenial(仅限 5.7)
mysql_secure_installation 怎么也做到非交互
mysql_secure_installation 默认全程交互,但可通过环境变量 + expect 脚本绕过。更轻量的做法是用 MySQL 8.0+ 的 --skip-password 模式配合 SQL 命令初始化,但最稳的仍是 expect。
一个最小可用 expect 脚本示例(保存为 secure.exp):
#!/usr/bin/expect -f set timeout 10 set password "your_strong_password" spawn mysql_secure_installation expect "Press y|Y for Yes, any other key for No:" send "y\r" expect "Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:" send "2\r" expect "Change the password for root ?" send "n\r" expect "Remove anonymous users?" send "y\r" expect "Disallow root login remotely?" send "y\r" expect "Remove test database and access to it?" send "y\r" expect "Reload privilege tables now?" send "y\r" expect eof
- 需先安装 expect:
sudo apt-get install -y expect - 脚本中所有
expect字符串必须与实际输出完全一致,包括空格和标点;MySQL 8.0 输出略有调整,建议先手动跑一次记下确切提示 - 不要在脚本里用
sudo调用mysql_secure_installation,expect 无法接管 sudo 密码输入 - 执行前加可执行权限:
chmod +x secure.exp && sudo ./secure.exp
真正容易被忽略的是:即使完成了全部非交互步骤,MySQL 8.0 默认绑定 127.0.0.1,且 root@localhost 使用 caching_sha2_password 插件——这意味着你写的自动化脚本如果用 Python + mysql-connector 或老版 PHP mysqli 连接,大概率报错 Authentication plugin 'caching_sha2_password' cannot be loaded。解决办法要么换驱动,要么安装后立刻执行 SQL 切换插件:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';










