[Django]-Django connect mysql problem, NameError: name '_mysql' is not defined

7πŸ‘

βœ…

The recommended way to use MySQL with Django is to install mysqlclient instead of mysql-connector-python.

If you use mysql-connector-python, then you need to change the ENGINE to 'mysql.connector.django' in your DATABASES setting (docs).

DATABASES = {
    'default': {
                'ENGINE': 'mysql.connector.django',
                'HOST': 'xxxxxx.compute.amazonaws.com',
                'PORT': '3306',
                'NAME': 'xxxxx',
                'USER': 'xxxxx',
                'PASSWORD': 'xxxxxx',
                'init_command': "SET sql_modes = 'STRICT_TRANS_TABLES'",
    },
}
πŸ‘€Alasdair

6πŸ‘

pip install PyMySQL

Django: settings.py

import pymysql  
pymysql.install_as_MySQLdb()

Source code pymysql.__init__.py

def install_as_MySQLdb():
    """
    After this function is called, any application that imports MySQLdb or
    _mysql will unwittingly actually use pymysql.
    """
    sys.modules["MySQLdb"] = sys.modules["_mysql"] = sys.modules["pymysql"]
πŸ‘€Gajanan

1πŸ‘

you need to instsall missing pkg by running this on ubuntu and then restart mysql

sudo apt-get install libmysqlclient-dev python-dev
sudo /etc/init.d/mysql restart

0πŸ‘

I just had this problem .
you should use

sudo apt-get install libmysqlclient-dev

if it said the package is missing then try :

sudo apt-get install libmariadb-dev

πŸ‘€the_yaz2000

Leave a comment