dongliang7545
2016-12-29 21:23
浏览 76
已采纳

Symfony无法连接到MySQL Docker容器

I have three Docker containers running on Mac OS sierra, namely web, mysql and mongo, and have linked both mongo and mysql into web, which is essentially a Ubuntu Xenail base, with Apache and PHP added.

I am currently mounting my local Symfony project into the web container, and that seems to be working fine, but when I try to interact with the DB in any way, I get:

An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

I've tried almost every combination of parameter values, but keep getting the same result.

I suspect it might have something to do with the way that I am linking the containers?

I'm in the process of learning Docker, so please excuse my limited knowledge.

Thanks!

Web dockerfile:

FROM ubuntu:xenial
MAINTAINER Some Guy <someguy@domain.com>

RUN apt-get update && apt-get install -y \
apache2 \
vim \
php \
php-common \
php-cli \
php-curl \
php-mysql \
php-mongodb \
libapache2-mod-php \
php-gd

RUN mkdir -p /var/www/symfony.local/public_html
RUN chown -R $USER:$USER /var/www/symfony.local/public_html

RUN chmod -R 755 /var/www

COPY config/php/php.ini /usr/local/etc/php/
COPY config/apache/sites-available/*.conf /etc/apache2/sites-available/

RUN a2enmod rewrite

RUN a2dissite 000-default.conf
RUN a2ensite symfony.local.conf

EXPOSE  80

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

Mysql dockerfile:

FROM mysql:5.7
MAINTAINER Some Guy <someguy@domain.com>

# Set the root users password
ENV MYSQL_ROOT_PASSWORD password

# Copy over the DB dump to be run upon creation
COPY sql/ /docker-entrypoint-initdb.d

# Copy over the custom mysql config file
COPY config/ /etc/mysql/conf.d

EXPOSE 3306

Run commands:

docker run --name mongo -d mongo #Im making use of the official Mongo image
docker run --name mysql -v /usr/local/var/mysql:/var/lib/mysql -d someguy/local:mysql
docker run --name web -d -p 80:80 --link mysql:mysql --link mongo:mongo -v ~/Sites/symfony.local/:/var/www/symfony.local/public_html/ someguy/local:web

Symfony parameters.yml file:

parameters:
    database_host: mysql
    database_port: 3306
    database_name: gorilla
    database_user: root
    database_password: password

UPDATE:

So I've moved over to using docker-compose, but am still receiving the same error.

docker-compose.yml file

version: "2"
services:
  web:
    build: ./web
    ports:
      - "80:80"
    volumes:
      - ~/Sites/symfony.local/:/var/www/symfony.local/public_html/
    depends_on:
      - db
      - mongo
  mongo:
    image: mongo:latest
  mysql:
    image: mysql:latest
    ports:
        - "3306:3306"
    environment:
        MYSQL_ROOT_PASSWORD: password
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongluobei9359 2016-12-29 23:33
    已采纳

    An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

    Means, it has nothing to do with your network per se - the links are just fine.

    What you are lacking is the how the user has been created, if the user has been created https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L122 .. so actually without a host limitation per se.

    The question in your case is, what is inside your "sql/" folder - those scripts are executed during the entrypoint.

    Be sure to never use exitX in those scripts, they will interrupt the main script, see https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L151

    Check your docker logs for mysql to ensure the script did not print you any warnings, use https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh as an reference.

    And last but not least, please use docker-compose. If you have issues with the timings ( mysql starting to slow and your web-container freaks out ), use a "wait for mysql" entrypoint in web:

    #!/bin/bash
    # this script does only exist to wait for the database before we fire up tomcat / standalone
    
    RET=1
    echo "Waiting for database"
    while [[ RET -ne 0 ]]; do
        sleep 1;
        if [ -z "${db_password}" ]; then
            mysql -h $db_host -u $db_user -e "select 1" > /dev/null 2>&1; RET=$?
        else
            mysql -h $db_host -u $db_user -p$db_password -e "select 1" > /dev/null 2>&1; RET=$?
        fi
    done
    

    Set db_host, $user, $pasword accordingly using ENV or whatever suits you.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题