0

I've seen several other posts with this issue. One in particular is this:

Docker MYSQL '[2002] Connection refused'

I tried to add PMA_HOST: mysql as instructed in the previous question.

Here is my docker-compose.yml file looks like:

version: "3.7"
services:
  www:
    build: .
    ports:
      - "8001:80"
    volumes:
      - ./www:/var/www/html/
    links:
      - db
    networks:
      - default
  db:
    image: mysql:8.0
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: myDb
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test
    volumes:
      - ./dump:/docker-entrypoint-initdb.d
    networks:
      - default
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - db:db
    ports:
      - 8000:80
    environment:
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test
volumes:
  persistent:

My dockerfile:

FROM php:7.0.30-apache
RUN docker-php-ext-install pdo pdo_mysql

Using the above, when trying to connect to the database, I get the error:

Conneciton failed: SQLSTATE[HY000] [2002] Connection refused

As stated, from the previously asked question, I updated the yml file to include PMA_HOST: mysql under the environment section of the phpmyadmin service. Problem is, when I do that, I can no longer log into the phpmyadmin.

Does anyone see what I am doing wrong and now I can fix it?

*** EDIT ***

Here is my database connection file:

<?php
$host = '127.0.0.1';
$dbname = 'myDb';
define ('DB_USER', 'user');
define ('DB_PASSWORD', 'test');

try 
{
  $dbc = new PDO("mysql:dbname=$dbname;host=$host", DB_USER, DB_PASSWORD);
  $dbc->SetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
  echo "Connection failed: " . $e->getMessage() . "<br/>";
}
?>
John Beasley
  • 2,577
  • 9
  • 43
  • 89

1 Answers1

2

Currently, you are trying to use 127.0.0.1 rather than actually setting the host as the service of the docker container you are running for db, to fix this you should change the files to something like so:

docker-compose.yml

version: "3.7"
services:
  www:
    build: .
    ports:
      - "8001:80"
    volumes:
      - www:/var/www/html/
  db:
    image: mysql:8.0 # also recommend using mariadb over the MySQL image.
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: myDb
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test
    volumes:
      - mysql-data:/docker-entrypoint-initdb.d
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8000:80
    environment:
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test
volumes:
  mysql-data:
  phpmyadmin:

database connection file:

<?php
$host = 'db';
$dbname = 'myDb';
define ('DB_USER', 'user');
define ('DB_PASSWORD', 'test');

try 
{
  $dbc = new PDO("mysql:dbname=$dbname;host=$host", DB_USER, DB_PASSWORD);
  $dbc->SetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
  echo "Connection failed: " . $e->getMessage() . "<br/>";
}
?>

this will allow you to use the service to autofill the host, it also then defined the volumes. but this should work and fix the connection refusal as it will find the host address and values now.

MysticSeagull
  • 206
  • 2
  • 10
  • Upon making the updates, now I'm getting the following error: Connection failed: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client – John Beasley Jan 11 '21 at 15:01
  • This is due to this reference [Stack Overflow](https://stackoverflow.com/a/53881212/14963397) (part of the reason I suggest mariadb) there is more on that page as well referring to the same issue – MysticSeagull Jan 11 '21 at 15:03
  • That seems to have worked. I am no longer getting any of the previously stated error messages. I'm running a few more tests, but all seems good. Thank you. – John Beasley Jan 11 '21 at 15:20