Upgrade to nodejs 18 Error: connect ECONNREFUSED ::1:3306 Ghost + Ubuntu

In this tutorial, we'll see how to solve a Ghost errors:

  • Message: connect ECONNREFUSED ::1:3306
  • Bad gateway Error code 502

We got this error from Ghost session when we do update to Node.js v18.X.X. The problen is caused by node.js 18 which breaks the MySQL connection due to IPV6.

Error: connect ECONNREFUSED ::1:3306

What happened to get the error:

Error: connect ECONNREFUSED ::1:3306

First I updated the Linux packages by:

apt-get update
apt-get upgrade

Next I've updated the node.js by commands from Upgrade Node.js

sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

and

NODE_MAJOR=18 # Use a supported version
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

finally:

sudo apt-get update
sudo apt-get install nodejs -y

This resulted the Ghost to crash during start and failing to update with errors - Error: connect ECONNREFUSED ::1:3306

Fix

The solution for error node.js Error: connect ECONNREFUSED ::1:3306 was to edit file: /etc/hosts:

sudo nano /etc/hosts

remove line: ::1

The file will become:

127.0.1.1 ubuntu-8gb-ash-1 ubuntu-8gb-ash-1
127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
#::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Now Ghost can be upgraded again by:

ghost update --force

Resources

You can find more here: