Symfony无法连接到MySQL Docker容器
我有三个在Mac OS sierra上运行的Docker容器,即web
, mysql
和mongo
,并且已经将mongo
和mysql
链接到了web
,这基本上是一个Ubuntu Xenail基础,添加了Apache和PHP。
我目前正在将我的本地Symfony项目安装到web
容器中,似乎工作正常,但是当我尝试以任何方式与数据库进行交互时,我会得到:
驱动程序发生exception:SQLSTATE [HY000] [2002]连接被拒绝
我已经尝试了几乎所有的参数值组合,但是不断得到相同的结果。
我怀疑这可能与我连接容器的方式有关。
我正在学习Docker,所以请原谅我有限的知识。
谢谢!
网站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
运行命令:
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文件:
parameters: database_host: mysql database_port: 3306 database_name: gorilla database_user: root database_password: password
更新:
所以我已经转移到使用docker-compose
,但仍然收到相同的错误。
docker-compose.yml文件
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
驱动程序发生exception:SQLSTATE [HY000] [2002]连接被拒绝
意思是,它本身与你的networking本身没有任何关系 – 链接就好了。
你缺乏的是如何创build用户,如果用户已经创buildhttps://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L122 ..所以实际上没有主机限制本身。
你的情况的问题是,什么是你的“sql /”文件夹 – 这些脚本在入口点执行。
请务必不要在这些脚本中使用exitX,它们会中断主脚本,请参阅https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L151
检查您的docker日志为了确保脚本没有打印任何警告,请使用https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh作为参考。
最后但并非最不重要的是,请使用docker-compose 。 如果你的时间有问题(mysql开始慢,你的web容器怪异),在web中使用“等待mysql”入口点:
#!/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
相应地使用ENV或任何适合你的设置db_host
, $user
, $pasword
。