31

After running the command mysql -v in Ubuntu, I am getting the following error:

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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Rohit Kumar
  • 311
  • 1
  • 3
  • 3

7 Answers7

70

I think this is because you are using client software and not the server.

  • mysql is client
  • mysqld is the server

Try:

Backup entire folder before doing this:

sudo rm /etc/apt/apt.conf.d/50unattended-upgrades*
sudo apt-get update
sudo apt-get upgrade

sudo service mysqld start

To check that service is running use: ps -ef | grep mysql | grep -v grep.

Uninstalling:

sudo apt-get purge mysql-server
sudo apt-get autoremove
sudo apt-get autoclean

Re-Installing:

sudo apt-get update
sudo apt-get install mysql-server
GreenROBO
  • 4,725
  • 4
  • 23
  • 43
Dmitri Sandler
  • 1,123
  • 6
  • 17
  • 24
    I am getting this error Failed to start mysqld.service: Unit mysqld.service not found – Rohit Kumar Dec 14 '16 at 16:37
  • How did you install it? Did you use `sudo apt-get install mysql-server`? – Dmitri Sandler Dec 14 '16 at 16:39
  • yes but got something like this: N: Ignoring file '50unattended-upgrades.ucf-old' in directory '/etc/apt/apt.conf.d/' as it has an invalid filename extension E: Sub-process /usr/bin/dpkg returned an error code (1) – Rohit Kumar Dec 14 '16 at 16:41
  • 1
    It looks like it did not get installed correctly. You will need to delete what is there and reinstall. Uninstalling and removing config by: `sudo apt-get purge mysql-server`. This is a good step to run Autoclean and autoremove: `sudo apt-get autoremove && sudo apt-get autoclean`. Not to install run: `sudo apt-get update && sudo apt-get install mysql-server`. Hope this helps. – Dmitri Sandler Dec 14 '16 at 16:49
  • getting same error again Errors were encountered while processing: mysql-server-5.7 mysql-server N: Ignoring file '50unattended-upgrades.ucf-old' in directory '/etc/apt/apt.conf.d/' as it has an invalid filename extension E: Sub-process /usr/bin/dpkg returned an error code (1) – Rohit Kumar Dec 14 '16 at 18:04
  • Backup folder. Remove `50unattended-upgr*` and re-run update & install. Updating all packages wouldn't hurt as well. See update to my answer. – Dmitri Sandler Dec 15 '16 at 02:10
  • 3
    If you dont want your data to be lost `sudo apt-get update sudo apt-get install mysql-server ` is enough – anjaneyulubatta505 Nov 02 '17 at 09:21
  • This worked for me, `sudo apt-get upgrade` triggered a message asking if I wanted to replace the deleted 50unattended-upgrades file with the manufacturer version. This defaults to no, so make sure you enter Y if you want that file to be replaced. – Dan Jan 27 '18 at 01:54
  • 3
    You should write this line "Backup entire folder before doing this:" above of the answer. because I read this line after uninstalling mysql but thank god I didn't had important database created – Yogesh Nikam Patil May 20 '19 at 09:38
  • i tried with restarting mysql service no worked , then reinstall mysql client dependency and reconfigure mysql . and restarted the service again , everything works fine , sounds weird but works ! root case : in my case while trying to remove python ..stupid command removed few dependency also . so had to reinstall those – Asraful May 21 '19 at 18:32
  • Worked for me. I had MySQL Workbench, but it said that the server wasn't running. It wasn't until I did the server install listed above that I could create schemas/tables etc. – CAMD_3441 Jul 05 '19 at 19:39
5

Yes, should try reinstall mysql, but use the --reinstall flag to force a package reconfiguration. So the operating system service configuration is not skipped:

sudo apt --reinstall install mysql-server
danilocgsilva
  • 187
  • 1
  • 13
5

By default, apt install MariaDB but not mySQL.

sudo apt install default-mysql-server

If you would like to install real mySQL database by apt, follow these steps.

Firstly, we need to remove all preceding mysql installed on the host.

sudo apt-get --purge autoremove "^mysql.*"

Dowload the stable package mysql-apt-config_0.8.15-1_all.deb from

sudo wget https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb

Install the downloaded release package:

sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb

Refresh your apt package cache to make the new software packages available

sudo apt update

Install MySQL by executing:

sudo apt-get update   
sudo apt-get install mysql-server mysql-client
libmysqlclient-dev

Check mysql status:

sudo service mysql status
Bean
  • 67
  • 7
Duc Toan Pham
  • 474
  • 6
  • 6
  • worked for me, you can change `sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb` for last version from `https://dev.mysql.com/downloads/repo/apt/` and just edit version number – Mreza Nemati Jul 13 '23 at 23:53
1

Try running

sudo apt-get install mysql-server 

if not installed.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1

I fixed by "purging" every file related to mysql and reinstalling mysql-server:

sudo apt-get purge mysql-server mysql-client mysql-common
sudo apt-get install mysql-server
supunbatagoda
  • 73
  • 1
  • 10
1

Navigate to /etc/systemd/system/ and do the following

  1. sudo rm -rf mysql.service
  2. sudo rm -rf mysqld.service
James Risner
  • 5,451
  • 11
  • 25
  • 47
0

I tried this also and more (re-installation etc.)

The answer was : I found query_cache_limit and removed it from the /etc/mysql/my.cnf file.

As soon as I removed it, the error log changed to a query_cache_size one so I removed that one too from my.cnf file. Within seconds, MySQL restarted successfully and I was back in business.

* Query Cache Configuration
query_cache_limit = 1M   
query_cache_size  = 16M 
* Logging and Replication
#
bad_coder
  • 11,289
  • 20
  • 44
  • 72