简体   繁体   English

mysql容器连接被拒绝

[英]mysql container connection refused

i have a docker-compose with a mysql service, it work fine when i establish the connection with mysql workbench, but when i trying to connect with my symfony app, it throw me an connection refused error. 我有一个使用mysql服务的docker-compose,当我与mysql工作台建立连接时,它工作正常,但是当我尝试与我的symfony应用程序连接时,它使我拒绝连接。

The host, port, user and password are correctly setted. 正确设置了主机,端口,用户和密码。

this is my compose: 这是我写的:

version: "3.1"

services:

  mysql:
    build:
      context: .
      dockerfile: docker/mysql/Dockerfile
    restart: always
    working_dir: /app
    volumes:
      - .data:/usr/data
    environment:
      - MYSQL_ROOT_PASSWORD=000000
      - MYSQL_DATABASE=my_database
      - MYSQL_USER=admin
      - MYSQL_PASSWORD=000000
    ports:
      - "3200:3306"

  webserver:
    image: nginx:alpine
    working_dir: /app
    volumes:
      - .:/app
      - ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - '8000:80'
    depends_on:
      - php-fpm

  php-fpm:
    build: docker/php-fpm
    working_dir: /app
    volumes:
      - .:/app
      - ./docker/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini
    links:
      - mysql
    depends_on:
      - mysql

this is the dockerfile for mysql: 这是mysql的dockerfile:

FROM mysql:5.7


ENV MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
ENV MYSQL_DATABASE=${MYSQL_DATABASE}
ENV MYSQL_USER=${MYSQL_USER}
ENV MYSQL_PASSWORD=${MYSQL_PASSWORD}

COPY ./docker/mysql/my_database.sql /docker-entrypoint-initdb.d/init.sql

EXPOSE 3306

this are my symfony config params: 这是我的symfony配置参数:

parameters:
    database_driver: pdo_mysql
    database_host: mysql
    database_port: 3200
    database_name: my_database
    database_user: admin
    database_password: '000000'

and this is the error throwed: 这是抛出的错误:

"exception": {
        "message": "An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused",
        "code": 0,
        "previous": {
            "message": "SQLSTATE[HY000] [2002] Connection refused",
            "code": 2002,
            "previous": {
                "message": "SQLSTATE[HY000] [2002] Connection refused",
                "code": 2002,
                "trace": [
                    {
                        "namespace": "",
                        "short_class": "",
                        "class": "",
                        "type": "",
                        "function": "",
                        "file": "/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php",
                        "line": 43,
                        "args": []
                    },
                    {
                        "namespace": "",
                        "short_class": "PDO",
                        "class": "PDO",
                        "type": "->",
                        "function": "__construct",
                        "file": "/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php",
                        "line": 43,
                        "args": [
                            [
                                "string",
                                "mysql:host=mysql;port=3200;dbname=my_database;charset=UTF8;"
                            ],
                            [
                                "string",
                                "admin"
                            ],
                            [
                                "string",
                                "000000"
                            ],
                            [
                                "array",
                                []
                            ]
                        ]
                    }

what can be the problem? 可能是什么问题?

当您暴露mysql容器的端口3306并将其映射到端口3200时,这意味着外界应该使用端口3200访问它。但是使用同一网络的其他容器必须使用端口3306。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM