[英]python manage.py syncdb error
我試圖在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)")
我從命令行創建數據庫是:
-- 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.
}
}
誰能告訴我我做錯了什么?
您的數據庫名為GlobalXdb ,但在這一行中...
#give user permissions to db
GRANT ALL ON django.* TO 'django_user'@'localhost'
您向名為django的數據庫授予django_user權限。
為正確的數據庫授予權限GlobalXdb應該可以解決您的問題。
錯誤消息顯示Access denied for user 'django_user'@'localhost'
所以我的猜測是用戶'django_user'不存在或您輸入的密碼錯誤。
另一個不太可能的建議是檢查為“ django_user”授予的權限。 該用戶可能沒有創建表的權限。
這可以通過在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
這樣可以為您提供運行時所需的安全性以及在命令行上所需的訪問權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.