3

I ran into this Problem with the PHP Data Object.

I cannot connect to my Database. First here is my PHP Script:

<?php

$serverName = "127.0.0.1";
$port = "3306";
$dbName = "callitTime";
$userName = "root";
$password = "superstrongPassword";

try {
    $db = new PDO("mysql:host=$serverName;dbname=$dbName;port=$port;",
    $userName, $password);

} catch (PDOException $pdoE) {
    echo 'An Error occurred: ' . $pdoE->getMessage();
}
?>

I am using:

  • PHP 7.1.16

  • Nginx 1.13.12-1

  • MySQL 8.0.11-1debian9

All as Docker Containers.

A phpinfo() tells me that PDO Drivers are loaded as follows:

PDO Drivers:

  • sqlite

  • mysql

Driver Versions:

  • mysqlnd 5.0.12-dev

  • SQLite Library 3.15.1

I get the Error:

An Error occurred: SQLSTATE[HY000] [2002] Connection refused

I can connect to the same Database via Phpstorm with the same Password and Username.

8
  • db-name should be dbname? Commented May 11, 2018 at 19:27
  • 1
    Did you create a user bridge network for your set of containers? Commented May 11, 2018 at 19:32
  • Yes the Containers are User Bridged! @user188737 Commented May 11, 2018 at 19:38
  • 1
    Then I don't understand why you use host=127.0.0.1. Surely the mysql container has a different IP address? Commented May 11, 2018 at 19:41
  • Never mind I remembered that wrong. @user188737 Commented May 11, 2018 at 19:49

2 Answers 2

3

It seems that this is not a problem related to the PHP Configuration.

The error Connection refused shows, from my point of view, that you can't establish a connection from the php container to the mysql container.

You need to expose the 3306 Port to the Webserver Container so that the PHP Container can establish a connection to the database. If you already bridged the containers you need to use the containers IP address and not your loopback 127.0.0.1.

Please see this answer for more information how to connect both containers and how to make a connection from a php to a mysql container:
https://stackoverflow.com/a/43128428/1118905

To narrow down the problem, you can try to establish a connection from within the PHP Container via Netcat to your given DB Host.

For example you can try to establish a connection with the following commands:

  1. Get into the container from which you want to test the connection.
    docker exec -it <name_of_container> bash
  2. Test to open up a connection via netcat (If not all ready available install it via f.e. apt)
    nc -vz 127.0.0.1:3306
Sign up to request clarification or add additional context in comments.

4 Comments

I´ll give that a try one moment!
now I´m getting confused I can't even exec into my container ... 2 seconds blank line return to host shell
You could try with docker attach to get stdin and stdout attached to the running container: docs.docker.com/engine/reference/commandline/attach
Depending on the answers in your comments on the questions I'm certainly sure, that with the answer in the linked SO Question you can solve your problem.
0

If you are inside container, 127.0.0.1 is not known as a service that hosts the database. You can use the mysql container name instead. For example $serverName = "mysql"; depending on the name of your container.

sample service definition

services:
    mysql:
        image: 'mysql:5.5'
        container_name: mysql
        ports:
            - '33066:3306'
   ...

Also note that the port to be used is the internal one.

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.