Out of the sudden, I am getting this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
on mysql when I run mysql -uroot.
I haven't touched web dev for around 1 month and as soon as I wanted to return to my project, I am getting this very annoying error out of nowhere. I have trie all advice and solutions from other answers and none of them work. This includes changing permissions to 777, ownership to root, mysql etc, killing PID all of those and none of them work. I even tried re-installing mysql and still getting the same error. I even re-installed my whole macOS Mojave and still getting the same error. I mean this error doesn't even make sense...
Is mysql completely broken or I am just super dumb?
Has anyone had this issue? If so how did you solve it?
The error says, it's using the socket /tmp/mysql.sock and failed.
Try to troubleshoot with the below things.
check the process status of mysqld and confirm its running or not. If running which socket is using.
ps -ef | grep -i mysqld
Based on the socket file try login to the MySQL through command line.
mysql -uroot -p -S <<file.sock>>
Check the my.cnf to make sure which socket file its mentioned under the [client] group option.
In your case, probably the mysql instance reading the socket from my.cnf files where it placed somewhere. Check the hierarchy of the configuration file usage by mysqld which may be located in either the /etc or /etc/mysql
I just installed MySQL 5.5.27 on WinXP. When I open a command prompt (Start -> Run, and type "cmd"), I can access MySQL by running "mysql -u root -p". However, when I open a Cygwin terminal and try the same thing, I get this error
$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql.sock' (2)
Indeed, there is no "/var/run/mysql.sock" file.
If you specify the host on the command line, this issue should go away:
mysql -u root -p -h 127.0.0.1
You can also create a my.ini that mysql will use:
echo [client] >c:\my.ini
echo user=root >>c:\my.ini
echo host=127.0.0.1 >>c:\my.ini
Then you can just type:
mysql -p
You can even add the password:
echo password="abracadabra" >>c:\my.ini
Then, just type:
mysql
and you're in!
See also https://serverfault.com/questions/337818/how-to-force-mysql-to-connect-by-tcp-instead-of-a-unix-socket
Try adding this to your command:
-h 127.0.0.1
The problem is that the mysql client default host is localhost, and it treats localhost specially, using a unix socket, which is accessed via that file, but your server may not be configured to listen on the unix socket.
However, if you access the same server via the loopback IP 127.0.0.1 it will use a TCP socket instead of the unix socket and (assuming the server is online) it should work.
Just to save few keystorkes,
Add following alias to your ~/.bashrc file.
alias mysql='mysql -u root -h 127.0.0.1'
After adding this, You can just type "mysql" in your terminal & there you go right inside mysql.
As most of people mentioned here - one of the solutions will be to use aliases. Don't like them to be honest because they are preventing me learning some really nice Linux commands :) It's just a joke. But the best way for you, as I think, will be to locate a ~/.bashrc file located in your home directory and put there:
alias mysql="mysql -h 127.0.0.1"
Don't forget that you have to restart your session in order for this solution to work or you may type bash command at the same terminal session - it will reload all your bash settings. Good luck!
P.S. I suggest you to close all other terminal windows before editing .bashrc because you may just got a read-only file. I had such issue under Win7x64
I successfully installed MySQL in Cygwin on my PC according to Rafael Hart. I created a database and performed some queries and everything worked great.
The next day when I tried logging into MySQL, I got the error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql.sock' (111 "Connection refused")
Apparently, when you shutdown your PC, the services also get shutdown and do not restart on boot.
To fix the problem, I typed the following command to restart the mysqld service:
$ mysqld_safe &
Then everything started working.
Here's how to run MYSQL from cygwin
Go here:
https://cygwin.rafaelhart.com/setting-up-mysql-on-cygwin/
To begin MySQL setup run the following:
mysql_install_db
Run mysql - you'll get a firewall alert from windows if you have it active.
mysqld_safe & Immediately following that, it would be wise to run the following:
mysql_secure_installation
I have tried all the solutions to access mysql that are described before in stackoverflow.
When I type mysql and the terminal asks for password,when I type password and press enter it says ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Any idea,this problem making me crazy.
Thanks in advance.
Make sure you're starting the mysql server first. The command is sudo mysqld_safe. Also try looking at my.cnf (likely in /etc/) and search for "socket". You may need to change the socket location to /tmp/mysql.sock.
After upgrading to Lion I can still start MySQL via System Preferences -> MySQL
When I try and login with the root user and password I was using on Snow Leopard I get this error via phpMyAdmin
#2002 Cannot log in to the MySQL server
Via the MySQL Query Browser however I get code 2003
If I try via Terminal with mysql -u root -p I get the error
-bash: mysql: command not found
Thanks for any help.
There was a problem with the socket and I had to change occurences of /var/mysql/mysql.sock to /tmp/mysql.sock as stated here http://birdchan.com/home/2011/07/20/osx-lion-mysql-sock-path/
Whenever I try to connect to MySQL to access phpmyadmin, it returns an error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (13)
I also tried to start MySQL through my server's terminal:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
When I restarted MySQL it displayed:
Stopping MySQL database server mysqld
df: `/var/lib/mysql/.': No such file or directory
df: no file systems processed
/etc/init.d/mysql: ERROR: The partition with /var/lib/mysql is too full!
How might I resolve this?
I've seen that a couple of times. It has meant that the actual MySQL server instance was down for some reason. It was fixed by a simple call to:
service mysql restart
Edit
I just noticed your comment The partition with /var/lib/mysql is too full!. This means your drive is too full to run MySQL. You need to either talk to your server administrator or just clean up the HD, but this will keep breaking until more room is available.
Normally this happens when you have don't have the mysql daemon started. most distros you can start it by typing in
/etc/init.d/mysqld start
and that should get you going. I think it can sometimes also be that after you did your install you will need to give root a password.
mysqladmin -u root password <enter password here>
You may want to check first that the dameon is atually running by doing a
ps -ef | grep mysql
Credit