简体   繁体   中英

django connec mysql --_mysql_exceptions.OperationalError: (2006, <NULL>)

In fact, I learn the django from the website: https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial02/

The next is my settting.py

DATABASES = {
    # 'default': {
    #     'ENGINE': 'django.db.backends.sqlite3',
    #     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    # }
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django_01',
        'USER': 'root',
        'PASSWORD': '9940',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
} 

mysql : enter image description here

I want to know how to solve this problem,or I don't know what's the problem? Thank you very much!

I think DATABASES is right, I want to connect mysql to django program named mysite, but I run 'python manage.py migrate':

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000000003B31D08>
Traceback (most recent call last):
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
    self.connect()
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection
    return Database.connect(**conn_params)
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\MySQLdb\__init__.py", line 85, in Connect
    return Connection(*args, **kwargs)
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\MySQLdb\connections.py", line 208, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2006, <NULL>)

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

Traceback (most recent call last):
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
    self.check_migrations()
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\core\management\base.py", line 442, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
    self.build_graph()
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\recorder.py", line 61, in applied_migrations
    if self.has_table():
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\recorder.py", line 44, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 255, in cursor
    return self._cursor()
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 232, in _cursor
    self.ensure_connection()
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
    self.connect()
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
    self.connect()
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection
    return Database.connect(**conn_params)
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\MySQLdb\__init__.py", line 85, in Connect
    return Connection(*args, **kwargs)
  File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\MySQLdb\connections.py", line 208, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2006, <NULL>)

Maybe it's because django can't support the new password encryption of mysql. You should try changing it. You can do this from the mysql command line client:

在此处输入图片说明

I got the same OperationalError: (2006, <NULL>) when using python to connect to MySQL Server on Windows. It seems like the error has something to do with the new authentication method in MySQL 8. I reconfigured the MySQL server from using the recommended authentication method to "Use Legacy Authentication Method" and the error goes away.

在此处输入图片说明

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