Ubuntu – Cannot reinstall thesql-server after its purge

16.04MySQL

I recently did a fresh install of Ubuntu 16.04. I also installed mysql-server and mysql-client (their default version 5.7.12) via apt-get install.

Because 5.7.12 was showing some issues, I decided to uninstall it. I tried apt-get remove mysql-server mysql-client first but saw that artifacts like /etc/mysql and /var/lib/mysql didn't get removed.

I then tried apt-get --purge removed mysql-server mysql-client, but still no difference.

So, I manually removed them (via rm -rf). I also manually removed all *mysql* files under /usr/ that came from any mysql-* dependency packages like mysql-common.

However, now, when I wish to re-install mysql-server and -client, I'm finding that I cannot.

In fact, now I'm in a state where I can neither apt-get remove mysql-server nor apt-get install mysql-server!

How do fix the situation I am in? If my local package repository metadata has become corrupted, how do I repair it?

I'd hate to reinstall the whole blessed OS with all my other apps and environment, one more time just because of mysql-server.

The following, for example, is the error I get when I try to remove mysql-server:

$ apt-get remove  mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libaio1 mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
  mysql-server
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
2 not fully installed or removed.
After this operation, 159 kB disk space will be freed.
Do you want to continue? [Y/n] 
(Reading database ... 237601 files and directories currently installed.)
Removing mysql-server (5.7.12-0ubuntu1) ...
Setting up mysql-server-5.7 (5.7.12-0ubuntu1) ...
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
invoke-rc.d: initscript mysql, action "start" failed.
dpkg: error processing package mysql-server-5.7 (--configure):
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 mysql-server-5.7
E: Sub-process /usr/bin/dpkg returned an error code (1)

And when trying to install, I get this error:

$ apt-get -f install mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  mysql-server
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
Need to get 0 B/10.1 kB of archives.
After this operation, 159 kB of additional disk space will be used.
Selecting previously unselected package mysql-server.
(Reading database ... 237599 files and directories currently installed.)
Preparing to unpack .../mysql-server_5.7.12-0ubuntu1_all.deb ...
Unpacking mysql-server (5.7.12-0ubuntu1) ...
Setting up mysql-server-5.7 (5.7.12-0ubuntu1) ...
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
invoke-rc.d: initscript mysql, action "start" failed.
dpkg: error processing package mysql-server-5.7 (--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.7; however:
  Package mysql-server-5.7 is not configured yet.

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)

Here's the output of apt-get install -f :

$ apt-get install -f 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
2 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up mysql-server-5.7 (5.7.12-0ubuntu1) ...
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
invoke-rc.d: initscript mysql, action "start" failed.
dpkg: error processing package mysql-server-5.7 (--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.7; however:
  Package mysql-server-5.7 is not configured yet.

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)

Best Answer

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).

Related Question