symfony 通过PHP(在docker上运行)连接RDS(mariadb)失败

dy1byipe  于 2023-10-24  发布在  PHP
关注(0)|答案(1)|浏览(148)

我正在使用EC2示例通过docker运行PHP(Symfony)应用。这是docker compose文件:

version: "3.7"

services:
  traefik:
    image: traefik:1.7
    container_name: so-traefik
    command: --docker --docker.exposedbydefault=false
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  app:
    image: thecodingmachine/php:7.2-v4-apache
    container_name: so-app
    labels:
      - traefik.enable=true
      - traefik.backend=app
      - traefik.frontend.rule=Host:so.localhost
    environment:
      APACHE_DOCUMENT_ROOT: web
      PHP_INI_MEMORY_LIMIT: 1G
      PHP_INI_ERROR_REPORTING: E_ALL
      PHP_INI_UPLOAD_MAX_FILESIZE: 512M
      PHP_INI_POST_MAX_SIZE: 512M
      PHP_INI_MAX_INPUT_VARS: 4096
      PHP_INI_MAX_EXECUTION_TIME: 500
      PHP_EXTENSION_GD: 1
      PHP_EXTENSION_IMAGICK: 1
      PHP_EXTENSION_INTL: 1
      PHP_EXTENSION_XDEBUG: 0
      PHP_EXTENSION_BCMATH: 1
      PHP_INI_XDEBUG__REMOTE_AUTOSTART: 0
      PHP_INI_XDEBUG__MAX_NESTING_LEVEL: 5000
      SYMFONY_ENV: prod
    working_dir: /var/www/html
    volumes:
      - "./website:/var/www/html"
      - "./website/vendor:/var/www/html/vendor:cached"

我可以通过docker CLI连接到RDS,例如,docker@724cef2e8b7a:/var/www/html$ mysql -h ${HOST} -P 3306 -u ${USER} -D ${DB} -p,其中HOST是AWS RDS端点成功,因此容器可以很好地解析主机名。但是当运行任何尝试访问数据库的Symfony命令时,我会收到连接错误。例如:

docker@724cef2e8b7a:/var/www/html$ php bin/console doctrine:query:sql 'SELECT * FROM product;'
11:27:30 ERROR     [console] Error thrown while running command "doctrine:query:sql 'SELECT * FROM product;'". Message: "An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known" ["exception" => Doctrine\DBAL\Exception\ConnectionException { …},"command" => "doctrine:query:sql 'SELECT * FROM product;'","message" => "An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known"]

In AbstractMySQLDriver.php line 103:
                                                                                                                                  
  An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known  
                                                                                                                                  

In PDOConnection.php line 47:
                                                                                                  
  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known  
                                                                                                  

In PDOConnection.php line 43:
                                                                                                  
  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known  
                                                                                                  

In PDOConnection.php line 43:
                                                                                               
  PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known

parameters.dist文件使用了正确的数据库主机名和选项(由于显而易见的原因,没有公开它们):

# This file is auto-generated during the composer install
parameters:
    database_host: ${DB_HOST}
    database_port: 3306
    database_name: ${DB_NAME}
    database_user: ${DB_USER}
    database_password: ${DB_PASS}
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    secret: ThisTokenIsNotSoSecretChangeIt
    mail_from_email: null
    mail_from_name: null
    portal_url: null
    portal_asset_url: null
    wkhtmltopdf_binary: /usr/local/bin/wkhtmltopdf.sh
    sendgrid_api_key: ${SENDGRID_KEY}
gopyfrb3

gopyfrb31#

我不知道到底是哪里搞砸了,但在清除了该高速缓存后,连接成功了:
php bin/console cache:clear
现在php bin/console doctrine:query:sql 'SELECT * FROM table;'返回预期的数据

相关问题