Error establishing a database connection

Posted April 22, 2015 6.4k views
UbuntuMySQLApachePHPServer OptimizationWordPress

Hi. I’m using 512 mb droplet with with Ubuntu 14.04 x64 and Sentora panel (installed on a fresh Ubuntu droplet). Then I followed these tutorials before I moved my websites on DO: Initial Server Setup with Ubuntu 14.04 and Additional Recommended Steps for New Ubuntu 14.04 Servers. My swap file is 1 GB.

I’m using this doplet for almost 2 months. I have 2 small Wordpress websites. The main one have around 200 visitors a day, the second one have around 60 visitors a day. Since the droplet’s start I’m experiencing Database Errors - Error establishing a database connection. Here is the error I have when I visit wp-admin.php:

I have to login via Putty nad type: sudo service mysql start. At the beginning these errors happened maybe 2-3 times a week. But currently I’m experiencing this problem 2-3 times a day. Maybe more, because I don’t know if there is a MySQL auto restart in Sentora panel. As far as I know, 512 MB droplet with 1 GB swap file should be enough for the traffic I have. At first I thought, maybe it happens when there are many people at the same time. So I opened live view on my Google Analytics page and noticed that in many cases when the error occurs, I had max 3 visitors at the same time. I had moments when there are 10-15 people at the same time surfing through my website’s posts and there are no errors.

I have no idea how to solve this problem. Can anyone help me?

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

Same problem with 1gb RAM.... I don’t understand why :(

  • Hi, chthomas91.
    I’ve managed to solve my problem. If you have Swap file set up, all you need to do is to change some mpm_prefork values. Here are mine, and it works like a charm now:

    StartServers          2
    MinSpareServers       2
    MaxSpareServers       3
    MaxClients           10
    MaxRequestsPerChild 3000

    Mpmprefork values should be located in /etc/apache2/apache2.conf file or /etc/apache2/mods-enabled/mpmprefork.conf file. If you don’t have these values in apache2 file and you don’t have mpmprefork.conf file, then you have to install mpmprefork module and restart apache.

    I also made some changes to my MySQL databases. I had InnoDB engine set in all of my databases, but all tables inside were in MyISAM. I also had some errors in tables when I checked mysql logs. So I updated MySQL to the latest version. Then I opened PhpMyAdmin, repaired all tables in all databases, then I optimazed them and switched all tables from MyISAM to InnoDB. Now, all my tables using one engine, not two. Remember to perform database backup before you start to mess with it. Hope it will help.

Hello, I am having this issue too. Would you mind sharing step-by-step instructions on how to perform all of the operations that solved your problem? I am new at this so while I can ssh into root or use console, I don’t know how to (for instance) mpmprefork, etc.

Thank you so much!

I decided to do it the lazy way…created a php script to restart the server....I have set it to run every is the code…

@$link = mysql_connect('localhost', 'user', 'password');
if (!$link) {
exec("sudo service mysql start");

crontab entry under root

*/1 * * * * php /home/{user}/bin/testconnection.php