Can't connect to local MySQL server through socket '/tmp/mysql.sock Can't connect to local MySQL server through socket '/tmp/mysql.sock python python

Can't connect to local MySQL server through socket '/tmp/mysql.sock


sudo /usr/local/mysql/support-files/mysql.server start 

This worked for me. However, if this doesnt work then make sure that mysqld is running and try connecting.


The relevant section of the MySQL manual is here. I'd start by going through the debugging steps listed there.

Also, remember that localhost and 127.0.0.1 are not the same thing in this context:

  • If host is set to localhost, then a socket or pipe is used.
  • If host is set to 127.0.0.1, then the client is forced to use TCP/IP.

So, for example, you can check if your database is listening for TCP connections vi netstat -nlp. It seems likely that it IS listening for TCP connections because you say that mysql -h 127.0.0.1 works just fine. To check if you can connect to your database via sockets, use mysql -h localhost.

If none of this helps, then you probably need to post more details about your MySQL config, exactly how you're instantiating the connection, etc.


For me the problem was I wasn't running MySQL Server.Run server first and then execute mysql.

$ mysql.server start$ mysql -h localhost -u root -p