Fedora Linux下无法启动MySQL服务的问题求助
无法启动MySQL服务的问题排查与解决
问题背景
学习Web开发时,尝试为Spring Boot应用搭建MySQL数据库,使用sudo dnf install mysql-community-server安装后,无法启动MySQL服务。
错误现象
执行以下命令后出现对应错误:
- 连接MySQL失败:
$ mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
- 查看版本正常:
$ mysql --version mysql Ver 8.0.31 for Linux on x86_64 (MySQL Community Server - GPL)
- 启动服务失败:
$ sudo service mysql start Redirecting to /bin/systemctl start mysql.service Failed to start mysql.service: Unit mysql.service not found. $ sudo systemctl start mysqld Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xeu mysqld.service" for details.
- 服务状态信息:
$ systemctl status mysqld.service × mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled) Active: failed (Result: exit-code) since Wed 2022-11-30 06:22:00 IST; 4s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 3783 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Process: 3818 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE) Main PID: 3818 (code=exited, status=1/FAILURE) Status: "Server shutdown complete" Error: 22 (Invalid argument) CPU: 1.043s Nov 30 06:21:59 fedora systemd[1]: Starting mysqld.service - MySQL Server... Nov 30 06:22:00 fedora systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE Nov 30 06:22:00 fedora systemd[1]: mysqld.service: Failed with result 'exit-code'. Nov 30 06:22:00 fedora systemd[1]: Failed to start mysqld.service - MySQL Server. Nov 30 06:22:00 fedora systemd[1]: mysqld.service: Consumed 1.043s CPU time.
配置文件信息
$ cat /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove the leading "# " to disable binary logging # Binary logging captures changes between backups and is enabled by # default. It's default setting is log_bin=binlog # disable_log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M # # Remove leading # to revert to previous value for default_authentication_plugin, # this will increase compatibility with older clients. For background, see: # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin # default-authentication-plugin=mysql_native_password datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/run/mysqld/mysqld.pid $ cat /etc/mysql/my.cnf cat: /etc/mysql/my.cnf: No such file or directory $ cat /var/lib/mysql/my.cnf cat: /var/lib/mysql/my.cnf: No such file or directory
补充日志信息
journalctl日志
$ journalctl -xeu mysqld.service ░░ Defined-By: systemd ░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel ░░ ░░ An ExecStart= process belonging to unit mysqld.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 1. Nov 30 06:22:00 fedora systemd[1]: mysqld.service: Failed with result 'exit-code'. ░░ Subject: Unit failed ░░ Defined-By: systemd ░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel ░░ ░░ The unit mysqld.service has entered the 'failed' state with result 'exit-code'. Nov 30 06:22:00 fedora systemd[1]: Failed to start mysqld.service - MySQL Server. ░░ Subject: A start job for unit mysqld.service has failed ░░ Defined-By: systemd ░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel ░░ ░░ A start job for unit mysqld.service has finished with a failure. ░░ ░░ The job identifier is 3531 and the job result is failed. Nov 30 06:22:00 fedora systemd[1]: mysqld.service: Consumed 1.043s CPU time. ░░ Subject: Resources consumed by unit runtime ░░ Defined-By: systemd ░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel ░░ ░░ The unit mysqld.service completed and consumed the indicated resources.
/var/log/mysqld.log日志
2022-11-30T00:44:42.089067Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.31) initializing of server in progress as process 1064 2022-11-30T00:44:42.091892Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting. 2022-11-30T00:44:42.091960Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it. 2022-11-30T00:44:42.092006Z 0 [ERROR] [MY-010119] [Server] Aborting 2022-11-30T00:44:42.092083Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.31) MySQL Community Server - GPL. 2022-11-30T00:44:42.331797Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.31) starting as process 1069 2022-11-30T00:44:42.339299Z 0 [Warning] [MY-010075] [Server] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 2d9a3bde-7048-11ed-a7cd-f406413544b8. 2022-11-30T00:44:42.342844Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory. 2022-11-30T00:44:42.342942Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed. 2022-11-30T00:44:42.342960Z 0 [ERROR] [MY-010119] [Server] Aborting 2022-11-30T00:44:42.343234Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.31) MySQL Community Server - GPL. 2022-11-30T00:49:37.543811Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.31) initializing of server in progress as process 3714 2022-11-30T00:49:37.544916Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting. 2022-11-30T00:49:37.544934Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it. 2022-11-30T00:49:37.545003Z 0 [ERROR] [MY-010119] [Server] Aborting 2022-11-30T00:49:37.545083Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.31) MySQL Community Server - GPL. 2022-11-30T00:49:37.768975Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.31) starting as process 3719 2022-11-30T00:49:37.773567Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory. 2022-11-30T00:49:37.773672Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed. 2022-11-30T00:49:37.773705Z 0 [ERROR] [MY-010119] [Server] Aborting 2022-11-30T00:49:37.774047Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.31) MySQL Community Server - GPL. 2022-11-30T00:52:00.113630Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.31) initializing of server in progress as process 3813 2022-11-30T00:52:00.114677Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting. 2022-11-30T00:52:00.114695Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it. 2022-11-30T00:52:00.114738Z 0 [ERROR] [MY-010119] [Server] Aborting 2022-11-30T00:52:00.114840Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.31) MySQL Community Server - GPL. 2022-11-30T00:52:00.358242Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.31) starting as process 3818 2022-11-30T00:52:00.362438Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory. 2022-11-30T00:52:00.362537Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed. 2022-11-30T00:52:00.362566Z 0 [ERROR] [MY-010119] [Server] Aborting 2022-11-30T00:52:00.362844Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.31) MySQL Community Server - GPL.
解决方案
从日志里的--initialize specified but the data directory has files in it和Failed to find valid data directory可以看出,问题出在MySQL数据目录/var/lib/mysql里有残留文件,导致初始化失败。按以下步骤操作:
- 先停止MySQL服务(如果处于错误启动状态):
sudo systemctl stop mysqld
- 备份数据目录里的文件(可选,防止有需要保留的数据):
sudo cp -r /var/lib/mysql /var/lib/mysql_backup
- 删除数据目录里的所有文件:
sudo rm -rf /var/lib/mysql/*
- 重新初始化MySQL:
sudo mysqld --initialize --user=mysql
- 启动MySQL服务:
sudo systemctl start mysqld
- 查看服务状态确认是否正常:
systemctl status mysqld
- 初始密码会在
/var/log/mysqld.log里,搜索temporary password获取:
sudo grep 'temporary password' /var/log/mysqld.log
用这个密码登录MySQL,然后修改密码:
mysql -uroot -p ALTER USER 'root'@'localhost' IDENTIFIED BY '你的新密码';
注意事项
- 确保操作时使用
sudo获取足够权限,避免权限不足导致的文件删除/初始化失败。 - 如果之前有重要数据,一定要先备份,避免数据丢失。
内容的提问来源于stack exchange,提问作者Prad
相关产品推荐
相关产品推荐

