Ubuntu 22.04+ 官方仓库已彻底移除 MySQL 5.7,apt install mysql-server-5.7 必失败;唯一可行方案是手动安装 Ubuntu 18.04 兼容的 deb 包(如 5.7.40),严格按 mysql-common → libmysqlclient20 → mysql-community-server 顺序执行 dpkg -i,并提前安装 libaio1t64、libtinfo6 等依赖及软链。

Ubuntu 22.04 及更新版本(如 24.04)的官方仓库已彻底移除 mysql-server-5.7,直接 apt install mysql-server-5.7 必然失败——这不是配置问题,是包确实不存在。
为什么 apt install mysql-server-5.7 总是报 “E: Version ‘5.7.x’ for ‘mysql-server’ was not found”
Ubuntu 自 22.04 起将 MySQL 8.0 设为唯一默认版本,mysql-apt-config 工具在新系统上也已失效(安装后选 5.7 会静默回退到 8.0)。即使你手动添加旧源或下载 mysql-apt-config_0.8.12-1_all.deb,apt update 后 apt-cache policy mysql-server 仍只显示 8.0 的候选版本。
- 根本原因:MySQL 官方已停止为 Ubuntu 22.04+ 提供 APT 仓库支持,
repo.mysql.com/apt/ubuntu中无jammy(22.04)或noble(24.04)目录 - 验证方式:
curl -I https://repo.mysql.com/apt/ubuntu/dists/jammy/返回 404 - 替代方案只有两种:deb 包手动安装,或二进制 tar.gz 安装;前者更接近传统流程,后者更可控但需配环境变量
deb 包安装必须按顺序执行 dpkg -i,且依赖要提前装好
从 mysql-server_5.7.40-1ubuntu18.04_amd64.deb-bundle.tar 解压出的 deb 文件有严格依赖顺序,跳过或颠倒会导致 dpkg 报错“unresolved dependency”或后续包无法配置。
- 必须先装:
mysql-common_5.7.40-1ubuntu18.04_amd64.deb - 再装:
libmysqlclient20_5.7.40-1ubuntu18.04_amd64.deb(新版 Ubuntu 缺失该库,不装则mysql-community-server安装时卡在 postinst 阶段) - 最后装:
mysql-community-server_5.7.40-1ubuntu18.04_amd64.deb(这才是真正启动服务的包) - 依赖命令:
sudo apt install libaio1t64 libtinfo6 libncurses6 libmecab2 -y(Ubuntu 24.04 需用libaio1t64替代libaio1;libtinfo5已被libtinfo6取代,必须建软链:sudo ln -sf /usr/lib/x86_64-linux-gnu/libtinfo.so.6 /usr/lib/x86_64-linux-gnu/libtinfo.so.5)
安装后 mysql -u root -p 连不上?大概率是 auth plugin 不兼容
MySQL 5.7 在 Ubuntu 22.04+ 上首次启动时,默认仍可能使用 caching_sha2_password 插件(尤其当你之前装过 8.0),导致老客户端报错:Client does not support authentication protocol。
- 不要等安装完再改:在
mysql-community-server首次配置阶段(即第一次运行dpkg -i后触发的mysql_install_db)前,先创建临时配置文件防止插件写死 - 操作:
sudo mkdir -p /etc/mysql/conf.d,然后写入:[mysqld] default_authentication_plugin = mysql_native_password
- 若已安装失败,可跳过密码验证启动:
sudo mysqld_safe --skip-grant-tables &,再进mysql -u root执行:UPDATE mysql.user SET plugin='mysql_native_password' WHERE User='root'; FLUSH PRIVILEGES; - 注意:
mysql_secure_installation在此场景下不可用——它会强制重置插件为caching_sha2_password
远程连接失败?bind-address 不是唯一问题
bind-address = 0.0.0.0 只解决监听范围,Ubuntu 22.04+ 默认启用 ufw 防火墙,且 MySQL 5.7 的 user 表中 host 字段默认是 localhost,两者叠加导致 Navicat 或应用连不上。
- 防火墙放行:
sudo ufw allow 3306(别漏掉) - 授权语句必须显式指定 IP 或通配符:
CREATE USER 'root'@'%' IDENTIFIED BY 'your_pass'; GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;(不能只改现有root@localhost的host字段) - 修改完务必执行:
FLUSH PRIVILEGES;,否则权限不生效 - 验证是否监听正确:
ss -tlnp | grep :3306输出中应含*:3306,而非127.0.0.1:3306
最易被忽略的是:Ubuntu 22.04+ 的 systemd 对 mysql 服务单元做了路径锁定,手动安装的 5.7 二进制或 deb 包若未正确注册 service 文件,systemctl start mysql 会静默失败——得检查 /lib/systemd/system/mysql.service 是否指向你安装的 mysqld 路径,而不是残留的 8.0 位置。


















