Reputation: 6081
I already found some threads to this topic but none of them were able to help me, because I already did the provided solution or the problem was not exactly the same as mine
I have a docker environment with an nginx/php-fpm etc server and am using the official mysql image which I'm linking into the other server. This is my docker-compose
db:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=symfonyrootpass
- MYSQL_DATABASE=symfony
- MYSQL_USER=symfony
- MYSQL_PASSWORD=symfonypass
project:
image: docker.io/project/project-dev
ports:
- "80:80"
environment:
- XDEBUG_HOST=${LOCAL_IP}
- XDEBUG_PORT=9000
- XDEBUG_REMOTE_MODE=req
links:
- db
volumes:
- ../project:/var/www/app
When I start it and connect from my host machine like this: mysql -h127.0.0.1 -usymfony -psymfonypass
and then use use symfony;
it works without problems. I see my database, I can edit, delete, update etc. I can do everything.
However, I can't say the same for docker. I'm using Symfony and when I have the parameters set correctly, I'm always getting the error
An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused
These are my parameters
database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: symfony
database_password: symfonypass
I already tried to change database_host
to localhost
but then I got the error No such file or directory
with the same SQLSTATE. I also tried to set the port to 3306
but this also didn't help.
What do I need to do to access my database from inside docker?
This is the part in the config.yml
where I describe my mysql connection
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
server_version: "%database_version%"
Upvotes: 2
Views: 784
Reputation: 3559
Your database is running in a different container than your project. Docker-compose will give it an IP address and a host name, which is db because this is how you named it. So, database_host should not be 127.0.0.1, or localhost, but db.
Upvotes: 3