Reputation: 389
I have a problem with docker and symfony to access mysql.
To try to query mysql by connecting to the php container:
docker exec -it symfony_php bash
Then I execute the following command:
bin/console doctrine:database:create
This is where I get the following error message:
An exception occurred in driver: SQLSTATE [HY000] [2002] Connection refused
Here is the code of my "docker-compose.yml":
version: '3'
services:
apache:
container_name: symfony_apache
build: .docker/apache
ports:
- "80:80"
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/www/symfony
depends_on:
- php
mysql:
container_name: symfony_mysql
image: mysql
ports:
- "3306:3306"
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
container_name: symfony_php
build: .docker/php
volumes:
- .:/home/www/symfony
depends_on:
- mysql
phpmyadmin:
container_name: symfony_phpmyadmin
image: phpmyadmin/phpmyadmin
ports:
- "8080:80"
environment:
PMA_HOST: mysql
PMA_PORT: 3306
links:
- mysql
Here is the code of my .env:
DATABASE_URL=mysql://root:[email protected]:3306/s4
Upvotes: 14
Views: 53924
Reputation: 579
My problem is the Docker network didn't work.
I've recreated Docker network and all containers and it helped me.
Example that removes containers and the network and recreates them.
docker-compose down
docker-compose up -d
Upvotes: 0
Reputation: 195
use this command to get the ip adress
docker inspect mysql | grep IPAddress
then for example:
DATABASE_URL=mysql://root:@the ip adress:3306/dbname?serverVersion=5.7
DATABASE_URL=mysql://root:@172.17.0.3:3306/test?serverVersion=5.7
or instead of the the ip adress make the name of the container
Upvotes: 5
Reputation: 302
Did you try to use the internal identifier as mysql host?
DATABASE_URL=mysql://root:root@symfony_mysql:3306/s4
DATABASE_URL=mysql://root:root@mysql:3306/s4
I don't think 127.0.0.1 works from within the container.
You also could try to use your 'external' IP or URL, as you have exposed port 3306 already.
Upvotes: 14
Reputation: 13667
You’re trying to connect to root:root
, but your user is sf4
. Try:
DATABASE_URL=mysql://sf4:[email protected]:3306/s4
Also try connecting from the command line, to make sure that it is actually working.
Edit: Just realised that Connection refused
is normally when it’s not running, not a user/password error. Check that MySQL is running.
Upvotes: 0