我有Symfony 4和数据库连接的问题。我尝试使用php bin/console make:migration
进行迁移,但后来我面临连接拒绝错误。
In AbstractMySQLDriver.php line 113:
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
In PDOConnection.php line 50:
SQLSTATE[HY000] [2002] Connection refused
In PDOConnection.php line 46:
SQLSTATE[HY000] [2002] Connection refused
我的.env文件是这样的。
###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=mysql://db_user:[email protected]:3306/db_name
DATABASE_USER=sf4user
DATABASE_PWD=sf4pwd
DATABASE_NAME=sf4
DATABASE_HOST=127.0.0.1
DATABASE_PORT=3306
###< doctrine/doctrine-bundle ###
这是我的packages / doctrine.yaml:
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
host: '%env(DATABASE_HOST)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PWD)%'
dbname: '%env(DATABASE_NAME)%'
port: '%env(DATABASE_PORT)%'
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
我明白数据库URL是基于我在.env文件中设置的那样解决的,但我无法使其工作,每当我尝试调用处理db的控制台方法时,我都会得到连接拒绝错误。
我使用docker-compose.yml文件构建项目:
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4
depends_on:
- php
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
command: ["--default-authentication-plugin=mysql_native_password"]
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root_pwd_test
MYSQL_DATABASE: sf4
MYSQL_USER: sf4user
MYSQL_PASSWORD: sf4pwd
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
ports:
- 8080:80
links:
- mysql
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root_pwd_test
PMA_HOST: mysql
我能错过什么?
我解决了这个问题: