2012-04-16 40 views
12

Tôi đã bắt đầu làm việc với django, và đây là lỗi trên 'runserver' sau khi thiết lập 'CƠ SỞ DỮ LIỆU' trong settings.pymysql_exceptions.OperationalError: (1045, "Truy cập bị từ chối cho người dùng 'root' @ 'localhost' (sử dụng mật khẩu: YES)")

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

phần settings.py My mã:

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. 
    } 

} 

tôi đã tạo ra 'my_site' cơ sở dữ liệu và đặc quyền được thiết lập một cách đúng đắn (root có tất cả đặc quyền). Tôi đã thực hiện thao tác này với 'phpmyadmin'.

Điều gì xảy ra?

Trả lời

13

Run trong giao diện điều khiển

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

tôi thực sự muốn chạy lệnh này thông qua python. Nhưng tôi bị từ chối truy cập. Bất kỳ ý tưởng làm thế nào để tự động hóa điều này? Cảm ơn –

0

DB của bạn không nhận ra người dùng và mật khẩu gốc của bạn. Nếu bạn có thể vào phpmyAdmin và cố gắng tạo một người dùng và mật khẩu khác và gán cho DB, nếu đó không phải là cố gắng tạo ra nhưng giao diện điều khiển với tạo người dùng và cấp tất cả.

+0

thực hiện: không làm việc –

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

như đọc trên dev.mysql.com.

+0

không làm việc cho tôi. –

0

Đối với bất cứ ai gặp phải vấn đề này - hãy kiểm tra để đảm bảo rằng người sử dụng cơ sở dữ liệu máy chủ localhost. Nếu được đặt thành %, nó sẽ không hoạt động, bạn sẽ cần phải thêm localhost hoặc thay đổi cài đặt bảo mật tổng thể của mình.

3

Sau khi đấu tranh trong hai ngày, cuối cùng tôi đã tìm thấy lỗi. Toàn bộ các bước để thiết lập một cơ sở dữ liệu mysql cho trang web của Django laø-

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': '', 
    } } 

Lưu ý: -Các mật khẩu và tên người dùng đưa ra ở trên là của localhost của tôi bạn có thay đổi nó như của bạn.

  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

bước 3, 4 và 5 chỉ cho biết cách tiếp cận thư mục chứa tập tin manage.py trong Ứng dụng của bạn.

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

Nếu bạn đang gặp bất kỳ lỗi như- "Không mô-đun tên MySQLdb" trong việc điều hành trên lệnh bạn có thể ghé thăm 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.

Các vấn đề liên quan