欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[原创] CentOS7 MySQL 无法启动

程序员文章站 2022-04-11 19:36:50
...
环境说明:
CentOS Linux release 7.3.1611 (Core) 64位
mysql  Ver 14.14 Distrib 5.7.19

现象:
安装完 Wordpress 及 DB 后,在 Wordpress 中删除了默认的文章,之后就突然莫名其妙的提示无法连接DB。(之前也可能做过了一些其它 DB 操作及修改,有点儿记不清了)

原因调查:
首先发现的是,不知道为什么,MySQL 被停止了,并且提示如下错误:
# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since 五 2017-10-20 09:13:16 CST; 633ms ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 10382 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE)
  Process: 10365 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

10月 20 09:13:16 67-216-220-130 systemd[1]: Failed to start MySQL Server.
10月 20 09:13:16 67-216-220-130 systemd[1]: Unit mysqld.service entered failed state.
10月 20 09:13:16 67-216-220-130 systemd[1]: mysqld.service failed.
10月 20 09:13:16 67-216-220-130 systemd[1]: mysqld.service holdoff time over, scheduling restart.
10月 20 09:13:16 67-216-220-130 systemd[1]: start request repeated too quickly for mysqld.service
10月 20 09:13:16 67-216-220-130 systemd[1]: Failed to start MySQL Server.
10月 20 09:13:16 67-216-220-130 systemd[1]: Unit mysqld.service entered failed state.
10月 20 09:13:16 67-216-220-130 systemd[1]: mysqld.service failed.

之后查看 MySQL 日志:
# tail -n 100 /var/log/mysqld.log
......
2017-10-20T01:09:48.647161Z 0 [Note] InnoDB: Buffer pool(s) load completed at 171020  9:09:48
2017-10-20T01:09:48.647236Z 0 [Note] InnoDB: Starting shutdown...
2017-10-20T01:09:48.747574Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
2017-10-20T01:09:48.747891Z 0 [ERROR] InnoDB: Cannot open '/var/lib/mysql/ib_buffer_pool.incomplete' for writing: Permission denied
2017-10-20T01:09:50.258312Z 0 [Note] InnoDB: Shutdown completed; log sequence number 4076122
2017-10-20T01:09:50.258371Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
2017-10-20T01:09:50.258378Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
......

日志显示 '/var/lib/mysql/ib_buffer_pool.incomplete' 没有写权限。看来是权限的问题。于是执行如下命令,查看所有者及目前的权限:
# ls -ld /var/lib/mysql
drwxr-x--x 6 mysql mysql 4096 10月 20 09:17 /var/lib/mysql

看来所有者没有问题,那么尝试为其子目录和文件修改所有者:
# chown mysql:mysql -R /var/lib/mysql

之后,重新启动 MySQL 服务:
# systemctl restart mysqld

MySQL 可以正常启动了。

============================================================
若查看 MySQL Log 提示的错误像下面这样:
# tail -n 100 /var/log/mysqld.log
......
2017-10-20T02:58:09.562816Z 0 [Note] InnoDB: Initializing buffer pool, total size = 96M, instances = 1, chunk size = 96M
2017-10-20T02:58:09.562860Z 0 [ERROR] InnoDB: mmap(103071744 bytes) failed; errno 12
2017-10-20T02:58:09.562867Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2017-10-20T02:58:09.562872Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2017-10-20T02:58:09.562878Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2017-10-20T02:58:09.562882Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2017-10-20T02:58:09.562886Z 0 [ERROR] Failed to initialize plugins.
2017-10-20T02:58:09.562889Z 0 [ERROR] Aborting

2017-10-20T02:58:09.562907Z 0 [Note] Binlog end
......

说明 MySQL 启动时初始化的缓存大小太大了(也就是说可能是你的 VPS 内在太小了导致的)。默认的缓存大小是 128M,但是我已经改成了 96M 还是报错,说明还是设置的太大了(我的 VPS 只有 512M 内存,但是上面运行了好多的服务,因此可用内存不足)。

因此继续缩小缓存大小:
# vim /etc/my.cnf

修改如下值 innodb_buffer_pool_size = 64M(设置成合适的大小)

之后重新启动 MySQL 就可以了。