Ubuntu – Can’t get MYSQL8 to work on Ubuntu 20.04

20.04MySQLserver

I have purged and re-installed mysql-client and server a few times, I have rebooted my system and yet I cannot get mysql to work on my computer.

I ran these commands according to another askubuntu post:

sudo apt-get --purge remove mysql-server mysql-common mysql-client.

sudo apt update && sudo apt dist-upgrade && sudo apt autoremove.

sudo apt-get install -y mysql-server mysql-client

Then I rebooted and nothing happened.

Ouput of commands:

mysql -V

mysql  Ver 8.0.20-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

sudo systemctl status mysql.service

Unit mysql.service could not be found.

sudo systemctl start mysql.service

Failed to start mysql.service: Unit mysql.service not found.

sudo systemctl start mysqld.service

Failed to start mysqld.service: Unit mysqld.service not found

I am not sure where to go from here as I have removed and purged and mysql seems to be installed just not working.

mysql -h localhost -P 3306 -u root -p or
mysql -h 127.0.0.1 -P 3306 -u root -p

Same result:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

cat mysql.cnf                                                           1 ↵
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

Any help is appreciated. Thank you.

<——UPDATE——>
Just ran the commands recommended in comments and this is the output:

sudo apt-get install –reinstall mysql-server-8.0

Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded.
Need to get 0 B/1,228 kB of archives.
After this operation, 0 B of additional disk space will be used.
Preconfiguring packages ...
(Reading database ... 326219 files and directories currently installed.)
Preparing to unpack .../mysql-server-8.0_8.0.20-0ubuntu0.20.04.1_amd64.deb ...
Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
dpkg: warning: old mysql-server-8.0 package pre-removal script subprocess returned error exit status 1
dpkg: trying script from the new package instead ...
Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
dpkg: error processing archive /var/cache/apt/archives/mysql-server-8.0_8.0.20-0ubuntu0.20.04.1_amd64.deb (--unpack):
 new mysql-server-8.0 package pre-removal script subprocess returned error exit status 1
Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
Failed to preset unit: File mysql.service: Link has been severed
/usr/bin/deb-systemd-helper: error: systemctl preset failed on mysql.service: No such file or directory
Failed to start mysql.service: Unit mysql.service not found.
invoke-rc.d: initscript mysql, action "start" failed.
Unit mysql.service could not be found.
dpkg: error while cleaning up:
 installed mysql-server-8.0 package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
 /var/cache/apt/archives/mysql-server-8.0_8.0.20-0ubuntu0.20.04.1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

ls -laF /lib/systemd/system/mysql.service

-rw-r--r-- 1 root root 524 Nov  6 11:34 /lib/systemd/system/mysql.service

Got this error:: sudo 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.
1 not fully installed or removed.
Need to get 0 B/1,228 kB of archives.
After this operation, 0 B of additional disk space will be used.
dpkg: error processing package mysql-server-8.0 (--configure):
 package is in a very bad inconsistent state; you should
 reinstall it before attempting configuration
Errors were encountered while processing:
 mysql-server-8.0
E: Sub-process /usr/bin/dpkg returned an error code (1)

sudo dpkg –configure -a

dpkg: error processing package mysql-server-8.0 (--configure):
 package is in a very bad inconsistent state; you should
 reinstall it before attempting configuration
Errors were encountered while processing:
 mysql-server-8.0

Best Answer

Try

sudo apt-get install --reinstall mysql-server-8.0

and check if mysql.service exists

ls -laF /lib/systemd/system/mysql.service

To fix broken packages

sudo apt-get install -f
sudo dpkg --configure -a

Fix mysql.service that may have been left over from an old installation

sudo killall mysqld
sudo systemctl disable mysql.service
sudo rm -rf /lib/systemd/system/mysql.service
sudo rm -rf /usr/lib/systemd/system/mysql.service
sudo rm -rf /etc/systemd/system/multi-user.target.wants/mysql.service
sudo rm -rf /var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/mysql.service
sudo rm -rf /var/cache/apt/archives/mysql-server-*.deb

sudo apt-get install -f
sudo dpkg --configure -a

sudo apt-get install --reinstall mysql-server-8.0

Related Question