简体   繁体   中英

PHP Connection refused

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/>";
}
?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM