I have 2 Docker containers using the following configuration:
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
I can connect to the db container from my local host (Mac OS) and I can connect to the db container (and execute sql statements successfully) from the web container. Additionally, when I exec to the web container I can execute doctrine:migrations:diff commands successfully.
However, when I try and run code like the following:
$User = $this->getDoctrine()
->getRepository(User::class)
->findAll();
I receive the following error:
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)
Here is the contents of my .env file
# 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 ###
Additionally, if I try and use the DBAL it returns the same error:
/**
* @Route("/test")
*/
public function testAction(Connection $connection){
$users = $connection->fetchAll('SELECT * FROM users');
return $this->render("startup/startup.html/twig", [
"NewUser" => $users
]);
}
Attempts to use the root username/password are also failing.
Ok, last additional data (I promise :)
Executing the following code works fine from within the /public folder of Symfony
<?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);
?>