Error establishing a database connection

How to fix the Error establishing a database connection?

2 Replies

There can be several causes of this issue. To start, try checking the status of your database. You can do this with the following commands, depending on what you're running:

sudo systemctl status mysql
sudo systemctl status mariadb
sudo service mysql status

You can find more info on the usage of these commands here.

Oftentimes, simply restarting your database with a command like sudo systemctl restart mysql will bring it back up and resolve the database connection error. Other times, you'll need to do some more digging to get to the route of the problem and resolve it. Feel free to post any command outputs if you need help interpreting them.

Another option would be restoring from any backups you may have.

You might also be interested in signing up for Linode Managed service, which would enable you to set up monitors for your websites/Linode. These monitors would alert us of any issues, and we'd then do troubleshooting like above for you.

If this error is with a WordPress instance, and your database is running, then you can also use the following command to investigate using either SSH or LISH:

## On Debian/Ubuntu
for i in /etc/apache2/sites-available/*; do
    if [ ! "$(echo "${i}" | grep default)" ]; then
        sudo cat "$(awk '/DocumentRoot/{print $2}' "${i}")wp-config.php" | egrep 'DB_USER|DB_PASSWORD'
    fi
done

## On RedHat/CentOS
sudo cat "$(awk '/DocumentRoot/{print $2}' /etc/httpd/conf.d/vhost.conf)wp-config.php" | egrep 'DB_USER|DB_PASSWORD'

This should output your WordPress database's username/password settings so that you can confirm them:

define( 'DB_USER', 'wordpress' );
define( 'DB_PASSWORD', 'Sup3rS3cuReP@ssw0rd' );

If they are incorrect, then you can fix them by editing the file and restarting your database.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct