Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...