My MySQL installation is broken. How to completely reconfigure it?

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                     

Solution 1:

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

Solution 2:

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
socket=/var/lib/mysql/mysql.sock

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

[client]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

Solution 3:

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.

Solution 4:

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.

Solution 5:

This worked for me

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

Description

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.


Refrences[!]:

1) DigitalOcean