Error establishing a database connection wordpress

Posted March 24, 2017 6.6k views
WordPressUbuntu 16.04

Error establishing a database connection wordpress. Is there any proper solution? I have tried many things but after 2 to 4 days same error occurs.

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
6 answers

most probably your out of resources and your mysql is crashing for that.
switch to a higher plan if you are on 5$ one or add some swap space

by Justin Ellingwood
One of the easiest way of increasing the responsiveness of your server and guarding against out of memory errors in your applications is to add some swap space. In this guide, we will cover how to add a swap file to an Ubuntu 16.04 server. <$>[warning] [label...

@jgojariya @newbie

Although swap is generally recommended for systems utilizing traditional spinning hard drives, using swap with SSDs can cause issues with hardware degradation over time. Due to this consideration, we do not recommend enabling swap on DigitalOcean or any other provider that utilizes SSD storage. Doing so can impact the reliability of the underlying hardware for you and your neighbors. This guide is provided as reference for users who may have spinning disk systems elsewhere.

If you’re on a 512MB Droplet and you’re seeing MySQL crash, I would recommend upgrading to a 1GB or 2GB Droplet. Running Apache/NGINX, MySQL/MariaDB, and PHP on a 512MB Droplet requires a great deal of optimization and is really only meant for very low traffic scenarios.

Thank you. I have already 1gb/30gb server. This error never occurs in shared hosting.

  • @jgojariya @jtittle

    When this happens, can you run tail -50 /var/log/mysql/error.log before restarting MySQL and post it here - this will tell us what happened before the crash.

    Shared hosting is different from DigitalOcean, since shared hosting doesn’t require anything from you. You just add your files and run, you have no administrative/configuration rights to the database or web server - someone else takes care of that.
    On DigitalOcean the entire hosting and maintenance part lands on you, but it also give you the freedom to do whatever you want, since you control the entire server.
    Normally I wouldn’t recommend DigitalOcean for hosting a regular WordPress website, since there is a lot of configuration and maintenance, which you don’t have on a simple shared hosting service.

    • Thank you very much.
      Now there is no result for tail -50 /var/log/mysql/error.log, because it is working now. I’ll paste response when it comes.

  • @jgojariya

    Shared Hosting is an entirely different ballgame :-).

    Some providers use CloudLinux + cPanel which allows them to limit resources on a granular and often problematic level (the case when providers are too strict). Other providers just use cPanel and allow a little more freely without limitations that are often imposed when using CloudLinux.

    When it comes to Shared Hosting, the latter often doesn’t count overall resource usage that is a result of Apache, MySQL, etc. They look at disk usage, disk I/O, overall CPU time, RAM, etc. It’s a little more hands on. With CloudLinux, you essentially hit a brick wall and your site slows down to a crawl, and if that doesn’t work, your site just errors out (last resort last I recall).

    That being said, shared hosting servers are/should be more powerful (see last comment) that a 1GB Droplet as they are hosting numerous clients to the tune of hundreds or thousands per server. They have more free CPU and RAM to play with, so if you use a little extra for a few minutes or hours, generally, you’re not going to cause a stir.

    When it comes to your Droplet, what they shared hosting provider would normally handle for you, such as server optimization, server security, ect – that’s all on you now.

    Default configurations rarely work well for production, so installing a LAMP Stack and expecting it to work 100% of the time, issue-free, isn’t going to happen. It’d be nice if it did – no doubt – but there’s far too many scenarios for generalized configuration setting to account for. What works on a 1GB Droplet will probably cause a 16GB Droplet to crawl as it’d be too tight with resources.

    On the other hand, what works on a 16GB Droplet would crash a 1GB Droplet in a matter of seconds or minutes as the resource allocations would be far too much for just 1GB of RAM.

    When it comes to services crashing, there’s really only two things you can do. You can throw hardware at it (upgrade your Droplet), or work on optimizing it the best you can. In some cases, throwing hardware at it is going to be the only way, especially as your site grows. Other times, optimizing, tweaking, tuning, testing – rinse and repeat – is the only way to keep your costs down and prevent $10 turning in to $20 or $20 in to $40, etc.

    The last few servers I setup for a few private web hosting providers that I’ve worked with in the past were huge. I wanted to give you an idea of what kind of setups some providers deploy and test with, which is why you may sometimes has a little more freedom when it comes to resources on shared hosting.

    The first server was a:

    • Dual Intel Xeon CPU (12 Cores) w/HT (48 total Cores)
    • 128GB RAM
    • 16x SSD in RAID-10
    • Dual Redundant 10Gbps Uplinks (Public/Private)

    That’s not too bad, but the next one was massive.

    • Quad Intel Xeon CPU (8 Cores/CPU) w/HT (64 total Cores)
    • 1TB RAM
    • 24x SSD in RAID-10 + Backup
    • Dual Redunant 10Gbps Uplinks (Public/Private)

    The last server probably isn’t one you’re going to see at most providers as that’s one huge server and it’s also expensive, thus not one that you’re run of the mill $3-$10 provider is going to use for clients or even VPS.

    • Thank you very much.

      • @jgojariya

        No problem at all :-). What I would do is test upgrading to a 2GB Droplet and keep an eye on RAM using top. Once you run top if you hit:

        SHIFT + M

        That’ll sort your processes and show human-readable resource usage in the process listing. There you can see about how much each process is using an can use. Looking at the VIRT column will tell you how much the process could use, while the column beside it (RES) is actual usage as of right now.

        MySQL will probably be the larger of the two as far as single-process usage, though Apache can use more when you combine all the processes together.

You can use this script to restart your server whenever there is database connection error.

Hello, @avdi

Have you checked the MySQL error_log in order to see if the server is running out of memory or if the issue is with to many MySQL connections? As per the tutorial you can run this command in order to check if the server have recently run out of memory:

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

zgrep will search through log files, including older log files that have been archived as compressed .tar.gz files. We’re searching for lines that contain allocate memory, in any error.log* file in the /var/log/mysql/ directory.

You can always login and check the MySQL error_log when the issue occurs using tail to either check the last 100 entries in the log or monitor the log in real time:

tail -n 100 /var/log/mysql/error.log

and monitor it in real time:

tail -f /var/log/mysql/error.log

Hope that this helps!

Hello, all

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

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 -O
  • Then execute it:

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 a worry-free MySQL hosting and focus on your application, I would recommend trying out the DigitalOcean Managed Databases:

This was mini tutorial was posted from bobbyiliev in this question in our community:

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


# 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

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

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

Hope that this helps!