ogbofjnr
ogbofjnr

Reputation: 1998

Can't connect to database in docker-compose

This is my docker-compose.yaml

version: "3.1"
services:
  mysql:
    image: mysql:8.0.13
    container_name: project-mysql
    volumes:
      - ./docker/data/mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: db
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: secret
    ports:
      - "3306:3306"

  webserver:
    image: nginx:alpine
    container_name: project-webserver
    working_dir: /var/www
    volumes:
      - .:/var/www
      - ./docker/config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "8080:80"

  php-fpm:
    build: ./docker/config/php
    container_name: project-php-fpm
    working_dir: /var/www
    volumes:
      - .:/var/www
    environment:
      XDEBUG_CONFIG: remote_host=172.17.0.1
      PHP_IDE_CONFIG: serverName=docker

The problem is that php container can't connect to mysql. Although from host I can connect with mycli -h 127.0.0.1 -P 3306 -u root. But when I exec in php container and try it, get 'Connection refused'

Upvotes: 1

Views: 6013

Answers (4)

Ayush Choudhary
Ayush Choudhary

Reputation: 341

It may be possible due to cached volume.

Try deleting your volume:

$ docker volume prune

WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
07c7bdf3e34ab76d921894c2b834f073721fccfbbcba792aa7648e3a7a664c2e
my-named-vol

Total reclaimed space: 36 B

And then try any one of these:

docker-compose up --build

OR

docker-compose up

Remember: Before deleting your unused volumes run the following command if your docker-compose up is still running:

docker-compose down

Upvotes: 0

Ntwobike
Ntwobike

Reputation: 2741

Usually conenction refused you get when request is coming from the different ip than it's bind-address. Try below should work.

  • Make sure mysql configuration my.cnf or whatever default configs doesn't block connect from outside(check bind-address in the mysql configuration if it 127.0.0.1 the its only allow to connect form locally, i would for now make it 0.0.0.0 or commented that line if exists)
    • mysqld --verbose --help => you will see all options
    • mysqld --verbose --help | grep bind-address => check the bind-address
  • Make sure the user i tried to login has enough privileges to connect(SELECT user,host FROM mysql.user;) check your user can connect from docker network => 172.* or anywhere=> %

Upvotes: 0

leopal
leopal

Reputation: 4959

Your mysql service binds mysql server's port 3306 to 3306 of the host machine. Thus is it normal that you can connect from host.

From inside php container, localhost is referring to that particular container as @David Maze said in the comments.

Since you are using docker-compose containers are in the same network, so you should use the service name in order to connect to the mysql server.

Try this from php container:

mycli -h mysql -P 3306 -u root

Upvotes: 2

rajesh-nitc
rajesh-nitc

Reputation: 5539

You are able to connect with user root. But in your docker-compose file, you are connecting through user dbuser. Let me know if that's not the issue.

Upvotes: 1

Related Questions