拒绝访问 Django : (1045, "Access denied for user 'darth_vader'@'151.33.241.48' (using password: YES)")

Access Denied Django : (1045, "Access denied for user 'darth_vader'@'151.33.241.48' (using password: YES)")

我正在使用 django 3.0 并为我的 Web 应用程序使用 MySQL 数据库。 我的 settings.py 代码是:

    DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'thirdihrms',
    'USERNAME': 'admin',
    'PASSWORD': 'xxxxxxxx',
    'HOST': 'database-1.xxxxxxxxx.us-east-1.rds.amazonaws.com',
    'PORT': '3306',
   }
}

它没有连接到 admin@151.33.241.48,而是连接到 darth_vader 用户名。 darth_vader名字是我的ubantu机器(本地开发环境)home用户名。

当我打开我的终端时,我看到“darth_vader@Rushikesh-HP:~$”。

错误信息:

Traceback (most recent call last):


File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 233, in get_new_connection
    return Database.connect(**conn_params)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/MySQLdb/connections.py", line 179, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (1045, "Access denied for user 'darth_vader'@'157.33.241.48' (using password: YES)")

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

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 366, in execute
    self.check()
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 395, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 63, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/core/checks/database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
    with self.connection.cursor() as cursor:
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 260, in cursor
    return self._cursor()
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 236, in _cursor
    self.ensure_connection()
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 233, in get_new_connection
    return Database.connect(**conn_params)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "/home/darth_vader/anaconda3/lib/python3.7/site-packages/MySQLdb/connections.py", line 179, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1045, "Access denied for user 'darth_vader'@'157.33.241.48' (using password: YES)")

是否有任何配置更改或设置更改,以便我可以 运行 我的应用程序。

指定用户名的key'USER' [Django-doc],不是'USERNAME'。如文档所述:

USER

Default: '' (Empty string)

The username to use when connecting to the database. Not used with SQLite.

因此您应该通过以下方式解决此问题:

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'thirdihrms',
    <b>'USER': 'admin',</b>  # <i>no</i> <s>USERNAME</s>
    'PASSWORD': 'xxxxxxxx',
    'HOST': 'database-1.xxxxxxxxx.us-east-1.rds.amazonaws.com',
    'PORT': '3306',
   }
}