docker撰写MySQL容器[2002]连接被拒绝 [英] docker compose MySQL container [2002] Connection refused
问题描述
我正在尝试为Nginx + PHP(Laravel)+ MySQL构建docker compose容器,但我一直收到连接被拒绝的错误消息
I'm trying to build docker compose containers for Nginx + PHP(Laravel) + MySQL, but I keep getting Connection refused error
docker-compose.yml
version: "3"
services:
nginx:
image: nginx:latest
ports:
- '8080:80'
volumes:
- ./nginx:/etc/nginx/conf.d
- ./logs/nginx:/var/logs/nginx
- ./apps:/var/www/html
depends_on:
- php
restart: always
php:
image: laradock/php-fpm:2.2-7.2
volumes:
- ./apps:/var/www/html
restart: always
mysql:
image: mariadb
ports:
- '33060:3306'
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=root
restart: always
在 ./apps
中,我有一个带有基本SQL设置的全新Laravel应用
In ./apps
I have a brand new Laravel app with basic SQL setup
所有容器运行都没有错误,我可以使用以下方式连接到MySQL容器
All containers run without errors, and I can connect to the MySQL container using
mysql -u 127.0.0.1 -P 33060 -u root -p
但是当我尝试从浏览器访问或将ssh直接导入php容器时,我仍然收到错误2002.
but I keep getting error 2002 when I try to access from the browser or direct ssh into php container.
我在服务器上运行了nginx和mysql,所以我使用8080和33060端口,不确定是否是问题所在.
I have nginx and mysql running on the server so I use 8080 and 33060 port, not sure if that's the problem.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=secret
感谢您的帮助.
推荐答案
我的第一个猜测是试图找到mysql unix快死了套接字文件,需要通过非本地主机进行连接时联系.如果您使用撰写文件启动它们,则您可以连接到docker提供的dns名称(即,链接:docker-mysql,将dns名称设置为docker-mysql).所以你必须更改您的连接线:
My first guess is that it is dying trying to find the mysql unix socket file, when it needs to be connecting over a non-localhost connection. If you are using the compose file to start them, then you can connect to the dns name provided by docker (ie, links: docker-mysql, sets the dns name to be docker-mysql). So you have to change your connection line:
-php -r新的PDO('mysql:host = localhost; port = 3306; charset = utf8','root','123123');'
+ php -r新的PDO('mysql:host = docker-mysql; port = 3306; charset = utf8','root','123123');'
>
通过将 DB_HOST
更改为 .env
文件
DB_HOST=docker_mysql_1
这篇关于docker撰写MySQL容器[2002]连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!