Resolving WordPress ‘Error Establishing Database Connection’ After Server Migration

It appears that you’ve taken thorough steps to troubleshoot the “Error establishing database connection” issue after migrating your WordPress installation to a new server. Despite your efforts, the problem persists. Let’s explore some additional troubleshooting steps to resolve this matter:

1. Check Database Credentials:

Ensure that the database credentials (name, username, and password) entered during the WordPress installation match the details of the restored database. Additionally, confirm that the database user has the necessary privileges.

2. Verify Database Host:

Instead of using “localhost” or “127.0.0.1” as the host, try using the actual server’s IP address. Sometimes, this can resolve connection issues.

3. Confirm Database Connection:

Manually test the database connection from the server’s command line using the following command:

mysql -u your_username -p your_password -h your_host your_database 

Replace “your_username,” “your_password,” “your_host,” and “your_database” with your actual database credentials. This can help identify any connection problems.

4. Check Database Server Status:

Confirm that MariaDB is running and accessible. You can check the status with:

systemctl status mariadb

5. Review Server Logs:

Examine the Apache and MariaDB logs for any error messages. These logs, typically found in /var/log/apache2/ and /var/log/mysql/ or /var/log/mariadb/, may provide insights into the issue.

6. Adjust Database Host in wp-config.php:

Manually edit the wp-config.php file and set the database host to the server’s IP address. Update the following line:

define('DB_HOST', '127.0.0.1'); 

to

define('DB_HOST', 'your_server_ip');

7. Check Firewall Rules:

Confirm that the firewall (ufw) is allowing traffic on port 3306. You can check and update this using:

sudo ufw allow 3306

8. Temporary Disable Plugins:

Temporarily deactivate all WordPress plugins by renaming the plugins folder in the wp-content directory. This ensures that a faulty plugin is not causing the issue.

9. Temporary Switch to Default Theme:

Similarly, switch to a default WordPress theme (e.g., Twenty Twenty-One) to rule out any theme-related problems.

10. Recheck File Permissions:

Confirm that the files and directories in your WordPress installation have the correct permissions. You can use the following commands to set appropriate permissions:

find /var/www/html/ -type d -exec chmod 755 {} \;
find /var/www/html/ -type f -exec chmod 644 {} \;

After performing these steps, attempt to access your WordPress site again. If the issue persists, the logs and error messages gathered during these steps may provide more specific details on the root cause of the problem.

Was this helpful?

Thanks for your feedback!

Leave a Reply

Your email address will not be published. Required fields are marked *