MYSQL Error 2002 cant connect to localhost through socket

January 17, 2017 710 views
MySQL

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.

3 Answers

@nritsonsa478bff2e7b215cb05

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.

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

  • sudo systemctl status mysql

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

  • sudo service mysql status

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

  • sudo systemctl start mysql

Or using service command:

  • 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:

  • 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.

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...
  • tried unblocking the ports but still same error
    also not out of space : http://prntscr.com/dx6apd

    tried uninstalling but get the error that ''mysql-server-5.1' is not installed,'
    and when reinstalling i get: mysql-server is already the newest version (5.7.16-0ubuntu0.16.10.1)

    Removed all of mysql (http://askubuntu.com/questions/640899/uninstall-mysql-completely) + Apache2

    Used Someusers method of : sudo apt-get install tasksel and having it install LAMP.

    When i checked APache2 there are no conf files and i cannot access the site from ip_address/index.html

    when looking for mysql -v i get "access denied for user 'root'@'localhost' (using password: No)

status says its active (running)
the log says : http://prntscr.com/dx3l1h

UFW is blocking the port?

Have another answer? Share your knowledge.