I am trying to create a MySQL database using Docker and need to add a new user (user=user1, password=user1). I have configured the docker-compose
file and set the environment variables accordingly:
version: '3.1'
services:
db:
image: mysql:8.0.33
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: my_test_database
MYSQL_USER: user1
MYSQL_PASSWORD: user1
volumes:
- ./database:/var/lib/mysql
However, whenever I run the setup, I encounter the following error message:
(pymysql.err.OperationalError) (1044, "Access denied for user
‘user1’@’%’ to database ‘my_test_database’") [SQL: USE Testtable1] (Background on this error at: https://sqlalche.me/e/14/e3q8)
How can I resolve this issue?
2
Answers
I managed to solve this problem. It seems that the administrator (root user) has to allow privileges to another user, in this case,
user1
. To do so, I followed these steps:user1
:After these steps,
user1
had all privileges to use the database.But although it was solved this way, I still don't understand because, based on the documentation here, it says:
I understand that by assigning this variable with the name of the database (as I did in the
docker-compose.yml
file, the user has all privileges automatically. However, it did not work in my case, and I had to solve it with the steps I explained above.Here is my Docker Compose file that is working with MySQL, perhaps there is an error because you didn’t name the database under volumes?