Ubuntu18.04升级20.04后MySQL服务无法启动求助
将Ubuntu从18.04升级至20.04(同步将MySQL从5.7升级到8)后,我遇到了MySQL无法启动的问题。尝试启动mysql服务时,进程卡在“Starting MySQL Community Server...”阶段。
执行systemctl status mysql.service得到以下输出:
USER@SERVER:~$ systemctl status mysql.service ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: activating (start) since Wed 2020-08-19 11:38:13 CEST; 576ms ago Process: 18006 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 18014 (mysqld) Status: "Server startup in progress" Tasks: 13 (limit: 1110) Memory: 261.6M CGroup: /system.slice/mysql.service └─18014 /usr/sbin/mysqld Aug 19 11:38:13 SERVER systemd[1]: Starting MySQL Community Server...
执行journalctl -xe得到以下日志信息:
USER@SERVER:~$ journalctl -xe -- -- The job identifier is 24060. Aug 19 11:38:17 boris-dev systemd[1]: mysql.service: Main process exited, code=exited, status=1/FAILURE -- Subject: Unit process exited -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- An ExecStart= process belonging to unit mysql.service has exited. -- -- The process' exit code is 'exited' and its exit status is 1. Aug 19 11:38:17 boris-dev systemd[1]: mysql.service: Failed with result 'exit-code'. -- Subject: Unit failed -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- The unit mysql.service has entered the 'failed' state with result 'exit-code'. Aug 19 11:38:17 boris-dev systemd[1]: Failed to start MySQL Community Server. -- Subject: A start job for unit mysql.service has failed -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- A start job for unit mysql.service has finished with a failure. -- -- The job identifier is 24060 and the job result is failed. Aug 19 11:38:17 boris-dev systemd[1]: mysql.service: Scheduled restart job, restart counter is at 139. -- Subject: Automatic restarting of a unit has been scheduled -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- Automatic restarting of the unit mysql.service has been scheduled, as the result for -- the configured Restart= setting for the unit. Aug 19 11:38:17 boris-dev systemd[1]: Stopped MySQL Community Server. -- Subject: A stop job for unit mysql.service has finished -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- A stop job for unit mysql.service has finished. -- -- The job identifier is 24126 and the job result is done. Aug 19 11:38:17 boris-dev systemd[1]: Starting MySQL Community Server... -- Subject: A start job for unit mysql.service has begun execution -- Defined-By: systemd -- Support: http://www.ubuntu.com/support -- -- A start job for unit mysql.service has begun execution. -- -- The job identifier is 24126.
服务器上有几个数据库,我希望能保留它们,但并非必须。请问有什么最佳解决方案?感谢您的帮助!
先别急,升级MySQL 5.7到8.0时常见的启动问题大多和系统表兼容性、配置文件冲突或者权限问题有关,咱们一步步来排查:
第一步:查看MySQL详细错误日志(最关键)
systemd和journalctl的日志不够细化,直接看MySQL自身的错误日志,一般路径是/var/log/mysql/error.log,执行命令:sudo tail -n 50 /var/log/mysql/error.log这里会明确告诉你故障原因——比如系统表需要升级、旧配置项不兼容,或是数据目录权限异常。
第二步:手动执行MySQL升级脚本
如果日志提示系统表需要升级,先停止自动重启的服务避免干扰:sudo systemctl stop mysql sudo systemctl disable mysql # 临时禁用自动重启然后手动运行升级命令:
sudo mysqld --upgrade=FORCE执行完成后重新启用并启动服务:
sudo systemctl enable mysql sudo systemctl start mysql第三步:检查配置文件兼容性
MySQL 8.0移除了query_cache_size、query_cache_type等旧配置项,如果你的/etc/mysql/my.cnf或/etc/mysql/mysql.conf.d/mysqld.cnf里还保留着这些,会导致启动失败。
先备份原配置:sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.bak然后注释掉所有报错的旧配置项,再尝试启动服务。
第四步:修复数据目录权限
升级过程中可能会改变MySQL数据目录的权限,默认数据目录是/var/lib/mysql,先检查权限:ls -ld /var/lib/mysql正常情况下目录应该属于
mysql:mysql用户组,否则执行权限修复命令:sudo chown -R mysql:mysql /var/lib/mysql sudo chmod 750 /var/lib/mysql之后再启动服务。
第五步:重置MySQL(保留数据)
如果上面的方法都无效,又想保留数据库,可以先备份数据再重置:# 备份数据目录 sudo cp -r /var/lib/mysql /var/lib/mysql_backup # 卸载现有MySQL组件 sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-* sudo rm -rf /etc/mysql /var/lib/mysql # 重新安装MySQL 8.0 sudo apt update sudo apt install mysql-server # 恢复自定义数据库(跳过系统自带的mysql、sys等库) sudo cp -r /var/lib/mysql_backup/[你的数据库名] /var/lib/mysql/ sudo chown -R mysql:mysql /var/lib/mysql # 启动后运行升级脚本 sudo mysql_upgrade -u root -p要是不需要保留数据,直接卸载重装即可,步骤会更简单。
内容的提问来源于stack exchange,提问作者Bo vl

