I have mysql 5.5 on my ubuntu 12.04 server. This command:

mysql -u root -p

works perfectly, but this gives me error:

mysqldump -u root -p mydb_name > a.sql
mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) when trying to connect

In my.cnf file, the socket is set to:

socket = /home/mysql/mysql.sock

so I have no idea where /var/run/mysqld/mysqld.sock is coming from. Thanks.

  • Did you verify that there are no other socket directives further down in my.cnf? Jul 13, 2013 at 0:29
  • yes i did, that is the only one.
    – AliBZ
    Jul 13, 2013 at 2:25

1 Answer 1


When reading the configuration file, the mysql command line client only reads configuration directives found in the [client] and [mysql] sections, while mysqldump only uses configuration directives found in the [client] and [mysqldump] sections.

If your socket directive in the config file is in the [mysql] section but not the [mysqldump] section then you should move it to the [client] section, and this should solve your problem.

So where does /var/run/mysqld/mysqld.sock come from? I remember very distinctly, once upon a time, beating my head against the wall repeatedly over that one.

It turns out, Ubuntu compiles their MySQL components from source, and when they do that, they compile them to use /var/run/mysqld/mysqld.sock unless the configuration file specifies otherwise or the socket is overridden on the command line... so if your socket directive isn't in [client] that would be one explanation of this behavior.

  • 1
    Thanx! that was it. I thought [mysqldump] was enough. I didn't even have [client].
    – AliBZ
    Jul 15, 2013 at 17:19
  • I had to add [mysqldump] socket=/home/lib/mysql/mysql.sock to my.cnf (centos) then restarted mysqld
    – zzapper
    Feb 17, 2017 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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