2

Well, I have multiple databases in django project. One is default and second istest_dev. i have created docker container. and then everything went well till building the container. after that When I try to call my django app api which is using test_dev db for reading data. I have encountered:

OperationalError at /api/projects/

(1045, "Access denied for user 'test_dev'@'xxx.xxx.xxx.xxx' (using password: YES)") 

In settings file, my databases configuration:

{
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': Path(__file__).resolve().parent.parent / 'db.sqlite3',
    },
    "test_dev" : {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "test",
        'USER': 'test_dev',
        'PASSWORD': 'db_password',
        'HOST': '?=hosted_domain',
        'PORT': '3306',
        'SSL': True
        }
}

If you know any solution. Please help me out of this. Soon I have to put things on production..

Ahmed Yasin
  • 886
  • 1
  • 7
  • 31

3 Answers3

4

This is an issue with database access privileges. Either the password is wrong, the user was not granted permissions to the specific database or table that you need, or the Django container's IP address does not match the one bound to the user.

The following SQL:

  1. ensures that the user exists and that login is permitted for that user from any IP address,
  2. ensures that the user has all privileges on the database
CREATE USER IF NOT EXISTS
  'test_dev'@'%' IDENTIFIED BY 'db_password';

GRANT ALL PRIVILEGES
  ON test.*
  TO 'test_dev'@'%';
Codebling
  • 10,764
  • 2
  • 38
  • 66
3

Well I solved my problem by changing the engine. I replaced the following line:

'ENGINE': 'django.db.backends.mysql',

with :

 'ENGINE': 'mysql.connector.django',

this code solve my current error but after this i have face an other error that i solve by editing requirements.txt file:

mysql-connector-python==8.0.27

to :

mysql-connector-python==8.0.26
Ahmed Yasin
  • 886
  • 1
  • 7
  • 31
0

I believe that while creating the database for the container, your user has not get the desired permissions, to give permissions use the below snippet.

In your terminal:

mysql -h 127.0.0.1 -P 3306 -u root -p

password is: password (default)

After that, show databases; and use test_dev;

Now, you can check grants by using show grants; command, since the user for all hosts does not have any permissions (or maybe there is no user at all), so use the below command to give privileges:

CREATE USER IF NOT EXISTS 'test_dev'@'%' IDENTIFIED BY 'db_password';

GRANT ALL PRIVILEGES ON test.* TO 'test_dev'@'%';
Ritik Banger
  • 2,107
  • 5
  • 25