错误1045(28000):拒绝访问用户'root'@'localhost'错误

时间:2018-07-19 02:41:33

标签: mysql django

当我在Django应用中运行python manage.py migration时,会发生类似

的错误
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/base.py", line 327, in execute
    self.check()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 61, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/core/checks/database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
    with self.connection.cursor() as cursor:
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 274, in get_new_connection
    conn = Database.connect(**conn_params)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
    return Connection(*args, **kwargs)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pymysql/connections.py", line 327, in __init__
    self.connect()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pymysql/connections.py", line 598, in connect
    self._request_authentication()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pymysql/connections.py", line 862, in _request_authentication
    auth_packet = self._process_auth(plugin_name, auth_packet)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pymysql/connections.py", line 933, in _process_auth
    pkt = self._read_packet()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pymysql/connections.py", line 683, in _read_packet
    packet.check_error()
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/home/vagrant/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
django.db.utils.OperationalError: (1045, "Access denied for user 'admin'@'localhost' (using password: NO)")

我写了mysql -u root,错误1045(28000):用户'root'@'localhost'(使用密码:否)的访问被拒绝,并且当我运行服务mysqld start时,无法启动mysqld.service:发生访问被拒绝错误。此外,当我运行service mysql stop时,无法停止mysql.service:发生了访问被拒绝错误。我向每个命令添加了sudo,但是同样的错误也发生了。我真的不明白为什么认证错误发生。我该如何解决?我的代码有什么问题?

2 个答案:

答案 0 :(得分:1)

在较新的MySQL版本中,您还需要在命令中使用-p标志:

mysql -u root -p

然后输入密码,您将可以登录。

  

现在,您可以使用具有密码和其他权限的admin创建新用户,并且可以在 settings.py 文件中使用。

答案 1 :(得分:0)

您需要成为root用户才能运行service mysqld startservice mysqld start