当前位置:   article > 正文

解决Ubuntu20.04启动MySQL报错(Job for mysql.service failed because the control process exited with error)

job for mysql.service failed because the control process exited with error c

运行环境:ubuntu20.04,mysql 8.0

mysql Ver 8.0.29-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))

授人以鱼不如授人以渔:具体错误原因请到日志文件/var/log/mysql/error.log中排查(注意要放开日志文件的写入权限)

mysql启动时报错情况

sudo service mysql start # 启动
  • 1

Job for mysql.service failed because the control process exited with error code.
See “systemctl status mysql.service” and “journalctl -xe” for details.

sudo service mysql status # 查看状态;建议加上sudo,会有更详细的信息
  • 1

● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2024-02-28 10:10:31 CST; 10min ago
Process: 546126 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=1/FAILURE)

0.Ubuntu下的mysql相关文件介绍

(注意Centos等其他linux系统下的mysql相关文件路径与Ubuntu不一样)

配置文件:

/etc/mysql/mysql.conf.d/mysqld.cnf # 其中log_error对应的路径是ubuntu中mysql的日志存放位置(log_error = /var/log/mysql/error.log)
/etc/mysql/my.cnf # 启动配置
  • 1
  • 2

日志文件

/var/log/mysql/error.log # 由/etc/mysql/mysql.conf.d/mysqld.cnf中的log_error设置
  • 1

数据文件:

/var/lib/mysql # 注意:/var/lib/mysql千万不要删!这是存放mysql数据文件的目录
  • 1

运行时文件:

/var/run/mysqld
/tmp/data
  • 1
  • 2

1.修改文件权限

# 可能需要创建/tmp/data、/var/run/mysqld【ubuntu18升级到20,需要增加/var/run/mysqld,并且需要sudo dpkg --configure -a】
sudo mkdir -p /var/run/mysqld /tmp/data
sudo chown -R mysql:mysql /var/lib/mysql /var/log/mysql /var/run/mysqld /tmp/data
sudo chmod -R 777 /var/lib/mysql /var/log/mysql /var/run/mysqld /tmp/data
  • 1
  • 2
  • 3
  • 4

2.测试(可选)

mysqld_safe测试启动服务端(无需用户名和密码)

# 启动;并写入日志(可查看日志报错信息)
mysqld_safe --defaults-file=/etc/mysql/my.cnf
# /etc/mysql/my.cnf是ubuntu下的mysql启动参数配置文件
  • 1
  • 2
  • 3

注意,测试通过后,要关闭测试进程,否则报错。

客户端连接mysql服务端,mysql -u root -p

可能出现客户端报错:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

tail -n50 /var/log/mysql/error.log找到日志报错:

[ERROR] InnoDB: Unable to lock ./ibdata1, error: 11, Could not open or create the system tablespace.

原因:之前启动的mysqld_safe进程占用

解决:找到其他占用端口的进程,kill

ps -ef | grep mysql
  • 1

user0 618596 617876 0 13:17 pts/22 00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/mysql/my.cnf
user0 618942 618596 0 13:17 pts/22 00:00:05 /usr/sbin/mysqld --defaults-file=/etc/mysql/my.cnf --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306

kill -9 618596 618942
  • 1

3.重启mysql

重启mysql服务端:

sudo service mysql restart
  • 1

sudo service mysql status查看正确的启动状态,示例如下:

● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2024-02-28 13:57:17 CST; 16min ago
Process: 623015 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 623049 (mysqld)
Status: “Server is operational”
Tasks: 38 (limit: 347383)
Memory: 369.1M
CGroup: /system.slice/mysql.service
└─623049 /usr/sbin/mysqld

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/456023
推荐阅读
相关标签
  

闽ICP备14008679号