Many times we face issues while starting the MYSQL server or on installation on Linux System. There may be a couple of reasons for this issue. Sometimes configuration file already exists in the system and is not removed properly. In that case, we have to discard the MYSQL package and configuration file and reinstall it again.
Today, we will explain how you can uninstall the MYSQL package and configuration file completely and fresh install it again on your Linux System.
Note: Make sure to take a backup of your databases if possible before doing the below steps; otherwise, there will be a permanent loss.
Step 1: Uninstall MYSQL Package?
First of all, we have to check whether MySQL is already running in our system or not. If it is already running in our system, then attempt to uninstall might throw you an unusual error.
$ sudo systemctl status mysql
In our case, the MYSQL server is already running in our system. So, we have to stop it before removing it and recheck the status.
$ sudo systemctl stop mysql $ sudo systemctl status mysql
Now time to remove all of the MySQL packages from your Linux System.
sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*
Step 2: Removing the MYSQL Directory
After uninstalling the MYSQL package, we have to remove the MYSQL directory from our system. By default, this directory is located in
/var/lib/mysql. Here all information related to your database is located.
We strongly recommend you to rename the directory instead of removing it if you wish to recover any piece of information in the future.
$ sudo mv /var/lib/mysql /var/lib/mysql_bk $ sudo mv /etc/mysql /etc/mysql_bk
The last step is to clean all the packages that are installed while installing MYSQL in our system.
$ apt autoremove $ apt autoclean
Step 3: Installation of MYSQL Package
After complete uninstallation and removing the configuration file from our system. On a fresh installation, it will recreate the fresh files in
Time to install MySQL server in our system using the following command, but before that, make sure your system is connected with working internet.
$ sudo apt install mysql-server
Read this :- How to install MySQL properly in Linux?
If you are still facing any difficulty while removing and reinstalling MySQL in your system. Feel free to ask your query in the comment section.