CentOSLinuxTutorials

How To Completely Removing MySQL Server in CentOS

Completely Removing MySQL Server in CentOS

In this tutorial, we will show you how to completely remove MySQL on your CentOS server. For those of you who didn’t know, MySQL is a widely used open-source relational database management system known for its reliability, performance, and ease of use. However, there may come a time when you need to completely uninstall the MySQL server from your CentOS system. This could be due to various reasons, such as upgrading to a newer version, replacing it with another database system, or performing a clean install.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you step-by-step how to remove MySQL on the CentOS server. Maybe it’s a good idea to back up databases before doing this.

Prerequisites

  • A server running one of the following operating systems: CentOS Linux or RHEL-Based.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Completely Removing MySQL Server in CentOS

Step 1. Stop MySQL Service.

The first step in uninstalling MySQL is to stop the running MySQL service. To check if the MySQL service is currently running, open a terminal and execute the following command:

sudo systemctl status mysqld

If the MySQL service is active, you will see output indicating its status. To stop the service, run:

sudo systemctl stop mysqld

This command will gracefully shut down the MySQL server, ensuring that all pending transactions are completed and data is safely written to disk.

Step 2: Uninstall MySQL Packages.

Now that the MySQL service is stopped, we can proceed with uninstalling the MySQL packages. It’s important to note that simply uninstalling the packages does not remove the associated data and configuration files.

For CentOS/RHEL systems, use the following command to uninstall MySQL:

sudo yum remove mysql-server mysql mysql-libs

On Fedora systems, use:

sudo dnf remove mysql-server mysql mysql-libs

During the uninstallation process, you may be prompted to confirm the removal of the packages. Press “y” and hit Enter to proceed.

Step 3. Remove MySQL Data Directory.

The MySQL data directory contains all the databases, tables, and related files. By default, the data directory is located at /var/lib/mysql. However, it’s always a good practice to check the exact path in the MySQL configuration file.

To find the data directory path, open the MySQL configuration file (my.cnf) using a text editor:

sudo nano /etc/my.cnf

Look for the datadir directive, which specifies the location of the data directory.

Instead of directly deleting the data directory, it is recommended to rename or move it to a different location as a backup measure. Use the following command to rename the data directory:

sudo mv /var/lib/mysql /var/lib/mysql_backup

This command renames the mysql directory to mysql_backup, effectively removing it from its original location.

Step 4. Remove MySQL Configuration Files.

MySQL stores its configuration files in various locations on the system. The most common locations are:

  • /etc/my.cnf
  • /etc/mysql/my.cnf
  • /usr/local/mysql/etc/my.cnf

To remove these configuration files, use the following commands:

sudo rm -rf /etc/my.cnf
sudo rm -rf /etc/mysql
sudo rm -rf /usr/local/mysql/etc

These commands will recursively remove the specified directories and their contents. Be cautious while using the rm -rf command, as it permanently deletes files without prompting for confirmation.

Additionally, check for any other MySQL-related configuration files in the /etc directory and remove them accordingly.

Step 5. Remove MySQL User and Group.

During the installation of MySQL, a dedicated user and group are created to run the MySQL server process. Removing these is an important step in completely uninstalling MySQL from your system.

To remove the MySQL user and group, execute the following commands:

sudo userdel mysql
sudo groupdel mysql

These commands will delete the mysql user and group from the system.

To verify that MySQL has been completely uninstalled, you can use the package manager to list any remaining MySQL packages. Run the following command:

yum list installed | grep mysql

If MySQL is fully uninstalled, this command should not produce any output. If you see any MySQL-related packages listed, you can remove them using the yum remove command followed by the package names.

Congratulations! You have successfully removed MySQL. Thanks for using this tutorial for removing MySQL from the CentOS system. For additional help or useful information, we recommend you check the official MySQL website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button