2012-04-16 231 views
12

我已經開始使用django了,這裏是設置後'runserver'的錯誤在settings.py「數據庫」mysql_exceptions.OperationalError:(1045,「拒絕用戶root'@'localhost'(使用密碼:YES)」)

mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)") 

我的代碼settings.py部分:

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 
     'NAME': 'my_site',      # Or path to database file if using sqlite3. 
     'USER': 'root',      # Not used with sqlite3. 
     'PASSWORD': 'root',     # 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. 
    } 

} 

我創建「my_site」數據庫和特權以正確的方式設置(根擁有所有權限)。 我已經用'phpmyadmin'完成了這個操作。

出了什麼問題?

回答

13

在控制檯

mysql> grant all privileges on *.* to [email protected] identified by 'password' with grant option; 
+0

我其實想通過python運行這個命令。但是我拒絕訪問。任何想法如何自動化?謝謝 – 2016-10-31 16:02:37

0

您的數據庫無法識別您的root用戶和密碼。如果你可以去phpmyAdmin並嘗試創建一個不同的用戶和密碼並分配給數據庫,如果這不起作用,嘗試創建,但與創建用戶控制檯,並授予所有。

+0

做有喜歡 - 「沒有名爲MySQLdb的模塊」的錯誤:沒有工作 – 2012-04-16 21:27:53

1

The instruction said to add the username with the SQL statement as:

GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON winestores.* TO [email protected] IDENTIFIED by 'password'; 

After doing this as root, I quit and tried to log back in with the new user. I got the ERROR 1045. I fixed it by logging back in as root and reissuing the SQL statement again except with [email protected] . It worked! I don't know why? Maybe it is the difference between IPs of '127.0.0.1' and 'localhost'???

如閱讀dev.mysql.com

+0

不會爲我工作。 – 2012-04-16 21:32:43

0

運行任何人遇到此問題 - 請檢查以確保該數據庫用戶主機localhost。如果設置爲%,它將不起作用,您將需要添加localhost或更改您的整體安全設置。

3

掙扎了兩天後,我終於找到了最新的錯誤。 整個步驟設置Django的網站mysql數據庫是 -

Your settings.py file must look like-

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': 'django_db', #Name of database you created 
     'USER': 'root',  #Username of database you created 
     'PASSWORD':'',   #Password of database you created 
     'HOST': '', 
     'PORT': '', 
    } } 

注:上面給出-The用戶名和密碼是我的本地主機,你必須將其更改爲你的事。

  1. create a database in localhost let name it django_db (for this you can use lamp/wamp/Xampp or mysql command prompt)

  2. Edit the file settings.py and save the password you require to open your database in DATABASES field as given below in the code.

  3. open terminal.

  4. If you are using virtual environment then switch into it by using command ->workon env_name

  5. Now cd app_name eg. cd mysite

步驟3,4和5只是告訴如何到達您的應用程序中找到的manage.py文件所在的文件夾。

6.To check you are in right directory run command ls -l. If you find manage.py file then everything is right go ahead

7.python manage.py migrate

如果您在上面的命令運行,您可以訪問link

8.python manage.py syncdb

9.python manage.py runserver

10.You will be asked for username and password for your Django admin, give whatever you want to open Django admin site.

11.check you are having some tables in your database in localhost at ` http://localhost/phpmyadmin . You are good to go now.

相關問題