Uninstall MySQL Server from Ubuntu Completely in 2 Easy Steps

Posted on

Uninstall MySQL Server from Ubuntu Completely in 2 Easy Steps

Uninstall MySQL Server from Ubuntu Completely in 2 Easy Steps

This guide aims to teach you how to Uninstall MySQL Server from Ubuntu Completely. You might discover that simply removing MySQL from your Ubuntu server doesn’t eliminate all the associated dependencies and files. Therefore, this article will demonstrate how to completely remove MySQL from your Ubuntu server. Keep reading for detailed instructions.

You can now follow the guide steps below from the Orcacore website to Uninstall MySQL Server from Ubuntu Completely.

In this guide, we will use Ubuntu 22.04 as an example to show you the guide steps.

Important Note: Remember to back up all your databases before you proceed with completely removing your MySQL server. This is also necessary for those who plan to upgrade their MySQL version.

Now, follow the steps below to Uninstall MySQL Server from Ubuntu Completely.

Remove MySQL From Ubuntu Linux

Step 1 – Stop MySQL Service on Ubuntu

First, you can verify whether your MySQL service is active or not on your Ubuntu server with the command below:

sudo systemctl status mysql.service

In my case, I get the following output:

**Output**
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: **active** (**running**) since Sun 2023-09-03 11:14:09 UTC; 21s ago
    Process: 2180 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=e>
   Main PID: 2188 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 4575)
     Memory: 365.3M
        CPU: 1.425s
     CGroup: /system.slice/mysql.service
             └─2188 /usr/sbin/mysqld
...

If your MySQL service is active and running, you must stop it by using the command below:

sudo systemctl stop mysql

At this point, if you check your MySQL status it must be inactive:

sudo systemctl status mysql.service
**Output**
mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: **inactive** (**dead**) since Sun 2023-09-03 11:17:24 UTC; 35s ago
    Process: 2180 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=e>
    Process: 2188 ExecStart=/usr/sbin/mysqld (code=exited, status=0/SUCCESS)
   Main PID: 2188 (code=exited, status=0/SUCCESS)
     Status: "Server shutdown complete"
        CPU: 3.235s
...

Step 2 – How To Completely Delete a MySQL Server from Ubuntu?

At this point, you must remove all the MySQL server packages from your Ubuntu.

Remove MySQL Server Packages

All the server packages related to MySQL start with mysql-server. Now you can easily use the following command to remove all the MySQL server packages:

sudo apt purge mysql-server*

With the apt purge command, MySQL configuration files and binaries will be deleted. But other associated files still remain in your server such as databases and log files.

You must delete the following files to remove the MySQL completely:

  • /etc/mysql
  • /var/lib/mysql
  • /var/log/mysql

So to remove all these files, you can use the following command:

sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql

Remove MySQL Server Dependencies

When you install MySQL server, the default package manager of your operating system will install the required dependencies for the service. At this point, you must remove the MySQL dependencies with the command below:

sudo apt autoremove

When you are done, clean up the disk space too by using the command below:

sudo apt autoclean

Note: In some situations, your configuration files may not be deleted. So you can use this command to remove them:

sudo apt remove dbconfig-mysql

That’s it, you are done with Uninstall MySQL Server from Ubuntu Completely.

Conclusion

At this point, you have learned How To Uninstall MySQL Server from Ubuntu Completely. Before you completely remove your MySQL server just remember to do your database backup that does not affect your data.

Hope you enjoy it. You may be interested in these articles too:

Fix Unknown collation utf8mb4_0900_ai_ci in MySQL

How To Disable Remote Access in MySQL Database


Alternative Solutions to Uninstall MySQL Server from Ubuntu Completely

While the provided method effectively removes MySQL, let’s explore two alternative approaches to achieve the same goal:

Alternative 1: Using apt remove and a Configuration File Search

This method is more granular and might be preferable if you want to review which packages are being removed before proceeding.

Explanation:

  1. Stop the MySQL service: This ensures that no files are in use during the removal process.
  2. Remove MySQL packages: Instead of purge, we use remove. This removes the binaries but leaves the configuration files intact. This allows you to inspect the files if needed.
  3. Find configuration files: We use dpkg -L to list files installed by the mysql-server package, then grep to filter only configuration files in /etc/mysql.
  4. Remove configuration files: We use rm -rf to remove the found config directories.
  5. Remove data and log directories: As in the original method, we remove the data and log directories.
  6. Autoremove dependencies: This removes any dependencies that are no longer required.
  7. Autoclean: This cleans up the apt cache.
  8. Remove dbconfig-mysql: In some situations, your configuration files may not be deleted. So you can use this command to remove them

Code Example:

sudo systemctl stop mysql

sudo apt remove mysql-server*

dpkg -L mysql-server-8.0 | grep /etc/mysql | sudo xargs rm -rf

sudo rm -rf /var/lib/mysql /var/log/mysql

sudo apt autoremove

sudo apt autoclean

sudo apt remove dbconfig-mysql

Note: Replace mysql-server-8.0 with the actual package name of your installed MySQL server version. You can determine this using dpkg -l | grep mysql-server.

Alternative 2: Using tasksel

tasksel is a Debian/Ubuntu tool that helps install and remove tasks (collections of related packages). While primarily used for initial system setup, it can also be used to remove meta-packages like mysql-server.

Explanation:

  1. Stop the MySQL service: Ensures no files are in use.
  2. Run tasksel: This opens a text-based interface.
  3. Deselect mysql-server: Navigate to the mysql-server entry and deselect it (usually by pressing the spacebar). This marks it for removal.
  4. Apply changes: Select "OK" to apply the changes. tasksel will then remove the mysql-server meta-package and its dependencies.
  5. Remove data and log directories: As in the original method, we remove the data and log directories to ensure complete removal.
  6. Autoremove dependencies: This removes any dependencies that are no longer required.
  7. Autoclean: This cleans up the apt cache.
  8. Remove dbconfig-mysql: In some situations, your configuration files may not be deleted. So you can use this command to remove them

Code Example:

sudo systemctl stop mysql

sudo tasksel

sudo rm -rf /var/lib/mysql /var/log/mysql

sudo apt autoremove

sudo apt autoclean

sudo apt remove dbconfig-mysql

Important Considerations for tasksel:

  • tasksel might not remove all configuration files. You might still need to manually remove the /etc/mysql directory.
  • This method is best suited for systems where MySQL was initially installed using tasksel.

By offering these alternative methods, users have more flexibility in choosing the approach that best suits their comfort level and specific needs when attempting to Uninstall MySQL Server from Ubuntu Completely. Each method provides a different balance between automation, granularity, and control over the removal process. Remember to always back up your data before performing any major system changes. You’ve now learned how to Uninstall MySQL Server from Ubuntu Completely using three different methods!

Leave a Reply

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