jgojariya
By:
jgojariya

Error establishing a database connection wordpress

March 24, 2017 2.6k views
WordPress Ubuntu 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.

4 Answers

@jgojariya
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
https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04

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
        
        c
        
        e
        

        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.
https://github.com/Gurnoor30196/Launch-Mysql

Have another answer? Share your knowledge.