Cannot reinstall mysql-server after its purge

Edited Jan 10 2017: This is a major review of this post to correct serious issues in this post.

The Error at Heart

The problem is that the package is still on the system in an half-installed and half-configured state and needs to be explicitly removed.

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)

What its really saying is that the package mysql-server-5.7 is a dependency for mysql-server, is already installed, but is not configured. So you need to purge it to remove those breadcrumbs left behind by mysql-server-5.7.

sudo apt purge mysql-server mysql-server-5.7

Rationale

When you install software using apt, it automatically handles dependencies for you as well.

When you remove certain packages, it may not handle those same dependencies. In the case of this post, that dependency is mysql-server-5.7.

You can check to see a package state by issuing the following command.

dpkg-query -l [package-name-here]

Usually if you see the code un or rc to the left of the package name, you'll be able to tell if it actually is a broken package.

When I experienced this issue, it was with libapache2-mod-php and libapache2-mod-php7.0. This was my output.

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                        Version            Architecture       Description
+++-===========================-==================-==================-============================================================
un  libapache2-mod-php          <none>             <none>             (no description available)

In my case, it claimed that the status of my package is unknown and that it is not installed (the code un) on my system.

When you tell apt to remove something, it can leave packages, configuration files, and other items that can be problematic during automated installations.

When you tell apt to purge something it does it's best to remove any breadcrumbs that remove might have left behind.

I originally stated that I assumed the process was still running, but most likely it was Inactive, or dead.

The best way to check if the service is interfering with your package removal is to check that service first.

1) Investigating the mysql service

Using the system error given to us by apt, we can actually use systemctl to investigate the error by checking on the status of the mysql service

sudo systemctl status [pattern]

In our case, we want to see if mysql is running so can type in

systemctl status mysql.service

You should see this output if the service is running

systemctl status mysql
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2017-01-10 23:10:06 EST; 1h 3min ago
 Main PID: 1206 (mysqld)
   CGroup: /system.slice/mysql.service
           └─1206 /usr/sbin/mysqld

note: If the service is dead, you'll see a short message indicating that there is no service by that name running and then skip to step 3.

2) Stopping the mysql service using systemctl

note: [pattern] must be the name listed by service or initctl. the reason I use pattern is because systemctl uses regex matching, so be careful if you must use the kill argument.

sudo systemctl stop [pattern]

where pattern represents the mysql daemon/service name. if stop does not work try

sudo systemctl kill [pattern]

For example

sudo systemctl stop mysql

3) Uninstalling/purging mysql

if you need to totally purge, make sure the service or process is stopped first and then make sure you're removing the right files and directories!

Note: Make sure you are targeting the correct mysql version. For example, if you use 5.5, adjust the version number appropriately.

WARNING: the following steps will delete your data! (the first command must be executed to do a backup)

source: How do I uninstall MySQL?

tar -zcvf ~/msql_backup.tar.gz /etc/mysql /var/lib/mysql 
sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-5.7 mysql-client-core-5.7
sudo rm -rfv /etc/mysql /var/lib/mysql
sudo apt autoremove
sudo apt autoclean

4) Fixing broken packages and handling missing dependencies

If the preceding steps did not work for you, you may need to run apt with the --fix-broken option to repair any damage done.

Make sure to apt update first and then apt install

sudo apt update    
sudo apt install mysql-server mysql-client --fix-broken --fix-missing

5) When all else fails, build and install mysql yourself

If none of the above works, you'll have to manually download the source, compile, and use make or bash to install from there (not as painful as it sounds since its all automated).


The problem with the first post is you can't reconfigure a meta-package well not for the sql items. You need to specify the current release item.

Say use;

apt search mysql-server

That should display a list of packages namely

"mysql-server-5.7" "mysql-server-core-5.7" or later releases

then;

dpkg-reconfigure --force mysql-server-5.7 mysql-server-core-5.7

done.


This is the correct solution for you

First, you will have to remove all packages of mysql-server:

WARNING: the following steps will delete your data! Do a backup first!

sudo rm -rf /var/lib/mysql

Then install:

sudo apt-get install lamp-server^

Or you can do:

sudo apt-get install mysql-server