[英]Can't connect to mysql DB by doctrine on docker
Basicly I have a problem with doctrine.基本上我对教义有问题。 It is my first time that I set up a project on docker containers using doctrine and mysql.
这是我第一次使用学说和 mysql 在 docker 容器上建立一个项目。
So I have docker-compose.yml所以我有docker-compose.yml
version: '3'
services:
api:
build:
context: ./
dockerfile: Dockerfile
container_name: php
depends_on:
- database
volumes:
- ./:/var/www/html/
ports:
- 8000:80
networks:
- backend
database:
container_name: mysql
image: mysql:8
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_db
MYSQL_USER: user
MYSQL_PASSWORD: Test1234
ports:
- 3306:3306
networks:
- backend
networks:
backend:
driver: bridge
and this is my Dockerfile这是我的Dockerfile
FROM php:7.4.12-apache
RUN apt-get update && apt-get upgrade -y \
&& docker-php-ext-install mysqli pdo_mysql
RUN apt-get install -y libzip-dev
RUN docker-php-ext-install zip
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
EXPOSE 80
my .env looks like (rest is commented by # at the beginnig)我的.env看起来像(其余在开始时由 # 注释)
APP_ENV=dev
APP_SECRET=00b8dcf4c62a3db98abc4ae12dc4a6fd
DATABASE_URL=mysql://user:Test1234@localhost:3306/bf_db
note that there is no difference between localhost or 127.0.0.1 in DATABASE_URL
请注意,
DATABASE_URL
localhost 或 127.0.0.1 之间没有区别
now when I want to run this command: docker-compose exec api php bin/console doctrine:database:create
现在当我想运行这个命令时:
docker-compose exec api php bin/console doctrine:database:create
I recieve this kind of errors:我收到这种错误:
[error] Error thrown while running command "doctrine:database:create".
Message: "An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused"
In AbstractMySQLDriver.php line 112:
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
In Exception.php line 18:
SQLSTATE[HY000] [2002] Connection refused
In PDOConnection.php line 38:
SQLSTATE[HY000] [2002] Connection refused
And I really don't know how to handle this我真的不知道如何处理这个
there is no difference between localhost or 127.0.0.1 in DATABASE_URL
DATABASE_URL 中的 localhost 或 127.0.0.1 之间没有区别
Because those refer to the PHP contianer, not the other container.因为那些指的是 PHP 容器,而不是其他容器。
It needs to be database:3306
它需要是
database:3306
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.