Question

Error Connecting to Database on Wordpress?

Posted September 21, 2021 141 views
MySQLWordPress

Hi, I have a couple websites running Wordpress and they will regularly get Error Connecting to Database Errors. I’ve been rebooting the droplets, but it’s not an efficient way to handle it. How can I stop these from occurring? Thanks!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

Hi @justjon,

This seems like your MySQL service going down. In such situations, you can always check the error log of the service that is down, and rather than rebooting the droplet, restart the MySQL service.

So, first, as soon as you see the Error Connecting to Database, you can check your error_log. You should be able to find the error_log at the following location /var/log/mysql. There check the last lines of your log, you should see a message somewhere that contains [ERROR]. That should be the reason behind the failure of the service.

One possible reason might be your droplet not having enough memory. What you can try is adding SWAP to your Droplet or upgrading to see if that would resolve the issue.

Regards,
KFSys

Hello, @justjon

I will recommend you to check our article on How To Debug the WordPress Error Establishing Database Connection:

https://www.digitalocean.com/community/tutorials/how-to-debug-the-wordpress-error-establishing-database-connection

This crash is most likely due to your system running out of memory. I’d suggest that you add a swap file to give yourself a bit more of a buffer. Check out this tutorial:

How To Add Swap on Ubuntu

What you can do is to examine the logs for killed processes and also out of memory entries. To check this you can run the following commands:

  • grep -i kill /var/log/messages
  • grep -i `oom` /var/log/mesaages

You will most definitely need to upgrade your droplet as you’re running out of memory and your application/website needs more resources in order to continue to operate.

What you can also do is to use the MySQLTuner script.

The MySQLTuner is a script written in Perl and allows you to quickly test your MySQL configuration and it gives you suggestions for adjustments to increase performance and stability.

According to the official GitHub page, it supports 300 indicators for MySQL/MariaDB/Percona Server in this last version.

To run the script you could do the following:

  • SSH to your Droplet
  • Download the script:
  • wget http://mysqltuner.pl/ -O mysqltuner.pl
  • Then execute it:
  • perl mysqltuner.pl

The script would run multiple checks against your MySQL instance, all checks done by MySQLTuner are documented here.

Also as stated in the official documentation, it is still extremely important for you to fully understand each change you make to a MySQL database server. If you don’t understand portions of the script’s output, or if you don’t understand the recommendations, you should consult a knowledgeable DBA or system administrator that you trust.

As a good practice make sure to always test your changes on staging environments before implementing them on your production database.

On the same note, if you want to have worry-free MySQL hosting and focus on your application, I would recommend trying out the DigitalOcean Managed Databases:

https://www.digitalocean.com/products/managed-databases-mysql/

This was mini tutorial was posted from bobbyiliev in this question in our community: https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

You can also create a simple bash script to check if MySQL is running and if not to restart it.

#!/bin/bash

# Check if MySQL is running
sudo service mysql status > /dev/null 2>&1

# Restart the MySQL service if it's not running.
if [ $? != 0 ]; then
    sudo service mysql restart
fi

Run this script every 5 minutes using a cron job like this one:

 */5 * * * * /home/user/scripts/monitor.sh > /dev/null 2>&1

Hope that this helps!
Regards,
Alex

by Brian Boucheron
WordPress is one of the most popular open source content management systems in the world. Although it started out focused on blogging, over the years it has developed into a more flexible platform for websites in general. After almost fifteen years of development it is quite polished and robust, yet issues can still come up. If you've recently attempted to load your WordPress-powered website and instead saw a message stating "Error Establishing Database Connection", the cause is most often...