连接被拒绝:Docker中的MariaDB

2022-06-25 00:00:00 docker mariadb mysql

早上好,

我配置了2个坞站容器,具有相同的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,他在评论中回答了我的问题:

容器之间的连接始终使用";Normal";端口;它们忽略端口:重新映射。在数据库连接字符串中使用标准MySQL端口3306。

相关文章