MYSQL Error 2002 cant connect to localhost through socket

Running a 1GB Droplet with Apache2 installed. I installed MYSQL but keep getting this error: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

been running though forums and stackoverflow but cannot seem to fix it. amongst trying to fix it i also seem to no longer be able to access sites through /var/www/sitename although ip_address brings up the apache 2 default page settings.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

status says its active (running) the log says :

UFW is blocking the port?

Problem like this can happen if MySQL is not running. You should check it’s status by using systemctl command:

  1. sudo systemctl status mysql

If you are using Ubuntu 14.04, above command will not work and you’ll have to use:

  1. sudo service mysql status

If you see that it’s stopped or failed try to start it again to see will it come up:

  1. sudo systemctl start mysql

Or using service command:

  1. sudo service mysql start

Check status once again. If it’s running, try accessing it. In that case you need to analyze logs to see why it went down. If not, did you change something in MySQL configuration? If yes, revisit it and make sure you didn’t make any mistake.

If starting service worked, to prevent this happening, you have to check log files. Usual reason why MySQL goes down is that it went out-of-memory. Take a look at system log - syslog:

  1. sudo nano /var/log/syslog

If you find there any OOM, out of memory, OOM killer entries that means MySQL went down due to out-of-memory. In that case consider upgrading Droplet or installing Swap. If not, look around and see is there any other MySQL error.


How was MySQL installed and did you modify any configuration files (i.e. my.cnf) at any point?

This is a really common error when it comes to modifying configuration as some configuration works on one version but not another. Knowing which works where is the often the difference between a working and failing MySQL server.

If you didn’t modify any configuration, I would recommend first simply restarting your Droplet. Seems very basic, but sometimes that’s all that’s needed without getting overly complicated and performing too many steps (i.e. prevent overkill).

You can do this from the command line by running:

shutdown -r now

Which simply means shutdown, restart, now! The server will disconnect and within a minute or so should be back online.