2

Trying to connect to mysql 8 (mysql:latest) from a php:latest image (without apache or nginx) and I'm getting this annoying error:

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Connection refused in /src/conn.php:5
Stack trace:
#0 /src/conn.php(5): PDO->__construct('mysql:host=data...', 'root', '12345678')
#1 {main}
  thrown in /src/conn.php on line 5

I've looked for +20 solutions here on stackoverflow but none of them was able fix my problem. I can connect without a problem using mysql workbench or DataGrid using the following credentials:

server: localhost user: root pass: 12345678

But any configuration I use on the php image I can't make them connect with each other.

My docker-compose file:

version: '3'
services:
  composer:
    container_name: composer
    networks:
      - backend
    image: composer
    volumes:
      - .:/app
    command: install
  database:
    container_name: mysql
    networks:
      - backend
    image: mysql:latest
    volumes:
      - ./database/mysqldata:/var/lib/mysql
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: 12345678
      MYSQL_DATABASE: testdb
    ports:
      - "3306:3306"
  php:
    build:
      context: .
    container_name: php
    networks:
      - backend
    image: php:apache
    links:
      - database
    env_file:
      - .env
    volumes:
      - .:/
    command:
      php /src/conn.php
    depends_on:
      - composer
      - database
networks:
  backend:

My conn.php:

<?php
    $db = new PDO('mysql:host=database;dbname=testdb', 'root', '12345678');

What I did until now to try to fix the problem:

  1. Change conn.php host to 127.0.0.1
  2. Change conn.php host to localhost
  3. Tried to use mysqli (got also connection refused)
  4. Tried another php images (with apache, nginx etc)
  5. Tried to use mysql as a host

PS: I'm using docker for mac

After 5 hours of research I really don't know what else to do and as I'm not a docker expert, would appreciate any help to point me the direction of the fix.

Thank you!

2
  • Can you try 127.0.0.1:3306 or localhost:3306 ? Commented Jan 27, 2019 at 21:42
  • @OmerTekbiyik got a different error: Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Cannot assign requested address in /src/conn.php:5 Commented Jan 27, 2019 at 21:44

2 Answers 2

6

In your case, you can't connect to mysql via localhost or 127.0.0.1. If you connect like this. It call to localhost inside php container. But there is no mysql install in your php container.

You must connetc via container name. Or docker inspect you mysql container and get the container IP. For example with your docker-compose

database:
    container_name: mysql

Now your mysql database host is mysql - container name instead localhost

Sign up to request clarification or add additional context in comments.

2 Comments

I did try this I got the same error. Look at my conn.php I'm already trying to use the service name or container name
Also I tried with the ip from docker inspect (172.21.0.2) and got the exact same error
0

Was able to find the solution. Turns out I need to wait for mysql service to be available before I start the php script. So I used the wait-for-it solution and did this on docker-compose:

 command: >
      bash -c "chmod +x /docker/./wait-for-it.sh
      && /docker/./wait-for-it.sh database:3306 -- echo 'database is up'
      && php phpscript.php"

Thank you for the help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.