0

I am using docker-compose along with django and mysql, and I want in the custom entrypoint of django I want to do tests via python manage.py test. Since I am connected to mysql, by default it goes to look for the database test_NAMEDATABASE, but it doesn't find it. So I tried to create it as it says here, but I have several problems with permissions. I think this is a very common situation, how did you solve it? This is my mysql service in docker-compose.yml:

  db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
  - 3306:${MYSQL_DATABASE_PORT}
environment:
  - MYSQL_RANDOM_ROOT_PASSWORD
  - MYSQL_USER
  - MYSQL_PASSWORD
  - MYSQL_DATABASE
  - MYSQL_DATABASE_HOST
  - MYSQL_DATABASE_PORT
volumes:
  - mysql_data:/var/lib/mysql

And this is my settings.py settings in django:

    'default': {
    'ENGINE': 'django.db.backends.mysql', 
    'NAME': os.environ.get('MYSQL_DATABASE'),
    'USER': os.environ.get('MYSQL_USER'),
    'PASSWORD': os.environ.get('MYSQL_PASSWORD'),
    'HOST': os.environ.get('MYSQL_DATABASE_HOST'),   # Or an IP Address that your DB is hosted on
    'PORT': os.environ.get('MYSQL_DATABASE_PORT')
}

It gives me this error when I try to manually create the database "test_NAMEDATABASE" inside the mysql container: "Access denied for user 'django'@'%' to database 'test_NAMEDATABASE"

Heading

3
  • Do you have values for the environment variables? Shouldn't it be - MYSQL_PASSWORD: password et c. Commented Feb 10, 2023 at 16:32
  • 1
    Yes, the variables are set correctly. They are taken from the .env file I have in my project. In fact everything works if I don't do python manage.py test Commented Feb 10, 2023 at 17:17
  • Access denied means that either the username and/or the password is not correct. Commented Feb 10, 2023 at 18:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.