The possible causes of “Error establishing a database connection” are usually:
- Incorrect hostname, username, or password
- MySQL server stopped
- Correct connection details but the tables are empty
- Corrupted tables
Incorrect connection details
We will open our wp-config.php file and look for the access credentials being used in wp-config.php:
define('DB_NAME', 'wordpress_9');
define('DB_USER', 'wordpress_0');
define('DB_PASSWORD', 'G0D5L_2anl');
define('DB_HOST', 'localhost:3306');Now we will connect via SSH using the details in the wp-config.php file:
mysql -uwordpress_0 -pG0D5L_2anl -hlocalhost -P3306 wordpress_9After entering the password, we should get something like:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1657
Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MariaDB [wordpress_9]>MySQL server stopped
If, when attempting the previous step, we get an error indicating that MySQL is not running or is stopped, we will connect via SSH and run:
service mysqld startor
service mysql startEmpty tables
If the connection details are correct and we are still getting the error: Error establishing a database connection. We need to check that our tables are not empty.
Corrupted tables
To fix the problem of corrupted tables, we will edit the wp-config.php file by adding:
define('WP_ALLOW_REPAIR', true);Next, we will go to www.midominio.com/wp-admin/maint/repair.php and a wizard like this will appear:
Once we have repaired the tables, we must remove the line “define(‘WP_ALLOW_REPAIR’, true);” from the wp-config.php file