How to fix the ‘Error establishing a database connection’ issue in WordPress on a Linux server?

Solution: 1

Check your wp-config.php file to ensure the database details are correct. It should look something like this:


// ** MySQL settings - You can get this info from your web host ** //

// Database name
define('DB_NAME', 'wp');

// MySQL database username
define('DB_USER', 'root');

// MySQL database password
define('DB_PASSWORD', '');

// MySQL hostname
define('DB_HOST', 'localhost');

Make sure the values (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST) match your actual database credentials.

Solution: 2

Check the PHP-FPM error logs in:

/var/log/php-fpm/error.log

In my case, when I ran:

/etc/init.d/mysqld status

I got the message:

mysqld dead but subsys locked

It turned out that MySQL was stopped! I’m not sure how it happened since I hadn’t changed anything.

The fix was simply to start MySQL again:

/etc/init.d/mysqld start