MySQL error 2002 / Database error: connection failed!

November 22, 2014 3.6k views

I host a roundcube installation as well as YOURLS, which both make use of MySQL databases. Everything worked very fine, up until yesterday evening; MySQL decided it was time to make a mess of things and refused to work! Since I host two of my websites on the same VPS, using the same MySQL installation, they both stopped working at once.

The errors I get on my websites are as follows:

Roundcube website: DATABASE ERROR: CONNECTION FAILED! Unable to connect to database.

YOURLS website: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

The YOURLS error gave more insight into my problem, so I googled MySQL socket error and got a few interesting suggestions. On Stack Overflow, they suggested to chmod 777 the mysql socket, add socket=/var/lib/mysql/mysql.sock to my.cnf, but none of this helps. However, one peculiarity I found is that MySQL starts working again after I restart it. Hmm, very strange. I'll try and reinstall it, after all, and see what comes of it.

Please help, if you know how to solve this problem. Thank you!

3 Answers

It looks like MySQL crashed. That usually happens when your droplet runs our of memory.

Try adding a swap file to your droplet: How To Add Swap on Ubuntu 14.04 | DigitalOcean.

by Justin Ellingwood
Swap space can be used as an "overflow" area for your system when you run out of RAM. The operating system can store data that would normally be kept in RAM on the hard drive in a specially formatted file. In this guide, we'll demonstrate how to create and use one of these files in Ubuntu 14.04.

This happend to 2 of my droplets too (yesterday evening)! How to fix it? I don't think that adding a swap file will help.

I have the same problem in one of my droplet. I did a full update of the packages in my system. the mysql is up and running now.

sudo apt-get upgrade

Have another answer? Share your knowledge.