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

Posted on

Question :

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

When I attempted to connect to a local MySQL server during my test suite, it
fails with the error:

OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

However, I’m able to at all times, connect to MySQL by running the command line
mysql program. A ps aux | grep mysql shows the server is running, and
stat /tmp/mysql.sock confirm that the socket exists. Further, if I open a
debugger in except clause of that exception, I’m able to reliably connect
with the exact same parameters.

This issue reproduces fairly reliably, however it doesn’t appear to be 100%,
because every once in a blue moon, my test suite does in fact run without
hitting this error. When I attempted to run with sudo dtruss it did not reproduce.

All the client code is in Python, though I can’t figure how that’d be relevant.

Switching to use host produces the error:

DatabaseError: Can't connect to MySQL server on '' (61)

Answer #1:

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.

Answered By: Alex Gaynor

Answer #2:

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 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, 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 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.

Answered By: Pratyay

Answer #3:

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
Answered By: jtoberon

Answer #4:

I’ve seen this happen at my shop when my devs have a stack manager like MAMP installed that comes preconfigured with MySQL installed in a non standard place.

at your terminal run

mysql_config --socket

that will give you your path to the sock file. take that path and use it in your DATABASES HOST paramater.

What you need to do is point your

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'test',
        'PASSWORD': 'test',
        'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
        'PORT': '',


also run which mysql_config if you somehow have multiple instances of mysql server installed on the machine you may be connecting to the wrong one.

Answered By: yask

Answer #5:

I just changed the HOST from localhost to and it works fine:

# of Django project

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
Answered By: Francis Yaconiello

Answer #6:

When, if you lose your daemon mysql in mac OSx but is present in other path for exemple in private/var do the following command


ln -s /private/var/mysql/mysql.sock /tmp/mysql.sock

2) restart your connexion to mysql with :

mysql -u username -p -h host databasename

works also for mariadb

Answered By: Sirbito X

Answer #7:

Run the below cmd in terminal


enter image description here

Then restart the machine to take effect. It works!!

Answered By: aurny2420289

Answer #8:

After attempting a few of these solutions and not having any success, this is what worked for me:

  1. Restart system
  2. mysql.server start
  3. Success!
Answered By: Prashanth Sams

Leave a Reply

Your email address will not be published. Required fields are marked *