Install of mysql-server after mariadb fails

I faced the same issue trying to upgrade from MySQL to MariaDB on Ubuntu 17.04 It was impossible to install MariaDB because the post-install script thought it failed to start the server (timeout error but the server was started). It was impossible to revert to MySQL because the install script thought it couldn't stop the server even if it was actually stopped.

Uninstalling with apt is difficult and overkill since it tries to uninstall every packages depending on MySQL client libraries.

Here is my solution to revert to MySQL then perform upgrading to MariaDB.

1) Detect all MariaDB and MySQL packages

apt search mariadb | grep "\[install"

and

apt search mysql | grep "\[install"

2) Force uninstall of all MariaDB and MySQL packages (server, client, libs) to clean the mess

sudo dpkg --force depends --purge <package> <package> ...

3) Clean remaining data in /etc and /var/lib/mysql

NB: I first tried to install both MariaDB or MySQL with my config and my data files but failed each time.

sudo rm -rf /var/lib/mysql* /etc/mysql

If you want to stick to MySQL and don't have a copy of your /var/lib/mysql in /var/lib/mysql-5.7 from the first MariaDB upgrade atempt, you shoud just move /var/lib/mysql /var/lib/mysql-5.7 instead of removing it.

4) Fix the system

sudo apt-get --fix-broken install
sudo apt autoremove
sudo reboot

5) Get MySQL data back

The --fix-broken install had reinstalled a clean MySQL (not a MariaDB since Ubuntu seems to like it that way).

sudo service mysql stop
mv /var/lib/mysql*5.7 /var/lib/mysql
sudo service mysql start

If you want to go to step 6 you should backup all your databases now.

6) Finally upgrade to MariaDB

If you already had backups, you can skip step 5.

sudo apt-get install mariadb-server

Yes, it was what I tried at first, but this time it works! You can recreate your database users and then restore your databases since MariaDB doesn't want to upgrade MySQL 5.7 bases.