Django : mysql : 1045, "Access denied for user

2020-02-04 06:54发布

I have the whole setup working for months on my local computer.
I'm installing on a remote site now.
Created a fresh mysql DB, and created a new user ("someuser") and gave it complete grants, like so -

GRANT ALL PRIVILEGES ON . TO 'someuser'@'localhost' IDENTIFIED BY 'somepassword' WITH GRANT OPTION;

I have sync'd the db, using "python manage.py syncdb" and the correct tables were created. My settings.py has this same user.

But when I try to login a user through the application, and it hits the DB, I see the following in the logs -

(1045, "Access denied for user 'someuser'@'localhost' (using password: YES)")

I logged in through mysql (installed on the same box as django) and checked the grants and it correctly shows -

Grants for someuser@localhost
GRANT ALL PRIVILEGES ON * . * TO 'someuser'@'localhost' IDENTIFIED BY PASSWORD '*thesaltedpasswordOverHere' WITH GRANT OPTION

I don't want to use the root user/password for django, since it doesn't seem the correct way.

Any pointers as to what might be wrong ?

10条回答
叼着烟拽天下
2楼-- · 2020-02-04 07:17

I do it like this for a database named foo_db:

create database foo_db;
create user foo_user identified by 'foo_password';
grant all on foo_db.* to 'foo_user'@'%';
flush privileges;
查看更多
Luminary・发光体
3楼-- · 2020-02-04 07:19

Refering to the answer of @duffymo and changed the last statement could work for me.

create database foo_db;
create user foo_user identified by 'foo_password';
grant all on foo_db.* to foo_user@localhost identified by 'foo_password' with grant option;
查看更多
Evening l夕情丶
4楼-- · 2020-02-04 07:19

I use this config and it works.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': '/etc/my.cnf',
        },
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
查看更多
淡お忘
5楼-- · 2020-02-04 07:21

as far as i understood someuser is a DB guest user and not the admin one, right?

If so, from your MySQL DB admin user, grant someuser to access MySQL environment table 'mysql.user' as follow:

GRANT SELECT ON mysql.user TO 'someuser'@'%';

To me it worked, Ivan

查看更多
小情绪 Triste *
6楼-- · 2020-02-04 07:26

In my case the settings.py has the following:

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'TRYDJANGO',
        'USERNAME':'user_trydjango',
        'PASSWORD':'passtry',
        'PORT':'3306',
        'HOST': 'localhost',
    }
}

And it works if I change the 'USERNAME' to 'USER':

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'TRYDJANGO',
        'USER':'user_trydjango',
        'PASSWORD':'passtry',
        'PORT':'3306',
        'HOST': 'localhost',
    }
}
查看更多
神经病院院长
7楼-- · 2020-02-04 07:26

I got the following error:

ModuleNotFoundError: No module named 'MySQLdb' django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?

Solution # 01: Verify that If you user has permission to access the database and perform the DDL & DML operations on it.

Solution # 02: Changed the database configuration in settings.py file

From:

DATABASES = {

'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'root'
    }
}

To:

DATABASES = {

'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

And it started working.

查看更多
登录 后发表回答