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/>"; } ?> 
4
  • restart one time server @john Commented Jan 11, 2021 at 14:19
  • @DarshanMalani - Not sure what you mean. I have rebuilt the Docker containers, and still having the same issue. Commented Jan 11, 2021 at 14:23
  • In the same way you need to reconfigure PHPMyAdmin to point to the database and not localhost, you also need to reconfigure the application's $host. Commented Jan 11, 2021 at 14:41
  • @DavidMaze - I am not sure what you mean. Would you be able to post an answer? Commented Jan 11, 2021 at 15:04

1 Answer 1

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.

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

3 Comments

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
This is due to this reference Stack Overflow (part of the reason I suggest mariadb) there is more on that page as well referring to the same issue
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.