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

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号