EntityManager-> flush Connection拒绝使用docker的HY000 2002

时间:2019-09-21 19:01:52

标签: symfony docker doctrine

当我将symfony与xampp结合使用时,没有问题。

但是,当我将symfony和docker结合使用时,下面会出现问题。

An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
Doctrine\DBAL\Exception\
ConnectionException
in vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php (line 93)
in vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php->convertException (line 169)
in vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php::wrapException (line 155)
in vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php::driverException (line 28)
in vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php->connect (line 356)
in vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php->connect (line 1236)
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php->beginTransaction (line 376)
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php->commit (line 359)

EntityManager->flush() in src/Controller/ArticleController.php (line 36)

$article->setTitle('Article One');            
$article->setBody('This is the body for article one');            
$entityManager->persist($article);            

$entityManager->flush();            

return new Response('Saves an article with the id of '.$article->getId());        

我在XAMPP和docker中的symfony项目完全相同。这就是为什么我认为DockerFile或docker-compose.yml或任何教义或symfony设置文件中存在问题

此问题仅在以下行发生:

EntityManager->flush()

没有问题:

php bin/console doctrine:database:create
php bin/console doctrine:migrations:migrate

这是我的设置:

doctrine.yaml

doctrine:
    orm:
        auto_generate_proxy_classes: false
        metadata_cache_driver:
            type: service
            id: doctrine.system_cache_provider
        query_cache_driver:
            type: service
            id: doctrine.system_cache_provider
        result_cache_driver:
            type: service
            id: doctrine.result_cache_provider
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '8.0.15'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'

services:
    doctrine.result_cache_provider:
        class: Symfony\Component\Cache\DoctrineProvider
        public: false
        arguments:
            - '@doctrine.result_cache_pool'
    doctrine.system_cache_provider:
        class: Symfony\Component\Cache\DoctrineProvider
        public: false
        arguments:
            - '@doctrine.system_cache_pool'

framework:
    cache:
        pools:
            doctrine.result_cache_pool:
                adapter: cache.app
            doctrine.system_cache_pool:
                adapter: cache.system

DockerFile

FROM php:7.3.0-apache

RUN curl -sS http://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
    && apt-get update && apt-get install -y git libzip-dev  unzip \
    && docker-php-ext-install zip \
    && docker-php-ext-install pdo pdo_mysql zip \
    && a2enmod rewrite headers

COPY . /var/www/html

WORKDIR /var/www/html/app

RUN composer install

docker-compose.yml

version: '3.4'

volumes:
  cache:
  log:

services:
  app:
    ports:
      - 80:80
    environment:
      - APACHE_DOCUMENT_ROOT=/var/www/html/app/public
    build:
      context: .
    volumes:
      - .:/var/www/html
      - ./config/vhost.conf:/etc/apache2/sites-available/000-default.conf
      - cache:/var/www/html/app/var/cache
      - log:/var/www/html/app/var/log

我虽然也许可以解决我的问题 我试图在docker镜像中更改make php.ini来做到这一点: PHP docker pdo_mysql driver not found

但是,当我重新启动docker映像时:

docker-compose down
docker-compose up -d

我的php.ini不再显示图像。

我希望flush()应该像xampp一样成功 有人可以帮我吗?

0 个答案:

没有答案