早上好,
我配置了2个docker容器,使用相同的Dockerfile,但端口不同,请参阅配置
mariadb:
container_name: mariadb
image: project/mariadb
build:
context: .
dockerfile: ./docker/mariadb/Dockerfile
environment:
MYSQL_ROOT_PASSWORD: "$MYSQL_ROOT_PASSWORD"
MYSQL_DATABASE: "$MYSQL_DATABASE"
MYSQL_USER: "$MYSQL_USER"
MYSQL_PASSWORD: "$MYSQL_PASSWORD"
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
- ./services/mysql/utf8mb4.cnf:/etc/mysql/conf.d/utf8mb4.cnf:ro
mariadb_test:
container_name: mariadb_test
image: project/mariadb
build:
context: .
dockerfile: ./docker/mariadb/Dockerfile
environment:
MYSQL_ROOT_PASSWORD: "admin"
MYSQL_DATABASE: project_test
MYSQL_USER: foo
MYSQL_PASSWORD: bar
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
ports:
- "3317:3306"
volumes:
- ./services/mysql/utf8mb4.cnf:/etc/mysql/conf.d/utf8mb4.cnf:ro
我可以连接到mariadb(端口:3306(集装箱没有任何问题,参数保存在.env:中
mysql://$MYSQL_USER:$MYSQL_PASSWORD@$MYSQL_HOST:$MYSQL_PORT/$MYSQL_DATABASE
但如果我尝试连接到mariadb_test,我会得到
SQLSTATE[HY000] [2002] Connection refused
即使我将连接参数硬编码为:
mysql://foo:bar@mariadb_test:3317/project_test
我很沮丧。。。我做错了什么?
感谢@David Maze在评论中回答了我的问题:
容器之间的连接总是使用";正常的";港口;它们忽略端口:重新映射。在数据库连接字符串中使用标准MySQL端口3306。