# DigitalOcean WordPress Database Connection Cannot Be Reestablished

- Link: https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/
- Published: 2020-04-21T09:18:20-07:00
- Author: rich

This happened again on one of my servers. The last time, it had to do with the lack
of a swap file and then running out of physical memory. Usually a reboot helps which
you do like this:

 1. [Rebooting a Digital Ocean](https://www.digitalocean.com/community/questions/how-to-reboot-droplet-through-the-command-line-restart-apache-server)
    image by ssh into the machine and then `sudo service apache2 restart` and if that
    doesn’t work reboot the droplet with `sudo shutdown now -r`
 2. [More in depth debugging](https://www.digitalocean.com/community/tutorials/how-to-debug-the-wordpress-error-establishing-database-connection).
    If you don’t mind having a server down or you were smart enough to have a load 
    balancer so you can run two servers at once (yes, that’s on my list), you can check
    things like the amount of memory available. But you use `sudo netstat -plt` to 
    see if mysql is running and restart with `sudo service mysql star`t. If that works,
    then you can grep the log looking for low memory `zgrep -a "allocate memory" /var/
    log/mysql/error.log*` and you can see what is going on. You can also use `free -
    h` to see how much memory you really have. In our case, a 1GB machine had 151MB
    available. Which really isn’t very much. so a 2GB machine is probably better.
 3. [Swapfile for safety](https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04).
    If you are running out of memory, they don’t recommend using a swapfile because
    it degrades the SSD, so it’s better to just upgrade the droplet. Personally, I 
    like the swapfile as a soft failure. But you can see if you have any with `sudo
    swap on --show` and if return nothing, then you don’t have it. The double check
    is to run `free -h` and see if Swap has anything. The do a `df -h` to see if you
    have disk space. In our case, of our 25GB SSD, we were 80% free so no problem there.
    Then you allocate a swapfile that is equal to your memory, so `sudo fallocate -
    l 1G /swapfile`, then make it read only and start it `sudo chmod 600 /swapfile &&
    sudo mkswap /swapfile && sudo mkswap on`. Finally, if this works `sudo free -h`
    should show it and make this permanent by adding a running `echo '/swapfile none
    swap sw 0 0' | sudo tee -a /etc/fstab` to add a line to the table.
 4. [Resize Droplet](https://www.digitalocean.com/docs/droplets/how-to/resize/). Of
    course, the better long term thing to do is to upgrade the droplet as WordPress
    in DigitalOcean really needs more that 1GB. For a website you use quite a bit, 
    you probably want more like a $10/month site with 2GB and 50GB of SSD.

### Share this:

 * [  Mastodon ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/?share=mastodon)
 * [  LinkedIn ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/?share=linkedin)
 * [  Facebook ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/?share=facebook)
 * [  WhatsApp ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/?share=jetpack-whatsapp)
 * [  Email ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/?subject=%5BShared%20Post%5D%20DigitalOcean%20Wordpress%20Database%20Connection%20Cannot%20Be%20Reestablished&body=https%3A%2F%2Ftongfamily.com%2F2020%2F04%2F21%2Fdigitalocean-wordpress-database-connection-cannot-be-reestablished%2F&share=email)
 * [More](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/#)

 * [  Telegram ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/?share=telegram)
 * [  Print ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/#print?share=print)
 * [  Pinterest ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/?share=pinterest)
 * [  Tumblr ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/?share=tumblr)
 * [  X ](https://tongfamily.com/2020/04/21/digitalocean-wordpress-database-connection-cannot-be-reestablished/?share=twitter)

### Like this:

Like Loading…
