Django + XAMPP on Mac (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

1.5k Views Asked by At

I am following this tutorial to build a Django app with MySQL.

I am using XAMPP-VM for Mac. I set up my Database settings for Django as so:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangoproject',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '8080',
    }
}

And I have started MySQL on XAMPP but when I run python manage.py migrate I get:

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

Thank you for your help!

5

There are 5 best solutions below

1
Demeter Szabolcs On

The mysql client tries to connect through the /tmp/mysql.sock by default. You just have to point it to XAMPP's socket with a symbolic link.

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
0
marqpdx On

I would try changing your HOST from 'localhost' to '127.0.0.1'. This usually rectifies it on my end.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangoproject',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '8080',
    }
}
0
Hank Moody On

Just change HOST from localhost to the ip 127.0.0.1. This will resolve your problem.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangoproject',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '8080',
    }
}
0
Shobhit Goel On

I got same error in mysql latest version, so I downgraded to mysql 5.7.0 and it's working fine.

0
user2559949 On

This is probably due to XAMPP-VM being installed and not the local XAMPP. Uninstall XAMPP-VM and install XAMPP.