mysql container connection refused

时间:2019-03-19 15:10:37

标签: mysql symfony docker

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.

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:

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:

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?

1 个答案:

答案 0 :(得分:2)

When you expose the port 3306 of your mysql container and map it to port 3200 this means that the outside world should access it using the port 3200. But other containers using the same network must use the port 3306.