问题原文
原帖链接 · Quoc Dat · CC BY-SA 3.0
I am using docker-compose to create mysql container.
I get host IP 172.21.0.2.
But when I connect mysql. I get error:
My
docker-compose.yml:version: '2' services: ### Mysql container mysql: image: mysql:latest ports: - "3306:3306" volumes: - /var/lib/mysql:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: test_db MYSQL_USER: test MYSQL_PASSWORD: test_passGet my host IP
docker inspect db_mysql_1 | grep IPAddress"IPAddress": "172.21.0.2",Access mysql:
mysql -h 172.21.0.2 -P 3306 -u root -proot.ERROR 1130 (HY000): Host '172.21.0.1' is not allowed to connect to this MySQL server
How can I connect to mysql container?
已采纳解答
原帖链接 · Mr.Coffee · CC BY-SA 4.0
You can pass an extra environment variable when starting the MySQL container MYSQL_ROOT_HOST=<ip> this will create a root user with permission to login from given IP address. In case where you want to allow login from any IP you can specify MYSQL_ROOT_HOST=%.
This will work only for a newly created containers.
When spinning new container:
docker run --name some-mysql -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
In compose file it would be:
version: '2'
services:
### Mysql container
mysql:
image: mysql:latest
ports:
- "3306:3306"
volumes:
- /var/lib/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_db
MYSQL_USER: test
MYSQL_PASSWORD: test_pass
MYSQL_ROOT_HOST: '%' # needs to be enclosed with quotes
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
