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

若如初见. 提交于 2019-12-20 09:56:19

问题


I've started working with django, and here is the error on 'runserver' after setting up 'DATABASES' in settings.py

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

My settings.py portion of code:

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

}

I've created 'my_site' database and privileges are set in the right way (root has all privileges). I've done this operations with 'phpmyadmin'.

What goes wrong?


回答1:


Run in console

mysql> grant all privileges on *.* to root@localhost identified by 'password' with grant option;



回答2:


After struggling for two days I finally found whats wrong. Entire steps to set up a mysql database for Django site are-

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

Note:-The password and username given above is of my localhost you have change it to yours.

  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

step 3, 4 and 5 just tells how to reach the folder where manage.py file found in your Application.

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

If you are having any error like- "No module named MySQLdb" in running above command you can visit the 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.




回答3:


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

GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON winestores.* TO username@127.0.0.1 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 username@localhost. It worked! I don't know why? Maybe it is the difference between IPs of '127.0.0.1' and 'localhost'???

as read on dev.mysql.com.




回答4:


For anyone encountering this issue - check to make sure that the database user host is localhost . If set to %, it won't work, you will need to add localhost or change your overall security settings.




回答5:


your DB doesn't recognize your root user and password. If you can go to the phpmyAdmin and try to create a different user and password and assign to the DB, if that is not working try to create but console with create user, and grant all.




回答6:


This occurs when your Username or password is wrong ....so go through the username and password once again.




回答7:


FYI if your using XAMPP, go directly into localhost/phpmyad>Privileges to modify this



来源:https://stackoverflow.com/questions/10181344/mysql-exceptions-operationalerror-1045-access-denied-for-user-rootlocalh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!