Symfony无法连接到MySQL Docker容器 [英] Symfony can't connect to MySQL Docker container
问题描述
我在Mac OS sierra上运行了三个Docker容器,分别是 web
, mysql
和 mongo
,并将 mongo
和 mysql
都链接到网络
,本质上是一个Ubuntu Xenail基础,添加了Apache和PHP。
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.
我目前正在将本地的Symfony项目安装到 web
容器,这似乎很好,但是当我尝试以任何方式与数据库交互时,我得到:
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:
驱动程序中发生异常:SQLSTATE [HY000] [2002]连接
被拒绝
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?
我我在学习Docker的过程中,请原谅我有限的知识。
I'm in the process of learning Docker, so please excuse my limited knowledge.
谢谢!
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
运行命令:
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
,但仍收到相同的错误。
So I've moved over to using docker-compose
, but am still receiving the same error.
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
推荐答案
驱动程序中发生异常:SQLSTATE [HY000] [2002]连接被拒绝
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.
您缺乏的是如何创建用户(如果已创建用户) https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L122 ..实际上本身没有主机限制。
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.
您遇到的问题是, sql /文件夹中的内容是什么-这些脚本在入口点期间执行。
The question in your case is, what is inside your "sql/" folder - those scripts are executed during the entrypoint.
确保切勿在这些脚本中使用exitX,它们会中断主脚本,请参见 https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L151
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
检查docker日志中是否存在mysql,以确保脚本未向您显示任何警告,请使用 https://github.com/docker-library/ mysql / blob / c207cc19a272a6bfe1916c964ed8df47f18479e7 / 5.7 / docker-entrypoint.sh 作为参考。
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.
最后但并非最不重要的是,请使用 docker-compose 。如果您在时间安排上有问题(mysql开始变慢并且您的Web容器出现异常),请在Web中使用 wait for mysql入口点:
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
设置 db_host
, $ user
, $ pasword
相应地使用ENV或适合您的任何内容。
Set db_host
, $user
, $pasword
accordingly using ENV or whatever suits you.
这篇关于Symfony无法连接到MySQL Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!