1

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!

Rot-man
  • 18,045
  • 12
  • 118
  • 124
S.Park
  • 11
  • 3
  • Possible duplicate of [Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)](https://stackoverflow.com/questions/5376427/cant-connect-to-local-mysql-server-through-socket-var-mysql-mysql-sock-38) – Tobias F. Nov 15 '18 at 07:41
  • Unfortunately it is not... – S.Park Nov 15 '18 at 08:00

5 Answers5

1

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',
    }
}
Vadim
  • 8,701
  • 4
  • 43
  • 50
Hank Moody
  • 51
  • 8
0

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

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',
    }
}
marqpdx
  • 51
  • 1
  • 4
0

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

ejderuby
  • 710
  • 5
  • 21
Shobhit Goel
  • 59
  • 1
  • 4
0

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