[英]Apache with Docker shows a [2002] Can't connect to local MySQL server
I use Mac OSX and MAMP. 我使用Mac OSX和MAMP。 I wanted to make a Docker Container for a web project, because it uses a php 5.5 version, and i didn't want to make changes on my MAMP.
我想为一个Web项目制作一个Docker容器,因为它使用的是PHP 5.5版本,并且我不想在MAMP上进行更改。 So here is the docker-compose.yml
这是docker-compose.yml
version: '2'
services:
www:
build: "./docker/apache"
volumes:
- .:/var/www
- ./docker/silexx.conf:/etc/apache2/sites-enabled/silexx.conf
working_dir: "/var/www"
command : "bash -c 'source /etc/apache2/envvars; apache2 -DFOREGROUND'"
ports:
- 80:80
links:
- mysql
mysql:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'silexx_dev'
MYSQL_ROOT_PASSWORD: 'root'
ports:
- 3306:3306
pma:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
restart: always
ports:
- 8080:80
volumes:
- /sessions
This is the Dockerfile : 这是Dockerfile:
FROM debian:jessie
RUN apt-get update
RUN apt-get install -y --force-yes apt-transport-https apache2 php5 php5-cli libicu-dev php5-gd php5-curl php5-mysql php5-memcached php5-intl php5-json php5-mcrypt curl sudo
RUN a2enmod rewrite
# install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get install -y --force-yes make git locales gcc g++
RUN echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
RUN locale-gen
ENV LC_ALL en_US.UTF-8
RUN rm /etc/apache2/sites-*/*
COPY default /etc/apache2/sites-available/default.conf
RUN a2ensite default
CMD "source /etc/apache2/envvars && apache2 -DFOREGROUND"
ENV LANG en_US.UTF-8
CMD "/etc/init.d/mysql start"
# forward request and error logs to docker log collector
RUN ln -sf /proc/1/fd/1 /var/log/apache2/access.log
RUN ln -sf /proc/1/fd/2 /var/log/apache2/error.log
EXPOSE 80
And I made a little vhost : 我做了一个小鬼:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/var/www/core/"
ServerName demo.silexx.local
ServerAlias demo.silexx.local
#ErrorLog "logs/silexx.com-error_log"
#CustomLog "logs/silexx.com-access_log" common
</VirtualHost>
Then I make docker-compose up, I generate my containers. 然后我使docker-compose起来,生成容器。
When I go to the console and mysql -h localhost -P 3306 --protocol=tcp -u root -proot 当我转到控制台和mysql -h localhost -P 3306 --protocol = tcp -u root -proot
It works perfectely ! 它完美地工作!
But in my navigator I have a 2002 error ... SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 但是在我的导航器中,我遇到了2002错误... SQLSTATE [HY000] [2002]无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器(2)
But I've modified my credentials ... 但是我修改了我的凭证...
define('DB_HOST', 'localhost');
define('DB_INTRANET', 'silexx_dev');
define('DB_INTRANET_UTI', 'root');
define('DB_INTRANET_PWD', 'root');
$oPDO = new PDO ('mysql:host=' . DB_HOST . ';dbname=' . DB_INTRANET, DB_INTRANET_UTI, DB_INTRANET_PWD);
$oPDO -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Honestely, I don't understand ... :s Have you got an idea please ? 老实说,我听不懂...:s请问您有个主意吗?
Thanks. 谢谢。
You must change the app's host
database config to the name of the database container that is linked to your app. 您必须将应用程序的
host
数据库配置更改为链接到应用程序的数据库容器的名称。
Here, in your example, the container name is mysql
, therefore you need this change: 在这里,在您的示例中,容器名称为
mysql
,因此您需要进行以下更改:
-define('DB_HOST', 'localhost');
+define('DB_HOST', 'mysql');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.