0

I can connect to my MySQL server externally (so I know the server is up and running) but when I try to connect locally I get the good old

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

So unlike other situations my server IS running - its just I can't connect locally?

Antony
  • 3,875
  • 30
  • 32
  • Does the mentioned file actually exist? Also see: http://stackoverflow.com/questions/4448467/cant-connect-to-local-mysql-server-through-socket-var-lib-mysql-mysql-sock – Philip Whitehouse Apr 27 '15 at 09:01
  • No but I can connect externally. Would an external connection not use the sock file? – Antony Apr 27 '15 at 09:22
  • No, that uses the TCP/IP socket - this is a Unix socket. A work-around is to connect to `127.0.0.1` instead I believe. `On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file.` https://dev.mysql.com/doc/refman/5.5/en/connecting.html – Philip Whitehouse Apr 27 '15 at 10:30

2 Answers2

0

It seems like you are not maintaining mysql.sock which is a must for local connections. You should first check mysql.sock file exists or not at your mentioned location i.e. /var/lib/mysql/mysql.sock. If not, add below in your my.conf file and restart mysql server

[mysqld]
socket=/var/lib/mysql/mysql.sock

[client]
socket=/var/lib/mysql/mysql.sock
Harsh Pandit
  • 101
  • 1
  • 5
  • If it doesn't exist, adding paths to it won't help. You can either set the paths correctly to the actual location or symlink it from the actual location to `/var/lib/mysql/mysql.sock` with `ln -s` – Philip Whitehouse Apr 27 '15 at 10:33
  • Sorry Philip I'm not understanding you properly. How will symlinking help for a file that doesn't exist? Do I still need to restart the server as well? – Antony Apr 27 '15 at 10:40
  • You need to find the mysql.sock file and either add lines similar to those posted but with the correct path OR create a symlink from the correct path to /var/lib/mysql/mysql.sock. Both the server and client need to use the same file. The file basically acts as a pipe to transmit information - https://en.wikipedia.org/wiki/Unix_file_types#Socket – Philip Whitehouse Apr 28 '15 at 16:25
0

You may be missing mysql-client.

Your server may be running fine, but you still need a client in order to run locally.

To fix this on Unix/Linux, use your favorite/default dependency management service to install mysql-client. Windows installs generally do this automatically, as do Mac installs from the MySQL website.

Examples

Debian/Ubuntu:

sudo apt-get install mysql-client

RedHat:

sudo yum install mysql-client
Community
  • 1
  • 1
smcjones
  • 5,490
  • 1
  • 23
  • 39