Symfony 4生成SQLSTATE [HY000] [2002] Connection拒绝使用Docker容器

时间:2018-03-19 00:44:14

标签: docker doctrine-orm docker-compose symfony4

我有2个Docker容器,使用以下配置:

version: '2'

volumes:
  db_data: {}

services:

  db:
    container_name: database-mysql-5.7
    build:
      context: ./docker/db/
    ports:
      - "3321:3306"
    volumes:
      - db_data:/var/lib/mysql

  web:
    container_name: apache-symfony-4
    build:
      context: ./docker/web/
    image: php
    links:
      - db
    depends_on:
      - db
    ports:
      - "8021:80"
    volumes:
      - ./app/:/var/www/symfony

我可以从本地主机(Mac OS)连接到db容器,我可以从Web容器连接到db容器(并成功执行sql语句)。另外,当我执行web容器时,我可以成功执行doctrine:migrations:diff命令。

但是,当我尝试运行如下代码时:

$User = $this->getDoctrine()
                ->getRepository(User::class)
                ->findAll();

我收到以下错误:

An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused

octrine\DBAL\Exception\ConnectionException:
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused

  at vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:108
  at Doctrine\DBAL\Driver\AbstractMySQLDriver->convertException('An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused', object(PDOException))
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:176)
  at Doctrine\DBAL\DBALException::wrapException(object(Driver), object(PDOException), 'An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused')
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:161)
  at Doctrine\DBAL\DBALException::driverException(object(Driver), object(PDOException))
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php:47)

以下是我的.env文件的内容

# This file is a "template" of which env vars need to be defined for your application
# Copy this file to .env file for development, create environment variables when deploying to production
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=7c2deaf7464ad40b484d457e02a56918
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
#TRUSTED_HOSTS=localhost,example.com
###< symfony/framework-bundle ###

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###

###> 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://hereswhatsontap:####@db:####/hereswhatsontap
###< doctrine/doctrine-bundle ###

此外,如果我尝试使用DBAL,它会返回相同的错误:

/**
     * @Route("/test")
     */
    public function testAction(Connection $connection){
        $users = $connection->fetchAll('SELECT * FROM users');

        return $this->render("startup/startup.html/twig", [
            "NewUser" => $users
        ]);
    }

尝试使用root用户名/密码也失败了。

好的,上次补充数据(我保证:)

执行以下代码可以在Symfony的/ public文件夹中正常工作

<?php
$dsn = 'mysql:host=db;dbname=hereswhatsontap;';
$username = 'hereswhatsontap';
$password = '####';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);

$dbh = new PDO($dsn, $username, $password, $options);

$sql = "SELECT * from user";
$statement = $dbh->prepare($sql);
$statement->execute();
$users = $statement->fetchAll(2);
print_r($users);
?>

2 个答案:

答案 0 :(得分:1)

非常感谢您的评论,这为我节省了更多的研究时间。我只想像我一样在Docker中添加新手,如果您想获得容器IP,则可以运行以下命令(这样,您就不必在仅使用inspect即可获得的整个配置转储中查找):

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' YOURCONTAINERID

再次感谢!

答案 1 :(得分:0)

这个问题是由于我对容器之间如何相互沟通完全缺乏了解。

Web容器和db容器基本上位于它们自己的“vpn”网络上,这意味着Web容器可以直接与db通信,然后根据我的配置使用内部端口(3306)。

因此从我的.env文件的DATABASE_URL值中移除端口(我作为外部端口)就是答案。

党!