1

I am new to Django and mysql and I am trying to configure my django application's backend to mysql. I am using XAMPP for my local mysql database and anaconda as my pkg manager. Also using a conda virtualenv.

When I go to run python3 manage.py migrate, I get this error:

django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (61)")

My database is setup as the following:

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

}

I changed 'HOST' to 'localhost' but then get this error:

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

Any help is appreciated!

Alex Harr
  • 13
  • 3

1 Answers1

4

Find your mysqld.sock file location then add it to HOST using xampp on Linux mysqld.sock file is in another location, therefore, it is not working for /var/run/mysqld/mysqld.sock

DATABASES = {

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER' : 'root',
        'PASSWORD' : '',
        'HOST' : '/opt/lampp/var/mysql/mysql.sock',
        'PORT' : ''
    }
}

Or Run MySQL server first then execute MySQL.

$ mysql.server start

$ mysql -h localhost -u root -p
piet.t
  • 11,718
  • 21
  • 43
  • 52
Ab Majeed
  • 106
  • 4
  • I went ahead and did some digging to find mysql.sock and found it at this path: `/Users/aharr/.bitnami/stackman/machines/xampp/volumes/root/var/mysql` Now running into this error: `django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/Users/aharr/.bitnami/stackman/machines/xampp/volumes/root/var/mysql/mysql.sock' (61)")` – Alex Harr Dec 08 '18 at 22:13
  • I found my.cnf file and was wondering if I should change `socket = /opt/lampp/var/mysql/mysql.sock` equal to the pathway above? – Alex Harr Dec 08 '18 at 22:22
  • I was able to figure it out. I installed XAMPP-VM which wouldn't let me directly access the socket since I had to mount an NFS mount. Downloaded regular XAMPP and pointed it to the mysql.sock in `/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock` Thanks, I appreciate it ! – Alex Harr Dec 08 '18 at 23:17