简体   繁体   English

python manage.py syncdb错误

[英]python manage.py syncdb error

I am attempting to get my first Django project working, on Windows, but I get an error message that reads: 我试图在Windows上使我的第一个Django项目正常工作,但出现一条错误消息,内容为:

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)")

I created my database from the command line as: 我从命令行创建数据库是:

-- 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'

My settings.py file contains: 我的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.
}

} }

Can anyone shed some light on what I have done incorrectly? 谁能告诉我我做错了什么?

Your database is named GlobalXdb and yet in this line... 您的数据库名为GlobalXdb ,但在这一行中...

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

you grant permissions to django_user on database named django . 您向名为django的数据库授予django_user权限。

Give permissions to the correct database GlobalXdb should solve your problem. 为正确的数据库授予权限GlobalXdb应该可以解决您的问题。

The error message says Access denied for user 'django_user'@'localhost' so my guess would be that the user 'django_user' doesn't exist or you typed the password wrong. 错误消息显示Access denied for user 'django_user'@'localhost'所以我的猜测是用户'django_user'不存在或您输入的密码错误。

Another less likely suggestion would be to check the rights granted for 'django_user'. 另一个不太可能的建议是检查为“ django_user”授予的权限。 It's possible that this user doesn't have permission to create tables. 该用户可能没有创建表的权限。

This is solved by defining two DATABASE profiles in your settings.py . 这可以通过在settings.py中定义两个DATABASE配置文件来解决。

default - Used by django at runtime with limited permissions for that extra security. 默认 -由django在运行时使用,具有有限的权限才能获得额外的安全性。

sync - Used by syncdb, as user root , with extra create privileges. 同步 -通过执行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':'',
  },
}

You will also have to grant limited permissions for the default runtime access in MySQL: 您还必须授予MySQL默认运行时访问的有限权限

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

Finally when you run syncdb specify your sync access profile: 最后,当您运行syncdb时,请指定您的同步访问配置文件:

python manage.py syncdb --database=sync

That should give you the security you want at runtime and the access you want on the command line. 这样可以为您提供运行时所需的安全性以及在命令行上所需的访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM