Fix MySQL corrupted configuration on Ubuntu

Today, I ran into a problem involving a corrupted MySQL server 5.5 configuration in Ubuntu 13.04. I am not sure how it got to be corrupted in the first place but whenever I ran “mysql” to open a connection to the server, I would get a very generic MySQL error code:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

As far as I can tell, the only way to fix that particular problem is to remove and purge configuration so I attempted to remove the server using “sudo apt-get –purge remove mysql-server-5.5” and clean up the other packages that depend on mysql-server-5.5. I also tried to reinstall the packages but I still ran into the same problem. After reinstalling the package, I noticed a problem where a root user had already been created on the server and my password wasn’t working with the account nor did I know what the original password was. I am not sure if this particular issue is related or not. However, after Googling the problem and trying many different things and combining solutions I finally found something that seems to have worked:

sudo apt-get --purge remove mysql-server mysql-common mysql-client
sudo mv /var/lib/mysql /var/lib/mysql.backup
sudo apt-get install mysql-server mysql-common mysql-client

I believe that the problem involves a corrupted table in the directory where MySQL stores the database files so you have to do additional file cleanup when trying to remove MySQL.

I wish you luck if you ever find yourself in this situation and hopefully you can easily remove the server. If not, the commands will create a backup of the files which you may be able to restore after you have reinstalled MySQL.