gpt4 book ai didi

docker - Symfony4/Docker : SQLSTATE[HY000] [2002] Connection refused

转载 作者:行者123 更新时间:2023-12-02 18:48:27 30 4
gpt4 key购买 nike

我对 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 ###

这是我的包/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

我可能会错过什么?

最佳答案

我通过这样做解决了它:

  • 降级到 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

关于docker - Symfony4/Docker : SQLSTATE[HY000] [2002] Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52344603/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com