Sassyブログ

好きなことで暮らしを豊かにするブログ

"Error: connect ECONNREFUSED 127.0.0.1:5432" は突然に…

ある日突然運用中のアプリが開け無くなりました…

ブラウザのコンソールには以下の表示が…

Uncaught (in promise) TypeError: Failed to fetch

とりあえずサーバーにログインしてバックエンドのプロセスが起動しているかPSコマンドで確認

プロセス一覧に表示されていないのでサーバーが死んでいる…

アプリのjournalログを確認する

Error: connect ECONNREFUSED 127.0.0.1:5432

DB接続が拒否されているみたい

DBのログを見てみる

私の環境ではDBのログは以下に格納されている

/var/lib/pgsql/14/data

ではログを開いて確認してみます。

listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-11-05 05:57:14.641 UTC [3715] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-11-05 05:57:14.644 UTC [3717] LOG:  database system shutdown was interrupted; last known up at 2022-11-05 05:56:22 UTC
2022-11-05 05:57:14.656 UTC [3717] LOG:  database system was not properly shut down; automatic recovery in progress
2022-11-05 05:57:14.658 UTC [3717] LOG:  redo starts at 0/6800D878
2022-11-05 05:57:14.658 UTC [3717] LOG:  redo done at 0/6800D960 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2022-11-05 05:57:14.672 UTC [3717] PANIC:  could not write to file "pg_wal/xlogtemp.3717": No space left on device
2022-11-05 05:57:14.673 UTC [3715] LOG:  startup process (PID 3717) was terminated by signal 6: Aborted
2022-11-05 05:57:14.673 UTC [3715] LOG:  aborting startup due to startup process failure
2022-11-05 05:57:14.674 UTC [3715] LOG:  database system is shut down

気になる箇所としては以下

PANIC: could not write to file "pg_wal/xlogtemp.3717": No space left on device

どうやらデバイスに空き領域が無いようだ

dfコマンドを使用してディスクの空き領域を表示してみる

df -h

やはり/dev/xvda1の使用率が100%になっていた。 なので空き領域が無くトランザクションデータが書き込めずに落ちているようだった。

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        978M     0  978M   0% /dev
tmpfs           987M   28K  986M   1% /dev/shm
tmpfs           987M   33M  954M   4% /run
tmpfs           987M     0  987M   0% /sys/fs/cgroup
/dev/xvda1      8.0G  8.0G 1010M  100% /
tmpfs           198M     0  198M   0% /run/user/1000

私の環境ではAWSでサーバーを立てているのでEBSボリュームを変更します。

とりあえず16GBにしておきます。

EBSボリュームの変更はドキュメントに記載されておりますが、1度行うと6時間ほど感覚を空ける必要があるので設定するときはどのくらい増やすか検討してから行うのがおすすめです。

After modifying a volume, you must wait at least six hours and ensure that the volume is in the in-use or available state before you can modify the same volume. This is sometimes referred to as a cooldown period.

引用元:https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_ModifyVolume.html

EBSボリュームが反映されるまで少し待ちます。

反映後サーバーを再起動します。

再起動後にサーバーへログインしても、状態がおかしかったのか上手く起動されていなかったので念のためpostgresの再起動を行いました。

/usr/pgsql-14/bin/pg_ctl restart -D /var/lib/pgsql/14/data

そうするとコンソールに以下が表示されました!

pg_ctl: PID file "/var/lib/pgsql/14/data/postmaster.pid" does not exist
Is server running?
trying to start server anyway
waiting for server to start....2022-11-05 06:15:00.183 UTC [3604] LOG:  redirecting log output to logging collector process
2022-11-05 06:15:00.183 UTC [3604] HINT:  Future log output will appear in directory "log".
 done
server started

これでDBは起動出来たのでアプリケーションを再起動したところ無事起動出来ました!