Using MySQL with Django - Access denied for user '@'localhost

后端 未结 7 2136
臣服心动
臣服心动 2020-12-07 17:46

So I\'m learning Django (1, 3, 1, \'final\', 0) through this resource: http://www.djangobook.com/en/2.0/chapter05/

I installed \'mysql-server\' and \'python-mysqldb\

相关标签:
7条回答
  • 2020-12-07 18:29

    You need to verify database configurations in settings.py file and also if your db user has privilege to access the database and perform operations on it.

    Please see if solutions mentioned here works for you.

    0 讨论(0)
  • 2020-12-07 18:30

    I also ran into this problem while following the Django tutorial on testing (https://docs.djangoproject.com/en/dev/intro/tutorial05/). When trying to test the polls app, I received the following error:

    user@host:~/django/mysite$ python manage.py test polls
    
    Creating test database for alias 'default'...
    Got an error creating the test database: (1044, "Access denied for user 'admin'@'localhost' to database 'test_django_mysite'")
    Type 'yes' if you would like to try deleting the test database 'test_django_mysite', or 'no' to cancel: 
    

    The problem is that django is creating a temporary database called test_django_mysite.
    I was able to solve this problem by granting access to 'admin'@'localhost' on the test_django_mysite database that it was trying to create.

    mysql> GRANT ALL PRIVILEGES ON test_django_mysite.* TO admin@localhost IDENTIFIED BY 'mypassword';
    Query OK, 0 rows affected (0.04 sec)
    

    It's worth noting that I only granted the privileges to test_django_mysite. I didn't need to actually create it.

    0 讨论(0)
  • 2020-12-07 18:39

    If you changed the database password and updated settings.py, you will also have to restart the wsgi.py process, for example by restarting apache.

    0 讨论(0)
  • 2020-12-07 18:40

    The django book is fairly old by now. in particular, database settings are now a dictionary, rather then the bunch of variables described in the django book.

    looking at the last line of your traceback, it looks like it's trying connect without a username.

    try changing

    DATABASE_ENGINE = ''
    DATABASE_NAME = ''
    DATABASE_USER = ''
    DATABASE_PASSWORD = ''
    

    to

    DATABASES = {
        'default': {
            'ENGINE': '',
            'NAME': ''
            'USER': ''
            # etc
        }
    }
    
    0 讨论(0)
  • 2020-12-07 18:43

    Just gonna leave my answer here because I lost few good minutes (almost hour) on this matter.

    Watch Your copy-paste :) Unfortunately, mysql return success while granting priviliges for not existing user to not existing database...

    Both queries:

    GRANT ALL PRIVILEGES ON python_user.* TO 'python_db'@'%' WITH GRANT OPTION;
    

    and:

    GRANT ALL PRIVILEGES ON python_db.* TO 'python_user'@'%' WITH GRANT OPTION
    

    will return success. Aldo I don't have such a user called python_db and db named python_user. :) That's sad. My mistake but also MySql was not helpful enough with output. :)

    0 讨论(0)
  • 2020-12-07 18:48

    My problem was that I was editing settings.py whereas configuration should have been done in local_settings.py

    Now everything seems to work.

    0 讨论(0)
提交回复
热议问题