I can not connect mysql to my Django project

0

In my settings.py file I put the following:

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

and when trying to do my python manage.py migrations I get the following error:

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

Does anyone know what is due?

django == 1.11.7 Ubuntu = 14.4

    
asked by Tajaludo 09.04.2018 в 03:57
source

3 answers

1

In Host, you already tried to put the localHost IP: 127.0.0.1

    
answered by 09.04.2018 / 06:30
source
0

To connect a Django application with MySQL you must first install the mysqlclient package. Once you have installed the package you must add the following code in setting.py:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'nombre_base_datos',
    'USER': 'usuario',
    'PASSWORD': 'contraseña',
    'HOST': 'localhost',
    'PORT': '3306',
    'OPTIONS': {
        'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
    },
}

}

Check in your database if you have a user linked to that database or if root has a password.

    
answered by 09.04.2018 в 08:43
0

Verify that you have installed this library with pip mysqlclient==1.3.12 something similar happened to me when I could not connect to my database

pip install mysqlclient
    
answered by 11.04.2018 в 23:16