Here are the following commands I have done.

$ sudo service mysql start
mysql start/running

$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

$ aptitude search mysql |grep ^i
i   libapache2-mod-auth-mysql       - Module Apache 2 pour l'authentification vi
i   libdbd-mysql-perl               - Perl5 database interface to the MySQL data
i   libmysqlclient16                - MySQL database client library             
i   mysql-client-5.1                - MySQL database client binaries            
i   mysql-client-core-5.1           - MySQL database core client binaries       
i   mysql-common                    - MySQL database common files, e.g. /etc/mys
i   mysql-server-5.1                - MySQL database server binaries and system 
i   mysql-server-core-5.1           - MySQL database server binaries            
i   php5-mysql                      - MySQL module for php5                     
  • alm - find your mysql.log cat /var/log/mysql.log and edit your answer withthis file....
    – hhlp
    Commented Oct 19, 2011 at 19:47
  • 5
    I like the way you used grep ^i there. Pretty intelligent. Commented Oct 19, 2011 at 20:25
  • What exactly does it do? aptitude is installation tools.
    – user4951
    Commented Aug 7, 2012 at 9:13
  • @JimThio That's to find the installed packages related to this question.
    – nanofarad
    Commented Aug 14, 2012 at 14:04

5 Answers 5


Before reinstalling check /var/log/mysql for logs files which may contain clues as to why mysql is not working. If there are no logs log to file might not be enabled in your configuration: Configure Mysql Error Log

To reinstall any package check installed version with

dpkg -l|grep mysql-server

then for version 5.7 use

sudo apt-get install mysql-server-5.7 --reinstall

If this doesn't allow you to reconfigure the package you can use

sudo dpkg-reconfigure mysql-server-5.7
  • sudo dpkg-reconfigure mysql-server-5.1 did the trick for me when I upgraded from 11.10 to 12.04 i mysqld wouldn't start and i was forced to manually sudo mysqld
    – Hanynowsky
    Commented Mar 14, 2012 at 2:12
  • sudo apt-get install mysql-server --reinstall this seems a very useful command. Commented Sep 23, 2020 at 2:47

ERROR 2002 (HY000): Can not connect to local MySQL server-through socket '/ tmp / mysql.sock' (2)

In MySQL installations can specify where we will have the socket for local connections. When making updates is not uncommon to see the error "Can not connect to local MySQL server socket-through." Let's see how to solve this problem.

The error look like this:

Mysql-u root-p
Enter password:
ERROR 2002 (HY000): Can not connect to local MySQL server-through socket '/ tmp / mysql.sock' (2)

thru ps we can see if is specified by parameter and the place where is it:

# ps -fea | grep mysqld
mysql    17661 14003  1 Feb19 ?        00:24:59 /usr/local/mysql-percona/libexec/mysqld --basedir=/usr/local/mysql-percona --datadir=/var/data/mysql/datadir/data --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock --log-error=/var/log/mysqld.log
root     23790  7840  0 09:25 pts/0    00:00:00 grep mysqld

In this case we see is in /var/lib/mysql/mysql.sock. If not found as a parameter should look into mysqld section of /etc/my.cnf to find the parameter:

grep socket /etc/my.cnf

If we know where is it we need to modify the same file (/etc/my.cnf) and add the parameter section socket client:

  • 1
    That is what I was going to put as an answer. I already know this is the correct answer for almost all related problems like this. Will +1 you when I get the ability again. Commented Oct 19, 2011 at 20:24
  • thanks for the thorough explanation! in my case even if i specified a socket in my.cnf it didn't listen on it (params shown in 'ps' are empty and no connection possible. workaround: add 'protocoll=tcp' to client section in my.cnf to force using tcp and not the socket.
    – Tapper
    Commented Mar 31, 2012 at 19:52

For the latter viewers:
I came to the similar error when I failed to upgrade mysql-server package, but I found this solved my problem. When sudo apt-get install -f doesn't work, try sudo apt-get purge mysql*. Now you can reinstall mysql-server.

  • 1
    please note that sudo apt-get purge mysql wont preserve your databases Commented Jun 17, 2017 at 11:08

One of the problems I am running into is knowing which version I am having issues with. This was a neat idea:

$ aptitude search mysql |grep ^i

But here's what I found was critical in the issue:

$ apt-get --reinstall install mysql-server-5.x

(where x is the version. If you don't add this, you could be working with the wrong dpkg.


This worked for me

sudo apt purge mysql*
sudo apt autoremove
sudo apt autoclean
sudo apt install mysql-server


1) The first command removes everything related to MySQL with their configuration files.

2) The second one removes those packages which are not needed anymore.


1) DigitalOcean

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .