Django和MySQL无法获得与数据库连接的访问​​权限ERROR:1045

时间:2016-07-20 22:25:03

标签: python mysql django connector

您好我正在与一些人合作开展一个项目。我们正在使用django 1.9我的mysql和有人设置django来连接mysql,然后为我们其他人编写了关于我们需要下载什么来连接python和mysql的说明。我正确安装了mysql但是我在访问数据库时遇到了问题。我们都假设有权访问数据库,但即使我只是在终端输入mysql django.db.utils.OperationalError: (1045, "Access denied for user 'django'@'localhost' (using password: YES)")并输入ERROR 1045 (28000): Access denied for user 'stevenJing'@'localhost' (using password: NO)我得到mysql -u root -p,我仍然会获得ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) python3 manage.py runserver }。我不确定我需要做些什么来解决这个问题我甚至无法做 Performing system checks... Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10434a510> Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection self.connect() File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/base.py", line 171, in connect self.connection = self.get_new_connection(conn_params) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 264, in get_new_connection conn = Database.connect(**conn_params) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQLdb/__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQLdb/connections.py", line 204, in __init__ super(Connection, self).__init__(*args, **kwargs2) _mysql_exceptions.OperationalError: (1045, "Access denied for user 'django'@'localhost' (using password: YES)") The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper fn(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run self.check(display_num_errors=True) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/base.py", line 426, in check include_deployment_checks=include_deployment_checks, File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/checks/registry.py", line 75, in run_checks new_errors = check(app_configs=app_configs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/checks/model_checks.py", line 28, in check_all_models errors.extend(model.check(**kwargs)) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/base.py", line 1178, in check errors.extend(cls._check_fields(**kwargs)) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/base.py", line 1255, in _check_fields errors.extend(field.check(**kwargs)) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 925, in check errors = super(AutoField, self).check(**kwargs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 208, in check errors.extend(self._check_backend_specific_checks(**kwargs)) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 317, in _check_backend_specific_checks return connections[db].validation.check_field(self, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/mysql/validation.py", line 18, in check_field field_type = field.db_type(connection) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 625, in db_type return connection.data_types[self.get_internal_type()] % data File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/__init__.py", line 36, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 184, in data_types if self.features.supports_microsecond_precision: File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 359, in mysql_version with self.temporary_connection(): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/contextlib.py", line 59, in __enter__ return next(self.gen) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/base.py", line 564, in temporary_connection cursor = self.cursor() File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/base.py", line 231, in cursor cursor = self.make_debug_cursor(self._cursor()) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/base.py", line 204, in _cursor self.ensure_connection() File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection self.connect() File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise raise value.with_traceback(tb) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection self.connect() File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/base.py", line 171, in connect self.connection = self.get_new_connection(conn_params) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 264, in get_new_connection conn = Database.connect(**conn_params) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQLdb/__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQLdb/connections.py", line 204, in __init__ super(Connection, self).__init__(*args, **kwargs2) django.db.utils.OperationalError: (1045, "Access denied for user 'django'@'localhost' (using password: YES)") 因为我得到一个很大的错误。

DATABASES = {
    'default': {
       # 'ENGINE': 'mysql.connector.django',
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'lolProject',
        'USER': 'django',
        'PASSWORD': 'django-pass',
        'HOST': '127.0.0.1',
        #'OPTIONS':{'read_default_file': ''},
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

这是我的Django的settings.py

click

设置数据库的人是MySQL的新手,我也是如此,如果有人从我们的git仓库中提取或克隆,他们怎么能得到它,他们可以访问数据库。

2 个答案:

答案 0 :(得分:1)

如果人们提取您的代码,并且您希望他们访问您的数据库,那么他们可能不应该使用&#34; localhost。&#34;如果您在本地主机上运行时遇到此错误,那么这是一个权限问题。

要做的第一件事是看看是否可以使用命令行MySQL或其他MySQL客户端使用这些凭据登录。

我很多都会遇到这类错误(不幸的是),这通常是因为我没有在MySQL中正确添加用户。我注意到只是把'&#39;%&#39;不考虑localhost,我通常需要添加&#39; user&#39; @&#39; localhost&#39;除了用户&#39; @&#39;%&#39;授予用户权限时。

编辑:为了授予权限,您需要以root用户身份登录mysql,通常您会使用以下内容:

% mysql -u root -P mysql

&#34; -P&#34;意味着你必须提供root / admin密码,最后一个&#34; mysql&#34;是要使用的架构。显然,您需要管理员访问才能执行此操作。

然后,在更新版本的MySQL中,首先创建用户:

> create user  'stevenJing'@'localhost' identified by 'password';

这实际上是一个不同于&#39; stevenJing&#39; @&#39;%&#39;的用户,所以如果您要从localhost和远程主机连接这两个用户,那么实际上你需要这两个用户

然后您需要为该用户授予权限:

> grant all privileges on <schema>.* to 'stevenJing'@'localhost';

架构显然是你的Django架构(上面的Django设置中的lolProject)。此外,当然,要授予该用户从其他地方登录的权限:

> grant all privileges on <schema>.* to 'stevenJing'@'%';

答案 1 :(得分:1)

Mysql权限

此错误是因为django用户无权访问lolProject数据库。您可以使用GRANT命令纠正此问题。 django用户需要很多权限,因为它需要创建和删除表作为迁移的一部分,所以像这样的人应该这样做。

GRANT ALL ON lolproject.* TO 'django'@'localhost';

更新:通常,您可以通过以mysql -u root打开mysql控制台来执行此操作,其中root是默认安装的帐户,通常没有与之关联的密码。

现在,如果每个人都在他们的计算机上安装了一个mysql服务器,那就没关系了。如果每个人都连接到同一个compupter,它应该是

GRANT ALL ON lolproject.* TO 'django'@'%';

由于从您的回购中提取的每个人都需要访问权限,因此您应该在settings.py

中使用IP而不是localhost

SQLite的

你考虑过使用sqlite吗?它几乎不需要任何设置,每个从您的仓库克隆或拉出的人都会获得当前数据库的副本(如果它已被添加到仓库中)。