1

我试图让我的第一个 Django 项目在 Windows 上运行,但我收到一条错误消息,内容如下:

File "c:\users\[username]\appdata\local\temp\easy_install-pazcre\MySQL_python-1.2.3-py2.7-    win32.egg.tmp\MySQLdb\connections.py", line 187, in __init__     mysql_exceptions.OperationalError: (1045, "Access denied for user 'django_user'@'localhost'     (using password: YES)")

我从命令行创建了我的数据库:

-- create the database
CREATE DATABASE GlobalXdb CHARACTER SET utf8;

-- create user
CREATE USER 'django_user'@'localhost' IDENTIFIED BY 'thepassword';

-- give user permissions to db 
GRANT ALL ON django.* TO 'django_user'@'localhost'

我的settings.py文件包含:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'GlobalXdb',                      # Or path to database file if using sqlite3.
    'USER': 'django_user',                      # Not used with sqlite3.
    'PASSWORD': 'thepassword',                  # Not used with sqlite3.
    'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
}

}

谁能阐明我做错了什么?

4

3 回答 3

5

您的数据库名为GlobalXdb,但在这一行中......

#give user permissions to db 
GRANT ALL ON django.* TO 'django_user'@'localhost'

您在名为django的数据库上授予 django_user 权限。

授予正确数据库GlobalXdb的权限应该可以解决您的问题。

于 2012-03-11T05:34:02.740 回答
2

错误消息说Access denied for user 'django_user'@'localhost',所以我的猜测是用户 'django_user' 不存在或者您输入的密码错误。

另一个不太可能的建议是检查授予“django_user”的权限。此用户可能没有创建表的权限。

于 2012-03-11T03:14:04.637 回答
0

这可以通过在settings.py中定义两个 DATABASE 配置文件来解决。

default - 由 django 在运行时使用,具有有限的权限以获得额外的安全性。

sync - 由 syncdb 作为用户root使用,具有额外的创建权限。

DATABASES = {
  'default': {
    'ENGINE':'django.db.backends.mysql',
    'NAME':'database_name',
    'USER':'runtime',
    'PASSWORD':'runtime_password',
    'HOST':'',
    'PORT':'',
  },
  'sync': {
    'ENGINE':'django.db.backends.mysql',
    'NAME':'database_name',
    'USER':'root',
    'PASSWORD':'',
    'HOST':'',
    'PORT':'',
  },
}

您还必须为MySQL 中的默认运行时访问授予有限权限:

GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.* TO 'runtime'@'localhost' IDENTIFIED BY 'runtime_password';

最后,当您运行syncdb时,指定您的同步访问配置文件:

python manage.py syncdb --database=sync

这应该为您提供运行时所需的安全性以及命令行上所需的访问权限。

于 2014-09-10T00:08:38.760 回答