Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

While one rarely looks forward to it, sometimes databases get corrupted. Unfortunately, when this happens, we might have to completely reinstall the database management system.

Ordinarily, if we’re trying to reinstall MySQL on Linux, we can uninstall and install using the distro’s package manager. But if the current installation is corrupted, the usual way may not work for us. So our best bet could be a clean reinstall.

In this tutorial, we’ll learn how to do a complete clean reinstall of MySQL on Debian and Red Hat Linux distros. For each distro, we’ll go over what to expect as we run the commands.

2. Using the apt Command

The apt command is the package manager for Debian distros, and we can do a clean reinstall of most packages via its options.

2.1. Uninstalling MySQL

When trying to completely remove MySQL or pretty much any other package, we have to first uninstall it.

However, the usual uninstall commands leave some files and metadata. But then, these might keep us from successfully setting up MySQL after removing it. For example, a corrupted database could persist.

So, we can include the –purge switch in the regular apt remove command:

$ sudo apt remove --purge mysql\* -y
...truncated...
The following packages will be REMOVED:
mysql-apt-config* mysql-client* mysql-common* mysql-community-client* mysql-community-client-core*
mysql-community-client-plugins* mysql-community-server* mysql-community-server-core* mysql-server*
0 upgraded, 0 newly installed, 9 to remove and 48 not upgraded.
After this operation, 298 MB disk space will be freed.
...truncated...
Removing mysql-common (8.0.31-1debian11) ...
...truncated...
Purging configuration files for mysql-community-server (8.0.31-1debian11) ...

Critically, this command thoroughly cleans the MySQL installation. Because of this, we should only use this method when we have a backup of our databases or when we have no means of retrieving any corrupted data.

After running the command above, we’ll check for leftovers with find:

$ sudo find / -name mysql
/etc/apparmor.d/abstractions/mysql
/usr/lib/python3/dist-packages/ansible_collections/community/mysql
/usr/share/bash-completion/completions/mysql
/usr/share/php8.1-mysql/mysql

This find command is a basic way to verify whether we still have MySQL files and directories. In our case, it returns some MySQL files and directories, but they aren’t directly related to the MySQL package. So, we’ll ignore them.

2.2. Reinstalling MySQL

After confirming that we’ve successfully done a clean removal of MySQL, we can move to the next step – reinstallation.

To set up MySQL again, we could run:

$ sudo apt install mysql mysql-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package mysql-server is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Unable to locate package mysql
E: Package 'mysql-server' has no installation candidate

Yet, as our output shows, that command might not work on some Debian distros.

Instead, we can set up MySQL by downloading the MySQL repo setup package for Debian distros:

$ wget http://repo.mysql.com/mysql-apt-config_0.8.24-1_all.deb
--2022-12-28 16:58:55-- http://repo.mysql.com/mysql-apt-config_0.8.24-1_all.deb
Resolving repo.mysql.com (repo.mysql.com)... 104.123.44.225
Connecting to repo.mysql.com (repo.mysql.com)|104.123.44.225|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 18048 (18K) [application/x-debian-package]
Saving to: ‘mysql-apt-config_0.8.24-1_all.deb.2’ mysql-apt-config_0.8.24-1_all 100%[=================================================>] 17.62K --.-KB/s in 0.005s
2022-12-28 16:58:57 (3.38 MB/s) - ‘mysql-apt-config_0.8.24-1_all.deb.2’ saved [18048/18048]

After that, we install the downloaded repo:

$ sudo dpkg -i mysql-apt-config_0.8.24-1_all.deb
(Reading database ... 90853 files and directories currently installed.)
Preparing to unpack mysql-apt-config_0.8.24-1_all.deb ...
Unpacking mysql-apt-config (0.8.24-1) over (0.8.24-1) ...
Setting up mysql-apt-config (0.8.24-1) ...
Warning: apt-key should not be used in scripts (called from postinst maintainerscript of the package mysql-apt-config)
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK

Next, let’s update the package information:

$ sudo apt update
Hit:1 http://repo.mysql.com/apt/debian bullseye InRelease
Hit:2 http://deb.debian.org/debian bullseye InRelease
Hit:3 http://security.debian.org/debian-security bullseye-security InRelease
Get:4 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:5 https://packages.sury.org/php bullseye InRelease [6,841 B]
Fetched 50.9 kB in 6s (8,062 B/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
48 packages can be upgraded. Run 'apt list --upgradable' to see them.

Finally, we can install MySQL:

$ sudo apt install mysql-server -y
...truncated...
 mysql-community-server mysql-community-server-core mysql-server
0 upgraded, 8 newly installed, 0 to remove and 48 not upgraded.
Need to get 0 B/35.1 MB of archives.
After this operation, 298 MB of additional disk space will be used.
Preconfiguring packages ...
Selecting previously unselected package mysql-common.
...truncated...
Setting up mysql-community-client (8.0.31-1debian11) ...
...truncated...
Processing triggers for libc-bin (2.31-13+deb11u4) ...

At this time, the latest version of the MySQL apt repo is 0.8.24-1. When running these same commands at a later time, we can adjust the version accordingly, if necessary.

After we run the installation commands, we can check whether MySQL is up and running. Shell commands like mysqlshow and mysqlcheck come in handy here.

3. Using the yum Command

When trying to do a clean MySQL reinstall on a Red Hat Linux distro, we can use yum. Like the apt command, we’ll start with uninstallation.

3.1. Uninstalling MySQL

To ensure we remove MySQL and its dependencies, we use the autoremove subcommand of yum:

$ sudo yum autoremove mysql\* -y
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.x86_64 0:8.0.31-1.el7 will be erased
--> Processing Dependency: mysql-community-client(x86-64) >= 8.0.11 for package: mysql-community-server-8.0.31-1.el7.x86_64
...truncated...
Removed: mysql-community-client.x86_64 0:8.0.31-1.el7 Dependency Removed: libaio.x86_64 0:0.3.109-13.el7 mysql-community-client-plugins.x86_64 0:8.0.31-1.el7 mysql-community-common.x86_64 0:8.0.31-1.el7 mysql-community-icu-data-files.x86_64 0:8.0.31-1.el7
...truncated...
Complete!

Since using yum remove alone doesn’t remove dependencies, we use autoremove to completely remove any trace of MySQL.

After running the command above, we can again use find to check for any related files we don’t want:

$ sudo find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/lib64/mysql

In this case, the output reveals we have three leftover MySQL files. Let’s delete them manually using the rm command:

$ sudo rm -rf /var/lib/mysql /usr/lib64/mysql

Then, we verify all of them are gone:

$ sudo find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql

At this time, we should be ready for a reinstall.

3.2. Reinstalling MySQL

To reinstall MySQL, we can use yum install:

$ sudo yum install mysql

However, in the absence of the MySQL yum repo, this may pick the MariaDB package. To avoid that, we’ll add the MySQL repo first:

$ wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
--2022-12-28 16:37:32--  https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 23.39.113.250, 2a02:26f0:e0:5bd::2e31, 2a02:26f0:e0:594::2e31
Connecting to dev.mysql.com (dev.mysql.com)|23.39.113.250|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm [following]
--2022-12-28 16:37:35--  https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.215.180.253
Connecting to repo.mysql.com (repo.mysql.com)|23.215.180.253|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11196 (11K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el7-7.noarch.rpm.1’

100%[======================================================================================================>] 11,196      --.-K/s   in 0s

2022-12-28 16:37:36 (98.9 MB/s) - ‘mysql80-community-release-el7-7.noarch.rpm.1’ saved [11196/11196]

Then, we’ll install the repo:

$ sudo yum install mysql80-community-release-el7-7.noarch.rpm -y
Loaded plugins: fastestmirror
Examining mysql80-community-release-el7-7.noarch.rpm: mysql80-community-release-el7-7.noarch
Marking mysql80-community-release-el7-7.noarch.rpm to be installed
...truncated...
Dependencies Resolved
...truncated...
Running transaction
  Installing : mysql80-community-release-el7-7.noarch                                                                                       1/1
  Verifying  : mysql80-community-release-el7-7.noarch                                                                                       1/1
Installed:
  mysql80-community-release.noarch 0:el7-7
Complete!

Finally, we can deploy MySQL with the new repo in place:

$ sudo yum install mysql-community-server -y
Loaded plugins: fastestmirror
...truncated...
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:8.0.31-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 8.0.31-1.el7 for package: mysql-community-server-8.0.31-1.el7.x86_64
...truncated...
Installed:
  mysql-community-server.x86_64 0:8.0.31-1.el7
Dependency Installed:
libaio.x86_64 0:0.3.109-13.el7      
...truncated...
mysql-community-client.x86_64 0:8.0.31-1.el7
...truncated...
perl-threads-shared.x86_64 0:1.43-6.el7
Complete!

Now, we can start MySQL:

$ sudo systemctl start mysqld

MySQL yum repos are available for various RHEL versions and other distros.

4. Conclusion

In this article, we learned how to do a complete clean reinstall of MySQL on two Linux distros (Debian and Red Hat).

Overall, the installation and uninstallation processes and commands for both distros are pretty similar. However, the discrepancies between the package manager and the filesystem remain. So, in the end, we can’t use exactly the same files and commands to pull off a clean reinstall of MySQL on all Linux distros.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!