简体   繁体   中英

What is django.db.utils.OperationalError: (2000, 'Unknown MySQL error')

Whenever I enter python.\manage.py runserver on Django, this error: django.db.utils.OperationalError: (2000, 'Unknown MySQL error') appears. I think it is a client-side error as other people running the same project do not have this error.

Does anyone know what the error is and potentially how to fix it?

I also use a virtual environment with Django and mysqlclient installed.

Here is the full error:

(virtualenv) C:\Users\lonyi\OneDrive\Documents\Gitlab\habit-app\Habit>manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
(0.016) SELECT @@SQL_AUTO_IS_NULL; args=None
(0.015) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
(0.032) SHOW FULL TABLES; args=None
(0.015) None; args=()
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 73, in 
execute
    return self.cursor.execute(query, args)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\cursors.py", line 206, in execute      
    res = self._query(query)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\cursors.py", line 319, in _query       
    db.query(q)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\connections.py", line 259, in query    
    _mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (2000, 'Unknown MySQL error')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\runserver.py", 
line 121, in inner_run
    self.check_migrations()
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 459, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 18, in 
__init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
    self.build_graph()
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\loader.py", line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\recorder.py", line 78, in 
applied_migrations
    return {(migration.app, migration.name): migration for migration in self.migration_qs}
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 287, in __iter__
    self._fetch_all()
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\compiler.py", line 1156, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\utils.py", line 90, in __exit__      
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 73, in 
execute
    return self.cursor.execute(query, args)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\cursors.py", line 206, in execute      
    res = self._query(query)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\cursors.py", line 319, in _query       
    db.query(q)
  File "C:\Users\lonyi\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\connections.py", line 259, in query    
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (2000, 'Unknown MySQL error')

You should set 'OPTIONS' 'init_command' in your database settings. It worked for me.

ie:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'DATABASE_NAME',
    'USER': 'DATABASE_USER',
    'PASSWORD': 'DATABASE_PASS',
    'HOST': 'DATABASE_HOST',
    'PORT': 'DATABASE_PORT',
    'OPTIONS': {
        'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM