WordPressで Error establishing a database connection エラーが出たときのメモ

ある日、自分のブログをブラウズしたら以下のエラーが表示された。

DigitalOceanでWordPressを運用するようになってから1年ほど経つが、この手のエラーに遭遇したのは初めてだった。結論から言うとmysqlが落ちており、これを再起動することで解決した。以下はその時のメモ。

エラー・メッセージによると、データベースとのやり取りに何らかの問題が発生している模様。
mysqlのエラー・ログ /var/log/mysql/error.log をチェックすると、以下のエラー・ログを吐き出したきりログが途切れていた。

# tail -n 20 error.log
2021-03-20T09:07:26.877685Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2021-03-20T09:07:26.877690Z 0 [Note] InnoDB: Uses event mutexes
2021-03-20T09:07:26.877694Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2021-03-20T09:07:26.877707Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2021-03-20T09:07:26.877711Z 0 [Note] InnoDB: Using Linux native AIO
2021-03-20T09:07:26.878005Z 0 [Note] InnoDB: Number of pools: 1
2021-03-20T09:07:26.878156Z 0 [Note] InnoDB: Using CPU crc32 instructions
2021-03-20T09:07:26.881775Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2021-03-20T09:07:26.881829Z 0 [ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12
2021-03-20T09:07:26.881836Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2021-03-20T09:07:26.881840Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2021-03-20T09:07:26.881848Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2021-03-20T09:07:26.881853Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2021-03-20T09:07:26.881857Z 0 [ERROR] Failed to initialize builtin plugins.
2021-03-20T09:07:26.881861Z 0 [ERROR] Aborting

2021-03-20T09:07:26.887475Z 0 [Note] Binlog end
2021-03-20T09:07:26.887543Z 0 [Note] Shutting down plugin 'CSV'
2021-03-20T09:07:26.887817Z 0 [Note] /usr/sbin/mysqld: Shutdown complete

どうやら、mysqlがメモリを確保できずシャットダウンした模様。サービス状況を確認してみたところ、確かにmysqlが落ちていた。

# ps aux | grep -i mysql | grep -v grep
#
#
# netstat -plt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      936/sshd
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN      1234/master
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN      701/systemd-resolve
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      936/sshd
tcp6       0      0 ip6-localhost:smtp      [::]:*                  LISTEN      1234/master
tcp6       0      0 [::]:https              [::]:*                  LISTEN      347/apache2
tcp6       0      0 [::]:http               [::]:*                  LISTEN      347/apache2

以下のコマンドでmysqlを再起動したことろ、再びブログをブラウズできるようになった。

(sudo) systemctl start mysql

今後エラーが頻発するようであれば、mysql再起動用のスクリプトを書くなり、メモリの割当を見直すなりする必要があるかもしれない。
今回はmysqlが落ちていたことがエラーの原因だったが、データベースのログイン認証に不備があっても同様のエラーが出る模様。

その後も度々mysqlが落ちるので、暫定処置として以下のmysql監視スクリプトをcron化して/etc/cron.d/に登録した。

#!/bin/bash

## script to monitor and restart mysql

error_log="/var/log/mysql/restart.log"

ps aux | grep '/usr/sbin/mysqld' | grep -v grep

if [ $? -ne 0 ]; then
	systemctl start mysql >/dev/null 2>&1
	echo "mysql restarted at $(date)" >> $error_log.$(date '+%Y-%m-%d-%H:%M:%S')
fi

Leave a Reply

Your email address will not be published. Required fields are marked *