The error "Error establishing a database connection" might occur for the following reasons:
- There are corrupted database tables.
-The remote database connection is disabled. -The database service is down. -There is insufficient space on your volume. -There are incorrect login credentials in your WordPress Configuration file
To fix this follow the steps below
- access the wp-config.php file using a text editor, such as the vi editor through this command
$ sudo vi wp-config.php
- Add the following line in your wp-config.php file. Make sure to add the line immediately before the line "That’s all, stop editing! Happy blogging".
- After adding the preceding setting to the file, access the following URL and then run Repair Database:
/wp-admin/maint/repair.php (for example, example.com/wp-admin/maint/repair.php)
- After running the database repair, remove the line of code you added to your wp-config.php file. If you don't remove this line, then anyone can run the repair on your database.
Disabled remote database connection
Sometimes databases reside on a remote database server. If the database server doesn't allow remote connections from the instance hosting the website, then you can't connect to the database. To troubleshoot this, do the following:
- Check the configuration file wp-config.php for the DB_HOST value. If the host isn't localhost or 127.0.0.1, then the database resides in a remote server, as shown in the following example:
- Try to telnet from the server to the remote server on port 3306. If you can't connect, then remote connections aren't allowed in the database configuration on the remote server. Or, there's a firewall on the remote server that's blocking the connection. Contact the external database owner or support for assistance allowing connections from your Lightsail instance.
It's a best practice to store the website database in a Lightsail managed database for high availability and security.
Database service is down
Note: The following file paths and commands might change depending on whether your Lightsail WordPress instance uses MySQL or MariaDB. Also, the file paths and commands vary depending on whether the instance uses native Linux system packages (Approach A), or if it's a self-contained installation (Approach B). To identify the database server type and which approach to follow, run the following commands:
test ! -f "/opt/bitnami/common/bin/openssl" && echo "Approach A" || echo "Approach B"
test -d /opt/bitnami/mariadb && echo "MariaDB" || echo "MySQL"
- If you verified that there are no table corruption and no remote database connection issues, and WordPress still can't connect to the database, then your database server might be down. This might happen due to database configuration issues, heavy traffic on a server, low disk space, low available memory, and so on. Check the database service status using the following command:
MySQL database server
sudo /opt/bitnami/ctlscript.sh status mysql
MariaDB database server
sudo /opt/bitnami/ctlscript.sh status mariadb
- If the preceding command shows that database is in the stopped state, then try starting the database service using the following command:
MySQL database server
sudo /opt/bitnami/ctlscript.sh start mysql
MariaDB database server
sudo /opt/bitnami/ctlscript.sh start mariadb
- If you're still not able to start the database service and you're seeing errors during the start process, then check the database service logs to identify the root cause and troubleshoot the issue. The main database service log file is located at one of the following locations in your Lightsail WordPress Instance:
MySQL database server following Approach A: /opt/bitnami/mysql/logs/mysqld.log
MySQL database server following Approach B: /opt/bitnami/mysql/data/mysqld.log
MariaDB database server following Approach A: /opt/bitnami/mariadb/logs/mysqld.log
MariaDB database server following Approach B: /opt/bitnami/mariadb/data/mysqld.log
-Database performance and connectivity can be affected by low disk space and/or low available memory. Check these resources using the df and free commands. Insufficient space on your volume
If the free disk space on your volume is 100% or just below 100%, then the database service might go down.
- Run the following command:
$ sudo df -h
The preceding command lists the amount of free disk space, as shown in the following example:
Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 400K 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/nvme0n1p1 8.0G 8.0G 0G 100% / tmpfs 389M 0 389M 0% /run/user/1000
If the command output shows that you don't have enough available space, you can resize your instance to bigger size. Or, you can delete unnecessary files from the server to create free space.
After increasing the free disk space, restart the database service.
Incorrect login credentials in your WordPress settings
WordPress needs a specific database connection string, which includes a user name, password, and host to access the database. If any of those items have changed, then WordPress can't access the database.
To verify you're using the correct connection string, get the connection string details DB_NAME, DB_HOST, DB_USER and DB_PASSWORD in the wp-config.php file.
Access the database from a terminal using the connection string. Make sure to replace DB_NAME, DB_HOST AND DB_USER with the values you got in step 1.
sudo mysql 'DB_NAME' -h 'DB_HOST' -u 'DB_USER' -p Enter password: ********
Note: The password isn't displayed as you enter it so that it won't be visible to other users.
- Press the ENTER key after entering the password.
If you're getting an Access Denied error when using the preceding command, then it usually means that the credentials are incorrect.
If you're using a remote DB host, then add the correct connection string in the wp-config.php file. If the database is in the same server, then make sure that DB_NAME is bitnami_wordpress and DB_USER is bn_wordpress.
To reset the database user password, do the following:
- Use the following command to access /home/bitnami/bitnami_credentials. Make a note of the root database password.
sudo cat /home/bitnami/bitnami_credentials
- Log in to MySQL/MariaDB shell using the following command, then enter the password you got from the preceding command. If you're not able to log in to shell using the database root password, then reset the password (MySQL or MariaDB)
sudo mysql -u root -p Enter password: ********
- Inside the MySQL or MariaDB shell, run the following query to make sure that database bitnami_wordpress exists:
- Run the following query to make sure that the database user bn_wordpress exists:
SELECT user FROM mysql.user;
- Reset the password of the database user "bn_wordpress" using the following query.
Note: Replace PASSWORD with the password you got from the wp-config.php file.
alter user 'bn_wordpress'@'localhost' identified by 'PASSWORD‘; alter user 'bn_wordpress'@'127.0.0.1' identified by 'PASSWORD‘;
alter user 'bn_wordpress'@'%' identified by 'PASSWORD‘;
Note: If none of the preceding resolutions work, you can restore your instance using a backup snapshot.
Why VPC with a "public subnet only and AWS Site-to-Site VPN access" cannot be configured?Accepted Answerasked 18 days ago
If only one of the tunnels of site to site vpn is down, is communication possible?Accepted Answerasked 15 days ago
Error establishing a database connection with RDS and OpenLiteSpeed AIMsasked 6 months ago
Site to Site VPN with Private and Public - Mikrotikasked a year ago
Struggling with Site to Site VPN using CiscoASAv and Public Encryption Domainsasked 7 months ago
AWS Lighthouse - site went down all of a sudden and now getting an Error establishing a database connectionasked 2 months ago
AWS site-to-site VPN tunnelsAccepted Answerasked 3 years ago
Site-to-Site - Status pendingasked a month ago
Conflict between AWS site-to-site VPN (to a VPC) and non-AWS client VPNasked 3 years ago
AWS Site-to-Site VPN authentication failing for Customer Gateway behind NAT deviceasked 2 months ago