Put your message here! Contact me for more information
 
 








 

I was trying to setup a MySQL cluster with CentOS 4.4 and VMWare and I was initially successful at getting the NDB storage engine to work. However, I could not start the MySQL API (or MySQL client, whatever you call it) because I ran into the infamous MySQL error of “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)“. Googling didn’t help much either because people were just asking and very few got the answer.

After a while, I was desparate. My cluster was up and I had no way to connect to it and test out the exciting cluster’s replicate feature. I went to bed at 4:00am last night and kept thinking what could go wrong? Setting up the ndb engine for the nodes was pretty straightforward but why just getting mysql client to run is so problematic? Well, here’s my solution to the puzzle:

My Setup

  • CentOS 4.4 installed from DVD with most packages unchecked, especially MySQL
  • The newest MySQL 5.0.27-max was used. See, I’m on the bleeding-side of technology.
  • MySQL is locaed in the default location at /usr/local/mysql/
  • When I installed MySQL, the mysql.server script was cp from /usr/local/mysql/support-files/mysql.server to /etc/init.d/
  • The my.cnf file is very simple like this

#file: /etc/my.cnf
ndbcluster
ndb-connectstring=’host=192.168.2.100′
[mysql_cluster]
ndb-connectring=’host=192.168.2.100′

Cause:

When the MySQL server (mysqld) starts, it created a sock file. Since I overwrote the original /etc/my.cnf without the socket parameter under [mysqld], the MySQL server (mysqld) created the socket file under /tmp/mysql.sock instead. Furthermore, I was also missing the socket parameter under [client] section in the my.cnf - I didn’t even have the [client] section.

What happens when I tried to start the mysql client (#mysql -u root) was that the mysql client was looking for the mysql.sock file but coudln’t find the path to the file. It then looked into the default path /usr/lib/mysql/ to find the mysql.sock file. Of course this doesn’t work because when mysqld was started, the socket file was created in /tmp instead. The client gave up, spit out the connect error prompt.

In brief, the cause of the infamous error is because the mysql client cannot find the socket file at run time.

Solutions

First of all, you should verify that mysqld is running by typing #ps aux | grep [m]ysqld. Then there are 2 ways to solve this: one is to fix the socket path in the /etc/my.cnf under [mysqld] and [client] like this

# file: /etc/my.cnf
[mysqld]
… # someother settings
socket=/var/lib/mysql/mysql.sock
[client]
… # some other settings
socket=/var/lib/mysql/mysql.sock

then restart the mysqld server so that the mysql.sock file is generated. The next time you start the client with #mysql -u root, you will magically get it. This fix is permanently but the catch is you will have to shutdown mysqld, the MySQL server itself.
The other way when you want to just login really fast to the mysql server, try this (actually I used this method to obtain the overall solution to the infamous error)

  • #find / | grep mysql.sock
    this will do a look up on the entire disk and look for the mysql.sock file. You will get back something like /tmp/mysql.sock. This is the socket file we will need to use.
  • #mysql -u root –socket=/tmp/mysql.sock
    this will tell the mysql client to login as root user and use the socket file located in /tmp/mysql.sock

Boom, you should get into the suddenly-loved mysql> prompt. I was so excited when I was able to fix the client and was able to access to the mysql prompt.

I hope this help you all. With this success in installing the MySQL 5.0.27 cluster, I am planning to do a screencast of setting the whole thing up (VMWare is just awesome. It allows me to have 3 CentOS 4.4 servers at the same time, making this installation a breeze)


 

16 Responses to “(my) Solution to the infamous ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)



3:57 am
December 19, 2006
#8592

Thanks Alex!
I was trying to install mySQL in my Mac OS 10.4.8 Macbook and I got snaged in the same problem. After some tries and a lot of websurfing in search for the solution I used yours , creating /etc/my.cnf exactly as you posted (didn’t bother for extra configuration options yet…) and then after a mysqld restart it worked!
Thanks a lot!
cheers
JAC




1:40 am
December 22, 2006
#10225

@Joao,

You’re very welcome. I’m glad that my little post was able to help you.

Merry Christmas!




Richard Buchgraber
12:13 pm
February 6, 2007
#15841

Hi Alex!

I am currently facing a very similar problem when setting up a mysql cluster node on CentOS Linux 4.4;
After I changed the my.cnf file as suggested and stopped / restarted the mysqld with service mysql stop (start) I received the message “Starting MySQL…. [FAILED]”

Although googling a lot I didn’t get it to work yet, so I wonder if you could give me some hints in how to manage this problem!

Best regards
Richard




Richard Buchgraber
12:42 pm
February 6, 2007
#15845

Add on: strange behaviour, it starts up now with your my.cnf but when I enter mysql console and type SHOW ENGINES; ndbcluster is still disabled… :-(




12:50 pm
February 6, 2007
#15847

Hello Richard!

I just happen to have the answer for your problem. Coincidentally there’s another Richard Harvey who came by my blog and shoot me an email about the same error as what you are having. Even more coincidentally this morning (Tue, Feb 06, 2007), Richard Harvey actually emailed me back and said that he had figured out the problem. It was because of SELinux being enabled when CentOS was being installed.

I don’t have the exact procedure to get MySQL working in your situation but I think if you keep digging for SELinux and MySQL, you will be able to solve the problem.

Cheers!




Richard Buchgraber
5:02 am
February 7, 2007
#15885

Thanks for your quick reply!

Unfortunately SELinux was already disabled, anyway the server doesn’t get up?

The strange thing is, when I change the my.cnf just to enable cluster (without mysql_cluster section and without ndb-connectstring) ndbcluster engine is enabled, but when I add the other lines the server doesn’t even start anymore…




Richard Buchgraber
5:05 am
February 7, 2007
#15886

… again add on: it starts, when ndb-connectstring is also entered in my.cnf, but then the ndbcluster engine is disabled!




Richard Buchgraber
7:47 am
February 7, 2007
#15904

Hi again!

finally I managed to get around this problem. It seems to be a mysql version bug that the ndbcluster didn’t start properly.
I downgraded to version 5.0.22 as advised at another page and this worked at once without any troubles!

Thanks for your assistance!

KR from Austria, Richard




Jessica
1:31 pm
March 5, 2007
#17824

Thanks so much for the help!!! Ohman, I’ve been working on setting up this L.A.M.P. server for a while. I’m a second year college student and didn’t really even know what I didn’t know in order to fix the problem. This is great! Thanks!




Jack Hill
7:24 pm
April 30, 2007
#27111

Hey Alex,

I’m new to the terminal codes and such, and just my luck mysql has the ERROR 2002. I am using the default mysql server with my macbook. I read that another user got there problem solved on a macbook by using your method and am wondering if you could help me with a step by step process on how to create that file.

Thanks alot




adam
9:59 pm
September 9, 2007
#56023

I am very new to mysql and I am in early stages of trying to learn about it. I attempted to see if the application was functional by typing mysql test and this is what I get:

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

I have edited my /etc/my.cnf file with your tips but to no avail. I’m so confused and desire a resolution to it. Originally, I was attempting to learn about MRTG and Cacti and since their dependent on Mysql; I thought perhaps this might be the source of my problems with their lack of functionailty. These are my current specs:

[root@Plutonium home]# cat /etc/redhat-release
CentOS release 4.5 (Final)
[root@Plutonium home]# uname -a
Linux Plutonium 2.6.9-55.0.6.EL #1 Tue Sep 4 21:10:58 EDT 2007 i686 athlon i386 GNU/Linux
[root@Plutonium home]# mysql -V
mysql Ver 14.7 Distrib 4.1.20, for redhat-linux-gnu (i686) using readline 4.3
[root@Plutonium home]# rpm -q mysql
mysql-4.1.20-2.RHEL4.1.0.1
[root@Plutonium home]#

I hope you can point me in the right direction. I have some books but they don’t cover this error. Also I’m a noob which doesn’t help but everyone has to start somewhere. An intelligent non-condecending response would be appreciated. Thank you for your time.

Regards,
Adam




4:33 am
October 15, 2007
#66055

I am using Solaris OS and have installed mysql. I have tried the work around mentioned above, changes in my.cnf and all. Nothing is working for me. The socket is not being created at all.
Please let me know how to proceed




11:00 pm
January 23, 2008
#105741

This is not working. please find a solution for this. First mysql was working fine in my system. With GUI I stopped my sql service. After that this problem started.

I have reinstalled mysql still it is same problem. please try to give us solution for this. if any body know solution pleasee send mail me.

Advanced thanks.




Rob
10:45 pm
March 11, 2008
#128170

Try commenting out the ‘basedir’ directive in your mysql config(my.cnf)

[mysql.server]
user=mysql
#basedir=/var/lib

Worked for me.




Abhishek
5:15 am
July 15, 2008
#183479

Hi Alex,

My 1st problem of : Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
got fixed by editing the /etc/my.cnf but now i am getting the following error

#./mysql -u root
ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: YES}

Is there any fix to this issue




Abhishek
5:28 am
July 15, 2008
#183481

I got the fix just putting it down in case somebody encounters the issue

service mysql stop

wait until MySQL shuts down. Then run

mysqld_safe –skip-grant-tables &

then you will be able to login as root with no password.

mysql -uroot mysql

In MySQL command line prompt issue the following command:

UPDATE user SET password=PASSWORD(”abcd”) WHERE user=”root”;
FLUSH PRIVILEGES;
EXIT

/etc/init.d/mysqld restart

At this time your root password is reset to “abcd” and MySQL will now
know the privileges and you’ll be able to login with your new password:

mysql -uroot -p mysql




 

Leave a Reply