61

I'm getting this error

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

even though I have managed to start mysql via command line in ubuntu

mysql stop/waiting
mysql start/running, process 17691

However when attempting to access the site I get a database connection error as well as the above error when trying to access mysql via mysql -u root -p

I checked my error logs and I saw this

    131029 12:53:34 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be remo$
    131029 12:53:34 [Note] Plugin 'FEDERATED' is disabled.
    131029 12:53:34 InnoDB: The InnoDB memory heap is disabled
    131029 12:53:34 InnoDB: Mutexes and rw_locks use GCC atomic builtins
    131029 12:53:34 InnoDB: Compressed tables use zlib 1.2.3.4
    131029 12:53:34 InnoDB: Initializing buffer pool, size = 26.0G
    131029 12:53:36 InnoDB: Completed initialization of buffer pool
    131029 12:53:36 InnoDB: highest supported file format is Barracuda.
    131029 12:53:38  InnoDB: Waiting for the background threads to start
    131029 12:53:39 InnoDB: 5.5.34 started; log sequence number 5146431500
    131029 12:53:39 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
    131029 12:53:39 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
    131029 12:53:39 [Note] Server socket created on IP: '0.0.0.0'.
    131029 12:53:39 [Note] Event Scheduler: Loaded 0 events
    131029 12:53:39 [Note] /usr/sbin/mysqld: ready for connections.
    Version: '5.5.34-0ubuntu0.12.04.1-log'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)

It's the first time I see this error and I'm not sure how to solve this issue, please help me out a bit here.

Thanks

UPDATE

Okay I tried glglgl's solution and after a restart, I get the following in the error log:

    131029 13:17:36 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be remo$
    131029 13:17:36 [Note] Plugin 'FEDERATED' is disabled.
    131029 13:17:36 InnoDB: The InnoDB memory heap is disabled
    131029 13:17:36 InnoDB: Mutexes and rw_locks use GCC atomic builtins
    131029 13:17:36 InnoDB: Compressed tables use zlib 1.2.3.4
    131029 13:17:36 InnoDB: Initializing buffer pool, size = 26.0G
    131029 13:17:38 InnoDB: Completed initialization of buffer pool
    131029 13:17:38 InnoDB: highest supported file format is Barracuda.
    131029 13:17:40  InnoDB: Waiting for the background threads to start
    131029 13:17:41 InnoDB: 5.5.34 started; log sequence number 5146431500
    131029 13:17:41 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306
    131029 13:17:41 [Note]   - '127.0.0.1' resolves to '127.0.0.1';
    131029 13:17:41 [Note] Server socket created on IP: '127.0.0.1'.  
    131029 13:17:41 [Note] Event Scheduler: Loaded 0 events
    131029 13:17:41 [Note] /usr/sbin/mysqld: ready for connections.
    Version: '5.5.34-0ubuntu0.12.04.1-log'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)
6

36 Answers 36

67

My problem was solved checking if the process was running on Ubuntu 12.04

ps ax | grep mysql

Then the answer was that it wasn't running, so I did

sudo service mysql start

Or try

sudo /etc/init.d/mysql start
1
  • 8
    there's no mysql in etc/init.d!
    – Rodrigo
    Commented Jul 23, 2015 at 15:51
52

Firstly, please confirm mysql-server is installed. I have the same error when mysql-server is installed but corrupted somehow. I do the trick by uninstall mysql completely and reinstall it.

sudo apt-get remove --purge mysql*
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get install mysql-server mysql-client
3
  • after updating ubuntu my installation got corrupted; clean installation gave me confidence
    – mumair
    Commented Aug 25, 2018 at 11:04
  • this worked for me although the first three commands for cleaning where not necessary
    – ze_iliasgr
    Commented Nov 9, 2018 at 12:35
  • i think this will remove all databases . orleady create try to do it with precaution Commented May 28 at 3:54
17

This took me ages to figure out but could you try the same -

1 - Update and Upgrade

sudo apt-get update
sudo apt-get upgrade

2 - Purge MySQL Server and Client (if installed).

sudo apt-get purge mysql-server mysql-client

3 - Install MySQL Server and Client fresh

sudo apt-get install mysql-server mysql-client

4 - Test MySQL

mysql -u root -p
> enter root password

should get socket not found in /var/run/mysqld/mysql.sock

4 - Configure mysqld.cnf with nano of vi

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Change bind-address from 127.0.0.1 to localhost

bind-address            = localhost

** Write and Exit

5 - Restart MySQL

sudo /etc/init.d/mysql restart

6 - check mysql

mysql -u root -p
> enter root password

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.13-0ubuntu0.16.04.2 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

You should get in to mysql cli now.

Hope this helps

Code.Ph0y

15

I had the same problem when i installed xampp on my system. The mysql server looks for /var/run/mysqld/mysqld.sock but the mysql.sock file was in xampp folder so i used

 find / -name '*.sock'

to find the mysql.sock file and then used

ln -s <the file location> /var/run/mysqld/mysqld.sock

to get a link for the *.sock file then tried mysql and it ran without error. Hope this could solve yours.

Remember to create the directory if it does not exists.

1
  • 2
    Also should use sudo when access denied (for creating the folder and the symbolic link)
    – Enrique
    Commented May 2, 2016 at 15:58
13

The client should be set consistent with server setting.

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
2
  • 2
    @DaveWalley I assume he is referring to the my.cnf file
    – Mark
    Commented Aug 28, 2014 at 5:59
  • 2
    Sure, and usually it's located in /etc/my.cnf or /etc/mysql/my.cnf
    – feuyeux
    Commented Sep 1, 2014 at 2:25
12

You don't need to reinstall MySQL server I was facing the same issue but I have resolved the issue by running these commands.

  1. ps -A|grep mysql
  2. sudo pkill mysql
  3. ps -A|grep mysqld
  4. sudo pkill mysqld
  5. sudo service mysql restart
  6. sudo mysql -u root -p
0
7

recently i have faced with this error in my mysql ERROR 2002 (HY000):

Can't connect to local MySQL server through socket

'/var/lib/mysql/mysql.sock' (111)

because i forget to start my mariadb. just type this command on ur terminal.

systemctl start mariadb.service
0
6

In the linux terminal, I run the following steps and they helped me out

1.First of all list all the mysql packages installed

  sudo dpkg -l | grep mysql

2.Delete the listed packages with the listed command

  sudo apt-get --purge autoremove <package name>

3.Reinstall mysql server

  sudo apt-get install mysql-server

This worked for me, Hope this works for you aswell.

Alternatively Try

sudo apt-get remove --purge mysql-\

then reinstall with

sudo apt-get install mysql-server mysql-client
1
  • still getting ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) Commented Apr 24 at 6:13
5

I deleted everything connected to mysql with

sudo apt-get remove --purge --auto-remove mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server-5.7

sudo rm -r /etc/mysql* /var/lib/mysql* /var/log/mysql*

and then reinstalled it again with

sudo apt-get update

sudo apt-get install mysql-server

and then started it with

mysql -u root -p

followed by the password

0
4

I have follow Wellington Lorindo posting. And My problem was solved.

Steps 1. run in terminal

ps ax | grep mysql

Result was

11200 ? Ssl 0:01 /usr/sbin/mysqld

11514 pts/0 S+ 0:00 grep mysql

Steps 2. Again type this

sudo service mysql start

And problem solved.

Thanks Wellington Lorindo

0
3

You may be missing mysql-server. install it using

sudo apt-get install mysql-server
1
  • 1
    "even though I have managed to start mysql via command line in ubuntu"
    – Martyn
    Commented Nov 1, 2017 at 13:22
3

This is an old tread with different solution proposed, but none of them worked for me. I'm adding this answer in the hope it will help somebody who struggled as me for some time.

I've checked all the existing answers and all the existing solution, but for me the issue was incorrect user permission on var/run/mysql folder.

I've checked the user permission on that folder and it was set to root. Once i changed to mysql:mysql the problem has gone.

So enter into /var/run/mysqld and change the user permission to:

chown -R mysql:mysql .
0
3

I had the problem.

This process worked for me:

Ensure that during remove process you choose No to deleting database.

sudo apt-get remove --purge mysql*

sudo apt-get autoremove

sudo apt-get autoclean

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

If you look close, you'll see the difference:

  • The server listens on /var/run/mysqld/mysqld.sock
  • The client tries to connect to /var/run/mysqld/mysql.sock

You'll have to adjust the one or the other.

1
  • 1
    I'm able to access the website and access the database but I can't access it via command line, still gets the same error Commented Oct 29, 2013 at 13:20
2

I had the same problem after altering my.cnf. I resolved this problem next steps.

First : to find the location of error.log, look at 'my.conf'.

Second : I looked up error.log, I found next massages,

"unknown variable 'default-......' " ..... " mysqld_safe Starting mysqld darmon with from /var/lib/mysql "

Third : I reset my.conf to the default, then restart, this problem was resolved.

I hope this helps.

2

The same error occured with my system as well. I had to remove mysql-server and reinstall it. Not other way around. Try running these commands it it works:

  1. sudo apt-get purge mysql-server
  2. sudo apt-get autoremove
  3. sudo apt-get install mysql-server
0
2

this worked for me

sudo /etc/init.d/mysql start

first i tried this, but i dont understand why it doesn't work this way

sudo service mysql start
2

On Debian this was a bind problem for me so changing bind-address from localhost to 0.0.0.0 helped.

vim /etc/mysql/my.cnf 

bind-address = 0.0.0.0

1
  • Faced the same on Ubuntu, our service team has moved the server and changed the IP address and MySQL started to fail after turning the machine on. I have updated the IP address in /etc/mysql/my.cnf and then did /etc/init.d/mysql start as most of the answers suggest here.
    – RAM237
    Commented Jul 30, 2018 at 9:19
1

I also met this problem. mysql -u root -p Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)

Then I find the reason is my selinux is enforcing, I disable it then restart mysqld , it works, hope helpful.

1
  • Which Distro of linux were you running? You mention selinux so I am guessing CentOS .... I am running Ubuntu and experiencing the same problem..
    – kenju254
    Commented Apr 27, 2014 at 19:46
1

There is another chance of you to get this particular error if you modify the max_connections in my.cnf file.

I also faced the same error which did not let me log in to MySQL server. In my case the problem was, I had to increase the max_connections size in

/etc/mysql/my.cnf

to

set-variable = max_connections = 5000

Due to this reason next day when I stared MySQL server it threw this error

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

Removing the set-variable = max_connections = 5000 segment from my.conf file did the trick. After removing that particular segment following command was used. MySQL server started up and running like previously.

sudo /etc/init.d/mysql start
1

This problem will occurred due to some unnecessary changes made in my.cnf file. Try to diff /etc/mysql/my.cnf with one of working mysql servers my.cnf file.

I just replaced /etc/mysql/my.cnf file with new my.cnf file and this works for me.

1
  • i just delete rm /etc/mysq/my.cnf and eveyting works fine Commented Mar 17, 2023 at 3:16
1

Try this:

sudo dpkg-reconfigure mysql-server-{version_number}

version_number should be mysql version number. For example

sudo dpkg-reconfigure mysql-server-5.6

Then access mysql like this:

mysql -u root -p
1

If you are using wsl on windows 10 with ubuntu you can run:

  • sudo /etc/init.d/mysql start the command sudo service start mysql wont work because you need to configure it. You can find how here
0

Two main reasons:

1) mysql-server isn't installed! You have to install it (mysql-server and not mysql-client) and run it.

2) It's installed by default and running. So, it's not possible to run it again through Xampp or Lampp. You have to stop it: sudo service mysql stop

then you can start it through Xampp. It's possible to check if it's running or not with this code: sudo netstat -tap | grep mysql

If you see a result like this: tcp 0 0 localhost:mysql : LISTEN 1043/mysqld

It means that it's running properly.

0

make sure the bind address configuration option in the my.cnf file in /etc/ is the same with localhost or virtual serve ip address. plus check to make sure that the directory for creating the socket file is well specified.

0

This provides an alternative solution if the issue relates to you. If your disk drive is full than the MYSQL server (and most other programmes) is unable to start. Delete files and folders to allow Mysql-server to start up.

0

Simple: run these codes:::

 1:: ls -lart/var/run/my*

   2::mkdir /var/run/mysqld

    3::touch /var/run/mysqld/mysqld.sock
    4:ls -lart /var/run/mysqld
    5::chown -R mysql /var/run/mysqld
    6::ls -lart /var/run/mysqld
REstart your mysql server

then finaly type mysql -u root or mysql -u root -p and press enter key. thanks

0

In my case the problem was that I had added "default-character-set=utf8" to my.cnf and that was corrupting the file. Instead change that line for "character_set_server=utf8". That should solve it, hope this helps.

0

Note that I ran into this trouble after I upgraded my Ubuntu 14.04 distribution using apt-get upgrade. I had to completely purge mysql-server and mysql-client and reinstall it to fix this issue.

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

Hope this helps.

0

I was stuck with this error too, and it took almost 3 hours to find it.

First of all, try to recall the changes that you made to your conf file, then undo the changes and save the file and start mysql. If you want to make any changes to the conf file, then stop mysql server and then change the desired values and restart mysql.

This error occurs because of an incorrect (wrong keyword or wrong value assigned) change made to the conf file. Most of the times this error occurs even if you restart the machine running the server, as the server is stopped, in that case, start mysql server.

Not the answer you're looking for? Browse other questions tagged or ask your own question.