python manage.py syncdb错误

时间:2012-03-11 03:02:52

标签: python mysql django syncdb

我试图让我的第一个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.
}

}

有人能说清楚我做错了什么吗?

3 个答案:

答案 0 :(得分:5)

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

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

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

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

答案 1 :(得分:2)

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

另一个不太可能的建议是检查授予'django_user'的权利。该用户可能无权创建表。

答案 2 :(得分:0)

通过在 settings.py 中定义两个DATABASE配置文件来解决此问题。

默认 - django在运行时使用有限权限来获得额外的安全性。

同步 - 由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

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

相关问题