Django / MySQL-python - 使用旧的(4.1.1之前的)身份验证协议拒绝连接(客户端选项' secure_auth'启用)

时间:2014-02-25 18:45:30

标签: python mysql django

很多人在SO上遇到过这个问题,但几乎所有答案都没用。

Traceback (most recent call last):
  File "/venv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
    self.validate(display_num_errors=True)
  File "/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/venv/local/lib/python2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
  File "/venv/local/lib/python2.7/site-packages/django/db/
backends/mysql/validation.py", line 14, in validate_field
    db_version = self.connection.get_server_version()
  File "/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 415, in get_server_version
    self.cursor().close()
  File "/venv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
    self.connection = Database.connect(**kwargs)
  File "/venv/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/venv/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2049, "Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enabled)")

3 个答案:

答案 0 :(得分:25)

对于管理服务器连接,连接选项卡,高级子选项卡中的MySQL Workbench 6.08,您必须选中使用旧身份验证协议框。'

enter image description here

答案 1 :(得分:13)

尝试与mysql命令行客户端连接时遇到同样的问题,我可以使用--skip-secure-auth选项来避免错误。

例如:

mysql DBNAME -h HOST_NAME_OR_IP -u DB_USER --password="PASSWORD" --skip-secure-auth

其他 mysqlX 命令的相同参数,例如mysqldump

答案 2 :(得分:8)

经过多次试验/错误后,问题被缩小为使用旧加密方式的密码。

mysql> select User, Password from mysql.user;
| user1           | *113D91F3A36D29C287C457A20D602FA384B8569F |
| user2           | 5d78f2535e56dfe0 |

Navicat等工具不会自动使用较新的样式密码,你必须明确告诉它不要使用旧样式(在Navicat中,它叫做Advanced / Use OLD_PASSWORD tncryption) - 这就是我们被绊倒的地方。

如果你正在使用mysql shell,你可以使用;

mysql> update mysql.user set password=PASSWORD("NEWPASSWORD") where User='testuser';
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select User, Password from mysql.user WHERE user = 'testuser';
| testuser | *B845F78DCA29B8AE945AB9CFFAC24A9D17EB5063 |

如果您想查找受此问题影响的所有用户;

mysql> SELECT Host, User, Password FROM mysql.user WHERE LENGTH(Password) = 16;

某些版本的Django不允许您在选项中使用secure-auth,但对于您可以使用的那些;

'OPTIONS': {
    'secure-auth': False
}

如果不支持,则会产生;

TypeError: 'secure-auth' is an invalid keyword argument for this function

对于那些将来遇到麻烦的人来说,这应该是一个快速的答案。