How to remove/reinstall MySQL
For years, remove and reinstall MySQL has been one of the hardest work for me. However, I’ve found a good solution.
Based on http://askubuntu.com/questions/763534/cannot-reinstall-mysql-server-after-its-purge and http://askubuntu.com/questions/760724/16-04-upgrade-broke-mysql-server
- Back up your
my.cnf file
in/etc/mysql
and remove it - Remove the folder
/etc/mysql/mysql.conf.d/
- Verify you don’t have a
my.cnf
file stashed somewhere else (I did in my home dir!) or in/etc/alternatives/my.cnf
. - Backup and remove
/etc/mysql/debian.cnf
files (not sure if needed, but just in case)sudo apt purge mysql-server mysql-server-5.7 mysql-server-core-5.7 sudo apt install mysql-server
- In case your syslog shows an error like “mysqld: Can’t read dir of ‘/etc/mysql/conf.d/'” create a symbolic link
ln -s /etc/mysql/mysql.conf.d /etc/mysql/conf.d
Then the service should be able to start withservice mysql start
.
sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.7 mysql-client-core-5.7
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean
Then reboot, reinstall. Everything’s finally fine.
Here is a script.
rm /etc/mysql/my.cnf
rm -rf /etc/mysql/mysql.conf.d/
rm /etc/alternatives/my.cnf
rm /etc/mysql/debian.cnfsudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.7 mysql-client-core-5.7
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean
reboot
#After reboot:
apt-get install mysql-server