英文:
Access denied for user when creating MySQL database with Docker
问题
我正在尝试使用Docker创建一个MySQL数据库,并需要添加一个新用户(用户=user1,密码=user1)。我已配置了docker-compose
文件并相应地设置了环境变量:
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
然而,每当我运行设置时,我遇到以下错误消息:
> (pymysql.err.OperationalError) (1044, "Access denied for user
> 'user1'@'%' to database 'my_test_database'") [SQL: USE Testtable1]
> (此错误的详细信息可在此链接中找到:https://sqlalche.me/e/14/e3q8)
我该如何解决这个问题?
英文:
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?
答案1
得分: 1
我成功解决了这个问题。似乎管理员(root用户)必须允许另一个用户拥有权限,这种情况下是 user1
。为此,我按照以下步骤操作:
- 删除容器并重新启动:
docker compose down
docker-compose up -d
- 以新用户身份登录,并显示分配的权限:
docker-compose exec db mysql -u user1 -p
SHOW GRANTS FOR 'user1'@'%';
- 再次以root用户身份登录(管理员以授权该用户
user1
的权限):
docker-compose exec db mysql -u root -p
GRANT ALL PRIVILEGES ON my_test_database.* TO 'user1'@'%';
FLUSH PRIVILEGES;
经过这些步骤后,user1
具备了使用数据库的所有权限。
但尽管通过这种方式解决了问题,我仍然不明白,因为根据这里的文档,它说:
MYSQL_DATABASE 此变量是可选的,允许您指定在镜像启动时要创建的数据库的名称。如果提供了用户/密码(请参见下文),那么该用户将被授予超级用户访问权限(相当于 GRANT ALL)到该数据库。
我理解通过将这个变量分配为数据库的名称(就像我在 docker-compose.yml
文件中所做的那样),用户应该自动具备所有权限。然而,在我的情况下并没有起作用,我不得不按照我上面解释的步骤来解决它。
英文:
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:
- Eliminate the container and up again this way:
docker compose down
docker-compose up -d
- Enter with the new user, and show the GRANTS assigned:
docker-compose exec db mysql -u user1 -p
SHOW GRANTS FOR 'user1'@'%';
- Enter again as root user (administrator to grant privilege to this user
user1
:
docker-compose exec db mysql -u root -p
GRANT ALL PRIVILEGES ON my_test_database.* TO 'user1'@'%';
FLUSH PRIVILEGES;
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:
> MYSQL_DATABASE This variable is optional and allows you to specify the
> name of a database to be created on image startup. If a user/password
> was supplied (see below) then that user will be granted superuser
> access (corresponding to GRANT ALL) to this database.
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.
答案2
得分: 0
以下是我Docker Compose文件的一部分,它正在与MySQL一起工作。也许有一个错误,因为您没有在volumes下命名数据库?
version: '3.8'
services:
db:
image: mysql:latest
restart: always
environment:
MYSQL_DATABASE: 'db'
MYSQL_USER: 'username'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- my-db:/var/lib/mysql
volumes:
my-db:
英文:
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?
version: '3.8'
services:
db:
image: mysql:latest
restart: always
environment:
MYSQL_DATABASE: 'db'
MYSQL_USER: 'username'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- my-db:/var/lib/mysql
volumes:
my-db:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论