Question

Error establishing a database connection - But nothing seems wrong? (following DigitalOcean tutorial)

Posted October 21, 2020 700 views
MySQLWordPressDatabases

Hi!

For the past days I’ve been getting the “Error establishing a database connection” message again on my Website (https://www.unidaystudio.com.br/) and trying to fix based on the answers here, google search and mainly this related DigitalOcean tutorial:

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

But I haven’t succedded in any of them.

I’ll explain the outputs I had following this tutorial and I think it will be helpful to understand what is going on. Also, I don’t know if that helps, but here is also a console output when I try to connect to the website:
http://prntscr.com/v3tdua

Step 1 — Check the Server Memory Resources

sudo netstat -plt

Mysql Server is running:
http://prntscr.com/v3t43u

zgrep -a "allocate memory" /var/log/mysql/error.log*

Outputs nothing. Here is an extra htop screenshot of my server:
http://prntscr.com/v3t58o
RAM Usage is 410M/986M.

Step 2 — Check Database Login Credentials

Checked and they’re correct: user, db name and password. Here is a screenshot:
http://prntscr.com/v3t7r5

Step 3 — Repairing the WordPress Database

I added the following code to wp-config.php:

define('WP_ALLOW_REPAIR', true);

Saved, uploaded and closed, then opened the .../wp-admin/maint/repair.php URL and got the exact same error:
http://prntscr.com/v3t9ux

Extra Steps I tried:

  • Disable all the plugins by renaming the /var/www/html/wp-content/plugins/ folder.
  • Reinstall mysql using apt-get
  • Restart mysql using: sudo service mysql stop; sudo service mysql start; and also using: systemctl restart mysql

So I’m basically out of ideas because every topic points to the same thing that I’ve already tried and failed.

Help?

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

Hello,

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 18.04

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/

Source:
https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

Hope that this helps!
Regards,
Alex

  • I have same issue as op. I was trying to activate a wordpress plugin called LearnPress and the issue happened, after rebooting the droplet I came back to the website and asked me to configure the plugin.

    The other time it happened was when trying to activate WooCommerce, you said to op “This crash is most likely due to your system running out of memory” and since I’m on the most basic plan I was wondering, would switching to a plan with more resources prevent that kind of thing from happening?

To Fix “Error Establishing a Database Connection”
Step 1: Get In Touch With Your Web Host Provider.
Step 2: Check If Your Plugin or Theme Files Haven’t Been Corrupted.
Step 3: Check If Your Database Hasn’t Been Corrupted.
Step 4: Check Your Database Connection Credentials.
Step 5: Restore The Default WordPress Files.