How To Completely Remove MySQL From Ubuntu

By bhagwatchouhan
How To Completely Remove MySQL From Ubuntu

This post explains the steps required to completely remove MySQL Community Server 5.7 from Ubuntu 18.04 using MySQL Notifier. The steps should be the same for other versions of MySQL and Ubuntu. MySQL 5.7 can be directly installed on Ubuntu 18.04.

In certain situations, we can observe the below-mentioned errors while running the commands including remove, autoremove, install, upgrade, etc.

dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
No apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                          Errors were encountered while processing:
 mysql-server-5.7
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

You may also face errors as shown below.

Setting up mysql-server-5.5 (5.5.49-0ubuntu0.14.04.1) ...
start: Job failed to start
invoke-rc.d: initscript mysql, action "start" failed.
No apport report written because the error message indicates its a followup error from a previous failure.
                      dpkg: error processing package mysql-server-5.5 (--configure):
 subprocess installed post-installation script returned error exit status 1
 dpkg: dependency problems prevent configuration of mysql-server:
  mysql-server depends on mysql-server-5.5; however:
  Package mysql-server-5.5 is not configured yet.
 dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 mysql-server-5.5
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

We may require to completely remove the existing MySQL server and re-install it to avoid such a situation.

 

Step 1: Backup

The most important step is to back up your table data, else all the existing data will be lost. MySQL stores the data at /var/lib/mysql /data/mysql.

# Data Backup
sudo rsync -av <mysql data> <backup location>
# Example sudo rsync -av /var/lib/mysql /data/mysql
# Complete Backup tar -zcvf <destination file> /etc/mysql /var/lib/mysql
# Example tar -zcvf /data/msql_backup.tar.gz /etc/mysql /var/lib/mysql

 

Step 2: Stop MySQL Server

Make sure that MySQL server is not running. It can be stopped using the below-mentioned commands.

# Check Status
systemctl status mysql.service
# Stop MySQL Server if its running systemctl stop mysql
# Kill the process if required systemctl kill mysql

Step 3: Remove MySQL Server

After having the backup and stopping the server, we can completely remove MySQL server using the commands as mentioned below.

# Complete uninstall
apt purge mysql-server mysql-client mysql-common mysql-server-core-5.7 mysql-client-core-5.7
# Remove residual files
rm -rfv /etc/mysql /var/lib/mysql
# Remove old config apt-get remove dbconfig-mysql

 

Step 4: Clean Dependencies

We can remove the left-over dependencies using the commands as mentioned below.

# Autoclean
apt autoclean
# Auto remove apt autoremove

 

Step 5: Re-Install

The MySQL server can be installed back using the below-mentioned commands.

# Refresh packages list
apt-get update    
# Re-install MySQL Server apt-get install mysql-server mysql-client --fix-broken --fix-missing

Notes: You may also follow the MySQL tutorials - How To Install MySQL 8 on Ubuntu, and Learn Basic SQL Queries Using MySQL.

 

Summary

We can follow the above steps in tough situations when all other attempts fail or time does not permit to wait for proper resolution. After re-installing the MySQL server, you may restore your existing database using the backup taken at the beginning of this tutorial.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan