Reputation: 146
I'm having the following error when trying to connect to DB using Symfony 5 and Docker.
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
Maybe you could know what's wrong on my config files. If i missed any config file that could be related to this problem, please tell me.
docker-compose.yaml:
mysql:
image: mysql:8
ports:
- "3306:3306"
volumes:
- ./docker/database:/docker-entrypoint-initdb.d
environment:
- MYSQL_DATABASE=${DATABASE_NAME}
- MYSQL_ROOT_PASSWORD=${DATABASE_ROOT_PASSWORD}
command: ["--default-authentication-plugin=mysql_native_password"]
doctrine.yaml:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
.env:
DATABASE_URL=mysql://root:[email protected]:3306/database_name?serverVersion=8.0
DATABASE_ROOT_PASSWORD=root
DATABASE_NAME=database_name
service definition:
some.repository:
class: Some\Class
arguments:
- '@database_connection'
Upvotes: 1
Views: 7314
Reputation: 974
The address in the DATABASE_URL 127.0.01 refers to the local docker container. The issue is that the MySQL is another container.
To resolve the issue: Replace 127.0.0.1 with the database container in your case “mysql”
Upvotes: 8