On a CentOS 7 machine, I am getting the following error when I try to log into mysql from the terminal:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
I then tried cd /var/lib/mysql
and ls -al
, but the resulting list of files did not contain mysql.sock
. So where can I find mysql.sock
and how can I resolve this error?
Here are a number of things I have tried already:
1.) When I saw that systemctl status mysqld
showed that mysql was stopped, I also tried systemctl start mysqld
but the terminal just gave an unresponsive cursor without a new prompt, as if it were held up waiting for something, but it remained in that limbo state indefinitely.
2.) So I opened another terminal window and ran systemctl status mysqld
, only to see that mysqld was again started. But the error persists.
3.) I tried find / -name "mysql.sock"
but the response was find: ‘/run/username/1000/gvfs’: Permission denied
.
4.) Then I read this posting and tried mysqladmin | grep d.sock
but there were no results.
5.) I even resorted to yum remove mysql
followed by yum install mysql
, but then mysql -u root
still gives the same original error at top of this posting.
6.) And then I read this other posting, but mysqladmin variables
gives the same error that it cannot connect because the mysql.sock
cannot be found.
7.) And I tried ln -s /tmp/mysql.sock /var/mysql/mysql.sock
but the same error persisted even though a link to mysql became visible when I tried cd /var/lib/mysql/
and ls -al
8.) Since the problem may have to do with the fact that systemctl start mysqld
gets hung up, I read this posting and tried chown -R mysql:mysql /var/lib/mysql
and systemctl start mysqld
but the result is Failed to issue method call: Unit mysqld.service failed to load: No such file or directory.
9.) I then read this posting, which caused me to run mysql --help | grep "Default options" -A 1
. The result printed the locations where mysql
looks for my.cnf
. I thus checked every location, and there was no my.cnf
file to be found. But I did find a mysql.cnf.rpmsave
, so I tried mv mysql.cnf mysql.cnf.rmpsave
. This caused there to be a my.cnf
in the expected directory, but systemctl status mysqld
says that mysqld
is not found. I tried yum install mysql
again, but it told me that mysql
is already installed. So I suspect that the newly renamed my.cnf
is corrupted. If so, how can I fix it? Here is the link to a copy of the newly renamed my.cnf
on a file sharing site.
The MySQL server's socket file is named mysqld. sock and on Ubuntu systems it's usually stored in the /var/run/mysqld/ directory. This file is created by the MySQL service automatically.
In the command line, run the following command: mysql -u root -p -S /var/run/mysqld/mysql. sock . Type a password for your root user and press Enter .
mysqld. sock is created by the mysql daemon on start, and it is removed (or, becomes un-connectable) if this daemon stops. This message is given by some client process, trying to connect your mysqld on this socket without success. Its most likely reason is that the mysqld doesn't run.
Realise this is an old post, but came across this myself just now and found it very hard to find the proper solution. I solved it by doing the following:
Install zlib
:
32-Bit Debian/Ubuntu: apt-get install zlib1g
64-Bit Debian/Ubuntu: apt-get install lib32z1
32/64-Bit Fedora: yum install zlib.i686
32/64-Bit Mandriva: urpmi zlib1
32-Bit SUSE: zypper install libz1
64-Bit SUSE: zypper install libz1-32bit
Connect to 127.0.0.1
instead of localhost
if that option is available to you. With MySQL connecting via 127.0.0.1
and localhost
use different methods (TCP for the former, Sock for the latter)
Make sure your firewall isn't blocking ports internally, perhaps consider disabling it temporarily to make sure this isn't the case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With