Clean Reinstall of MySQL on Ubuntu

Recently, I came back to an existing WSL2 installation of MySQL, which unfortunately failed to start the mysql service (“MySQL has been frozen to prevent damage to your system.”). As I didn’t need any of the data, that I potentially used in the past, I decided to do a clean reinstallation of mysql-server, which turned out to be a bit tricker than I thought at first.

WARNING: This will delete your databases! Do not do this, if you want to retain your data!

sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /var/lib/mysql*
sudo rm -rf /etc/mysql
sudo apt-get install mysql-server

This worked successfully, but then the mysql service still didn’t want to start, because now the database was completely uninitialized.

sudo mysqld --initialize

And finally, to make sure we’re securing the installation:

sudo mysql_secure_installation

Now you can access mysql by running: sudo mysql

Troubleshoot

If you an error similar to the following:

mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)

It means, that you didn’t properly delete all the /var/lib/mysql* directories before and thus things weren’t correctly initialized when running sudo mysqld --initialize

Do the steps above again, but make sure the directories are deleted first this time.

References

Leave a Comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.