Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm just new to mysql so I apologize for any silly questions. My problem started off with me not being able to log in as root anymore into my mysql install. I was attempting to run mysql without passwords turned on... but whenever I would run the command :

    # mysqld_safe --skip-grant-tables &

I would never get a prompt back... (I was trying to follow the instructions here: http://www.cyberciti.biz/tips/recover-mysql-root-password.html) The screen just looks like this:

 root@jj-SFF-PC:/usr/bin# mysqld_safe --skip-grant-tables
 120816 11:40:53 mysqld_safe Logging to syslog.
 120816 11:40:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

and I don't get a prompt to start typing the sql commands to reset the password.

When I kill it by doing a CTRL + C, I get the following message:

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

Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

If I retry the command and leave it long enough, I do get the following series of messages:

root@jj-SFF-PC:/run/mysqld# 120816 13:15:02 mysqld_safe Logging to syslog.
120816 13:15:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
120816 13:16:42 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

[1]+  Done                    mysqld_safe --skip-grant-tables
root@jj-SFF-PC:/run/mysqld#

But then if i try to log in as root by doing:

# mysql -u root

I get the following error message:

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

I checked and /var/run/mysqld/mysqld.sock file doesn't not exist. The folder does, but not the file.
Also, I dunno if this helps or not, but I found did a find / -name mysqld and it came up with:

/var/run/mysqld - folder
/usr/sbin/mysqld - file
/run/mysqld - folder

I'm just new to linux and mysql, so i don't know if this is normal or not. But I'm including this info just in case it helps ...

** I finally decided to uninstall and reinstall mysql. **

apt-get remove mysql-server
apt-get remove mysql-client
apt-get remove mysql-common
apt-get remove phpmyadmin

After resintalling all packages again in the same order as above, during the phpmyadmin install, it gave me the same error -

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

So I tried again to uninstall/reinstall. This time, after I uninstalled the packages, I also manually renamed all mysql files and directories to mysql.bad in their respective locations.

 /var/lib/mysql 
 /var/lib/mysql/mysql
 /var/log/mysql
 /usr/lib/perl5/DBD/mysql
 /usr/lib/perl5/auto/DBD/mysql
 /usr/lib/mysql
 /usr/bin/mysql
 /usr/share/mysql
 /usr/share/dbconfig-common/internal/mysql
 /etc/init.d/mysql
 /etc/apparmor.d/abstractions/mysql
 /etc/mysql

Then I tried to reinstall mysql-server, mysql-client again. But I've noticed that it doesn't prompt me for a password. Isn't it supposed to ask for an admin password?

share|improve this question
    
I got the same problem. I solved it just now. stackoverflow.com/questions/18150858/… – robert Aug 9 '13 at 16:28
    

23 Answers 23

up vote 53 down vote accepted

To find all socket files on your system run:

sudo find / -type s

My Mysql server system had the socket open at /var/lib/mysql/mysql.sock

Once you find where the socket is being opened, add or edit the line to your /etc/my.cnf file with the path to the socket file:

socket=/var/lib/mysql/mysql.sock

Sometimes the system startup script that launched the command line executable specifies a flag --socket=path. This flag could override the my.cnf location, and that would result in a socket not being found where the my.cnf file indicates it should be. Then when you try to run the mysql command line client, it will read my.cnf to find the socket, but it will not find it since it deviates from where the server created one. So, Unless you care where the socket resides, just changing the my.cnf to match should work. I

If you're super user in the linux system, based on the above just do this:

kill -9 4969

or sometimes you can do this:

pkill -9 mysqld

After you do this you might want to look for a pid file in /var/run/mysqld/ and delete it

Make sure the permissions on your socket is such that whatever user mysqld is running as can read/write to it. An easy test is to open it up to full read/write and see if it still works:

chmod 777 '/var/run/mysqld/mysqld.sock

If that fixes the issue, you can tailor the permissions and ownership of the socket as needed based on your security settings.

Also, the directory the socket resides in has to be reachable by the user running the mysqld process.

share|improve this answer
2  
Please don't recommend using kill -9; there are several ways that should be tried before it has to come to that. – Ignacio Vazquez-Abrams Aug 16 '12 at 16:17
    
@dot run this in file root 'find . -name mysqld.sock'. I have seen the socket file in other places than /var/run. Once you find it you might need to edit your my.cnf file to point to where it is found. – Ray Aug 16 '12 at 17:57
    
@ray, i tried to run the original command in a different way (i found a ubuntu specific post on another site..). Here's the commands I ran: "sudo service mysql stop" and "sudo mysqld --skip-grant-tables". After I do the second command, i get the following error: "InnoDB: Unable to lock ./ibdata1, error: 11". I can't find this ./ibdata1 file... InnoDB: Check that you do not already have another mysqld process – dot Aug 16 '12 at 18:24
    
@dot run ps -aux | grep mysqld and see if any other mysql processes are running – Ray Aug 16 '12 at 19:05
    
@ray. i'm rippng my hair out man. i decided to blow mysql away and retry... note my updated post. – dot Aug 16 '12 at 19:36

Try this command,

sudo service mysql start
share|improve this answer
    
+1 my.cnf was pointing to the right directories, by mysql service had died – wruckie May 10 '14 at 12:10
1  
it works. thanks a lot – adarshaU Oct 20 '14 at 14:40
    
I tried the accepted answer but couldn't find the mysql.sock, this worked for me. Thank you – Giovanne Afonso Feb 23 '15 at 5:59
    
its worked for me. Thank you.. – Udhayakumar Sep 8 '15 at 13:57
    
I had to use sudo service mysqld start – Gavin Miller Dec 9 '15 at 18:46

The solution is way easier.

  1. First, you have to locate(in Terminal with "sudo find / -type s") where your mysql.sock file is located. In my case it was in /opt/lampp/var/mysql/mysql.sock
  2. Fire up Terminal and issue sudo Nautilus
    This starts your Files manager with super user privileges
  3. From Nautilus navigate to where your mysql.sock file is located
  4. Right click on the file and select Make Link
  5. Rename the Link File to mysqld.sock then Right click on the file and Cut it
  6. Go to /var/run and create a folder called mysqld and enter it
  7. Now right click and Paste the Link File
  8. Voila! You will now have a mysqld.sock file at /var/run/mysqld/mysqld.sock :)
share|improve this answer
    
How do you locate your mysql.sock file? I tried find / -name 'mysql.sock' but it returns nothing. – Matthew Lock Mar 26 '13 at 3:11
2  
@MatthewLock First off, make sure that mysql is running (by "ps aux | grep mysql"), otherwise, no mysql.sock would appear. Second, use "sudo find / -type s", which gives you all sockets on your system to locate mysql.sock – narengi Jun 29 '13 at 11:34
    
Report: This worked for me (although with different directories, since I'm using a different distribution): here's what I did: 1- checked at what location mysql tries to access the socket file (by reading the log file). For me, it was /usr/var/mysqld/mysqld.sock 2- made sure mysqld is running (by running /where/your/mysql/is/mssqld_safe) 3- searched for the location where mysql socket now sits (by sudo find / -type s let's just say it's on /tmp/mysql.sock) 4- made a soft link from where-it-is to where-it's-to-be (by sudo ln -s /usr/var/mysql/mysql.sock /tmp/mysql.sock) – narengi Jun 30 '13 at 8:26
4  
What if the output of sudo find / -type s does not contain any mysql.sock? – Halil Pazarlama Feb 25 '15 at 15:16
1  
It works. But I have no idea why these developers never do this job in past years? – Robert Yi Jiang Aug 24 '15 at 1:12

This error occurs due to multiple installations of mysql. Run the command:

ps -A|grep mysql

Kill the process by using:

sudo pkill mysql

and then run command:

ps -A|grep mysqld

Also Kill this process by running:

sudo pkill mysqld

Now you are fully set just run the following commands:

service mysql restart
mysql -u root -p

Have very well working mysql again

share|improve this answer
    
Hey folks, I was running all over the web trying to fix this. My problem was that I was trying to setup docker. I wanted it to use the mysql data dir that was previously used by mysql outside of docker... that ps command was the best! – Michael Nov 13 '14 at 16:14
1  
I faced same problem .This solution helped me.Thanks man – Suraj Palwe Mar 2 '15 at 17:14

I faced same error and found that it was due to upgradation of packages, So after restarting my system I resolved error.

I think due to sql libraries/ packages update that error occured, So try this if you are doing some upgrading :)

share|improve this answer

Okay just copy and paste these codes: This should be done in the terminal, inside a server, when your mysql database is not properly installed, and when you are getting this error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'.

Stop MySql

sudo /etc/init.d/mysqld stop

Restart it or start it

sudo /etc/init.d/mysqld restart or sudo /etc/init.d/mysqld start

Make a link like this and give it to the system

ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

Run a secure installation which guides all the process you need to do to configure mysql

/usr/bin/mysql_secure_installation
share|improve this answer

There is a lots of reason for this issue, but sometimes just restart the mysql server, it will fix the issue.

sudo service mysql restart
share|improve this answer

The answer of the user load step worked for me. Sometimes is need edit the file in /etc/mysql/my.cnf add line to client

[client]
password = your_mysql_root_password
port  = 3306
host  = 127.0.0.1
socket  = /var/lib/mysql/mysql.sock
share|improve this answer
    
This gives me: mysql: unknown variable 'Host=127.0.0.1' – Splaktar Mar 19 '14 at 13:54
    
@Splaktar: there's a typo. Host need to be lowercase host and it will work. Also, this solution worked for me. – Mohammad Faisal Apr 18 '14 at 9:10

Maybe someone facing this problem. I am using Mysql Workbench on Ubuntu 14 and got this error.

mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when trying to connect

Find your socket file by running sudo find / -type s, in my case it was /run/mysqld/mysqld.sock

So, I just created a link to this file in tmp directory.

sudo ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
share|improve this answer

you can find mysqld.sock in /var/run/mysqld if you have already installed mysql-server by sudo apt-get install mysql-server

share|improve this answer

Changing the host to 127.0.0.1 worked for me.

Edit the file in /etc/mysql/my.cnf and add the below mentioned line to the section: client

[client]
port  = 3306
host  = 127.0.0.1
socket  = /var/lib/mysql/mysql.sock

After you are done with it. Execute the following command.

sudo service mysql start
share|improve this answer
    
This my friend, helped me :) – N Bhargav May 5 '15 at 12:00
    
@NBhargav i am so happy that u could solve ur problem .. Cheers buddy .. 😇😇 – Dulith De Cozta May 5 '15 at 12:41

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

in /etc/my.cnf add this lines:

[client]
socket=/var/lib/mysql/mysql.sock <= this path should be also same as is[mysqld]

And restart the service with: service mysql restart

this worked for me

share|improve this answer

In My case two mysqld processes were running.. killed the optional processs by using pkill -9 mysqld

share|improve this answer
    
I needed to add the 'sudo' kill the process. – noti Jan 20 '14 at 15:02

If you have a lot of databases and tables on your system, and if you have innodb_file_per_table set in my.cnf, then your mysql server might have run out of opened objects / files (or rather the descriptors for these objects) Set a new max number with

open-files-limit = 2048

and restart mysql. This approach might help when the socket is not created at all, but really this might not not be the real problem, there is an underlying problem.

share|improve this answer

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

solutions

finally uninstall and reinstall mysql. **

  • sudo apt-get remove mysql-server
  • sudo apt-get remove mysql-client
  • sudo apt-get remove mysql-common
  • sudo apt-get remove phpmyadmin

then install again by

  • sudo apt-get install mysql-server-5.6

After this operation, 164 MB of additional disk space will be used.

  • Do you want to continue? [Y/n] Y press YES for complete installations

...... .......

  • At last you will get these lines....

    Setting up libhtml-template-perl (2.95-1) ...

    Setting up mysql-common-5.6 (5.6.16-1~exp1) ... Processing triggers for libc-bin (2.19-0ubuntu6) Processing triggers for ureadahead (0.100.0-16) ...

  • And then

    root@ubuntu1404:~# mysql -u root -p (for every password first u should use )

  • Enter password:

  • Note :Entered password should be same as the installation time password of mysql(like .root,system,admin,rahul etc...)

    Then type

  • USE rahul_db(database name);

Thanks.**

share|improve this answer
    
please improve the answer quality – Muhammed Athimannil Oct 3 '14 at 13:55

I run my MySQL on a virtual machine in Ubuntu, So what had happened was when I restarted my host and the VM, The IP had changed. I had configured mysql to run on IP 192.168.0.5 and now due to dynamic allocation of IP, my new IP was 192.168.0.8

If you have the same problem just check your ip with the command ifconfig.

Check your MySQL binding with the command cat /etc/mysql/my.cnf | grep bind-address

If both IP are the Same, then reinstall your mysql server

If not, then change your IP in /etc/network/interfaces using nano, vi, vim or anything of your preference.

I prefer sudo nano /etc/network/interfaces

and enter the following

auto eth0
iface eth0 inet static
address 192.168.0.5
netmask 255.255.255.0

Save the interfaces file, restart your interface sudo ifdown eth0 && sudo ifup eth0 replace "eth0" with your network interface

Restart MySQL sudo service mysql stop followed by sudo service mysql start

If you have the same issue as mine, You are good to go!

share|improve this answer

I just had this problem on Ubuntu 14.10

Turns that mysql-server was no longer installed (somehow it had been removed) but I couldn't just install it because there were some broken packages and dependency issues/conflicts.

In the end I had to reinstall mysql

sudo apt-get remove mysql-client
sudo apt-get install mysql-server
share|improve this answer

I would also check the mysql configuration. I was running into this problem with my own server but I was a bit too hasty and misconfigured the innodb_buffer_pool_size for my machine.

innodb_buffer_pool_size = 4096M

It usually runs fine up to 2048 but I guess I don't have the memory necessary to support 4 gigs.

I imagine this could also happen with other mysql configuration settings.

share|improve this answer

This was mentioned a couple of times already, but this worked immediately for me:

service mysql restart

share|improve this answer

This solution can help others, sometimes the fix is very simple.

In your live environment restart mysql server and apache

sudo service mysql restart
sudo apache services restart

this is a common problem when you install a plugin or execute any database commands.

share|improve this answer

locate the my.cnf file with the mysql config mine was under /etc/mysql/my.cnf (change the location where it says socket accordingly)

port            = 3306
socket          = /tmp/mysql.sock

Here is entries for some specific programs The following values assume you have at least 32M ram

This was formally known as [safe_mysqld]. Both versions are currently parsed.

[mysqld_safe]
socket          = /tmp/mysql.sock
nice            = 0
share|improve this answer

just type this code in terminal sudo opt/lampp/bin/mysql -u root or sudo opt/lampp/bin/mysql -u root -p if you have set a password..

share|improve this answer

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

Sometimes it is due to when mysql server goes down , in live server i think you should have to restart server and also in localhost you need to restart mysql server .

share|improve this answer

protected by Community Jul 23 '15 at 8:39

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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