Symfony4 / Docker:SQLSTATE [HY000] [2002]连接被拒绝
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Symfony4 / Docker:SQLSTATE [HY000] [2002]连接被拒绝相关的知识,希望对你有一定的参考价值。
我有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:db_password@127.0.0.1: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: 'AppEntity'
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
我能错过什么?
答案
我解决了这个问题:
- 降级到mysql 5.7
- 从docker-compose.yml删除命令:[“ - default-authentication-plugin = mysql_native_password”]
- 在.env文件中对DATABASE_URL中的凭据进行编码,并引用“mysql”(来自docker compose的服务名称)而不是127.0.0.1 DATABASE_URL = MySQL的:// sf4user:sf4password @ MySQL的:3306 / SF4
以上是关于Symfony4 / Docker:SQLSTATE [HY000] [2002]连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章
AIX上的DB2,db2客户端连接报错:CODE=-4214,SQLSTAT=28000 密码已到期错误解决方案
Symfony 4.4 FOS user-bundle api/auth/login 在 null 上调用成员函数 findOneBy()