How to properly uninstall MySQL Server in Ubuntu – FOSS Linux

3K

Ubuntu’s package management system makes it very simple to install, update, or remove software from the system. However, some software packages have many configuration directories and need extra effort to uninstall.

MySQL server is one such frequently used piece of software in Ubuntu. We cannot use the standard apt purge command to remove the MySQL server from the system completely. Certain measures must be taken to ensure that MySQL and its components are properly removed from the Ubuntu system.

We can frequently encounter problems when trying to start the MYSQL server on a Linux system. There may be several explanations for this. First, sometimes there is an old configuration file on the system and it is not cleared properly after uninstallation. Therefore, we must manually delete the MYSQL server and configuration file in that scenario.

Today, we will show you how to remove MYSQL server and configuration file from your Ubuntu operating system.

Note: Before proceeding, back up all MySQL Server databases. All files and directories related to MySQL Server will be deleted after completing this operation. Therefore, if you want to save your databases, you must first set up a backup.

Uninstall successfully

MySQL Server on Ubuntu To completely uninstall MySQL Server and all its dependencies

from your system, perform the actions listed below in order:

Shut down MySQL Server Uninstall

  1. MySQL Server Uninstall MySQL databases and log files
  2. Uninstall
  3. dependencies We will

review all the steps mentioned above in detail; Therefore, there is no need to worry. All you need is to sit back, relax and closely follow the guide to understand how to properly uninstall MySQL Server and its dependencies from your Ubuntu operating system. For the sake of this guide, I will be using Ubuntu 22.04, which at the time of writing this guide, is the latest stable version of Ubuntu.

1. Close

MySQL Server

The first step is to determine whether or not the MySQL server is operating on our machine. Because trying to remove it while it is currently running can cause an error.

Run the following command to check the status of the MySQL server on your PC.

sudo systemctl status mysql

If the report indicates that MySQL Server is operating on the machine, we must stop it before continuing. To end the process, use the following command:

sudo systemctl stop mysql

To see if the service has stopped, use the systemctl status mysql command. After making sure that the program is not running, we can remove the MySQL server seamlessly. We can also remove any dependencies that were installed during the server configuration process:

sudo systemctl status mysql

2. Uninstall

MySQL Server

Removing packages from MySQL Server is the next stage in the uninstall procedure. Because all MySQL Server packages on Ubuntu start with mysql-server, we can remove them all at once with the apt-purge command.

To uninstall MySQL packages from your system, use the following command

: sudo apt purge mysql-server*

OR

You can use this command to ensure that any type of MySQL installed on your system is uninstalled

. sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*

Note: We could have deleted the MySQL package using the remove command. However, the remove command simply removes/deletes the program binaries, but the purge command also deletes the application configuration data.

3. Uninstall

MySQL databases and log files

Although we use the purge command to remove the program’s binaries and configuration files, specific databases, security keys, and configuration files must be purged individually

.

The configuration files that need to be deleted are located in /etc/mysql. The security keys that need to be removed can be found in /var/lib/mysql.

We need to make sure that these files are completely deleted. Otherwise, they will remain on your system and create problems when reinstalling MySQL Server.

Run the instructions below to see if there are any files in the folders listed above.

ls /etc/mysql sudo ls /var/lib/mysql Run the

following command to delete these configuration files, security keys, and database files

. sudo rm -r /etc/mysql /var/lib/mysql

Note: We recommend renaming these folders instead of deleting them, as the data contained in them may be useful in the future if

you need to restore data.

If you enabled logging for MySQL Server, we would also need to clear the log files. Use the following command to clear any log files produced by MySQL Server.

sudo rm -r /var/log/mysql

Note: If the file does not exist, you will see output similar to the following:

4. Uninstall

dependencies

When we install MySQL Server, the package manager also installs several additional dependencies required to run the server. However, because we removed the main package, MySQL Server, these dependencies are no longer needed and must be uninstalled.

Dependencies like these are sometimes referred to as orphaned packages since their main package has been removed and these packages are no longer useful.

Run the following apt command to remove such dependencies.

sudo apt autoremove

It is important to remember that the autoremove command removes all orphaned packages from the system. As a result, the above command will remove residual dependencies from MySQL Server and delete other orphaned packages. We can also use the apt autoclean command to remove any remaining dependencies.

That’s all. The MySQL server has been completely uninstalled from your Ubuntu operating system.

Conclusion To

summarize, the article taught us how to properly remove/uninstall MySQL Server and its related components from Ubuntu. You must first shut down all active MySQL services to completely remove the MySQL server from your machine. Then, uninstall the MySQL server. Once you’re done, uninstall the MySQL databases and log files, then finish by uninstalling the dependencies. Following those guides to the latter will permanently remove the MySQL server from your Ubuntu operating system.

Database operations should be performed with extreme caution to avoid data loss. Backing up your databases is standard practice in the business and can prevent you or your company from suffering catastrophic data loss.

Contact US