我有这个容器
services:
php:
container_name: user-php
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/var/www/html
- ./xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
ports:
- "80:80"
depends_on:
- mysql
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
XDEBUG_MODE: "develop,debug"
XDEBUG_CONFIG: "client_host=host.docker.internal start_with_request=yes"
mysql:
container_name: user-mysql
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: user_database
MYSQL_USER: my_user
MYSQL_PASSWORD: my_password
ports:
- "3306:3306"
volumes:
- database_data:/var/lib/mysql
volumes:
database_data:
我可以使用
docker exec
和使用 dbbeaver-ce
连接到数据库,但是当我尝试在 php 容器上执行 php bin/console make:migrations
时,我收到此错误消息
[critical] Error thrown while running command "make:migration". Message: "An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused"
In ExceptionConverter.php line 101:
An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused
In Exception.php line 28:
SQLSTATE[HY000] [2002] Connection refused
In Driver.php line 34:
SQLSTATE[HY000] [2002] Connection refused
这是
.env
文件上的连接网址
DATABASE_URL="mysql://root:[email protected]:3306/user_database?serverVersion=8.0.39&charset=utf8mb4"
我将学说实体映射更改为 xml,但这应该不是问题。
在docker内部,使用容器名称而不是127.0.0.1。在你的情况下,它是
root:root@mysql:3306/...