Unable to connect to Docker MySQL database: Warning: mysqli::__construct(): (HY000/2002): Connection refused [duplicate]

Created 2 containers one with Apache/PHP and the other with MySQL by docker-compose. I am new to Docker and PHP and it appears my settings are wrong since I get the following errors:

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /var/www/html/db.php on line 3 Failure! No such file or directory That's for a php file:

<?php

$conn=mysqli_connect('localhost', 'a', 'bbbbbb', 'my_db');

if ($conn == false) {
        echo 'Failure! ';
        echo mysqli_connect_error();
        exit();
}

?>

And the other:

Warning: mysqli::__construct(): (HY000/2002): Connection refused in /var/www/html/index.php on line 22 Connection failed: Connection refused

That's for the file supposed to show all databases:

<?php

getenv('MYSQL_DBHOST') ? $db_host=getenv('MYSQL_DBHOST') : $db_host="localhost";
getenv('MYSQL_DBPORT') ? $db_port=getenv('MYSQL_DBPORT') : $db_port="3306";
getenv('MYSQL_DBUSER') ? $db_user=getenv('MYSQL_DBUSER') : $db_user="a";
getenv('MYSQL_DBPASS') ? $db_pass=getenv('MYSQL_DBPASS') : $db_pass="bbbbbb";
getenv('MYSQL_DBNAME') ? $db_name=getenv('MYSQL_DBNAME') : $db_name="my_db";

if (strlen( $db_name ) === 0)
  $conn = new mysqli("$db_host:$db_port", $db_user, $db_pass);
else 
  $conn = new mysqli("$db_host:$db_port", $db_user, $db_pass, $db_name);

// Check connection
if ($conn->connect_error) 
    die("Connection failed: " . $conn->connect_error);
 
if (!($result=mysqli_query($conn,'SHOW DATABASES')))
    printf("Error: %s\n", mysqli_error($conn));

echo "<h3>Databases</h3>";

while($row = mysqli_fetch_row( $result ))
    echo $row[0]."<br />";

$result -> free_result();
$conn->close();
?>

My docker-compose file:

version: '3'

volumes:
  datafiles:

services:
  mysql:
    image: mysql:latest
    container_name: mysql8-stack
    build:
      context: ./mysql8
      dockerfile: Dockerfile
    environment:
      - MYSQL_USER=a
      - MYSQL_PASSWORD=bbbbbb
      - MYSQL_DATABASE=my_db
      - MYSQL_ROOT_PASSWORD=bbbbbb
      - MYSQL_TCP_PORT=3306
    volumes:
      - datafiles:/var/lib/mysql
    restart: always

  website:
    container_name: php73-stack
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - MYSQL_DBHOST=mysql
      - MYSQL_DBPORT=3306
      - MYSQL_DBUSER=a
      - MYSQL_DBPASS=bbbbbb
      - MYSQL_DBNAME=my_db
    ports:
      - 80:80
      - 8083:443
    depends_on:
      - mysql

I want to create mysql database on docker-compose startup from .sqlgz backup. MySQL Dockerfile:

FROM mysql:latest

ADD *.gz /docker-entrypoint-initdb.d
EXPOSE 3306

Dockerfile for Apache/PHP:

FROM php:7.3-apache

RUN apt-get update && apt-get install -y git
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN a2enmod rewrite

COPY src /var/www/html/
EXPOSE 80/tcp
EXPOSE 443/tcp

EDIT: The index.php (see above) does show what is expected in this configuration (docker-compose):

version: '3'

volumes:
  datafiles:

services:
  mysql:
    image: mysql:8.0.0
    container_name: mysql8
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_TCP_PORT=3306
    volumes:
      - datafiles:/var/lib/mysql
    restart: always

  website:
    container_name: php73
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - MYSQL_DBHOST=mysql
      - MYSQL_DBPORT=3306
      - MYSQL_DBUSER=root
      - MYSQL_DBPASS=secret
      #- MYSQL_DBNAME=
    ports:
      - 8080:80
      - 8083:443
    depends_on:
      - mysql
<?php

getenv('MYSQL_DBHOST') ? $db_host=getenv('MYSQL_DBHOST') : $db_host="localhost";
getenv('MYSQL_DBPORT') ? $db_port=getenv('MYSQL_DBPORT') : $db_port="3306";
getenv('MYSQL_DBUSER') ? $db_user=getenv('MYSQL_DBUSER') : $db_user="root";
getenv('MYSQL_DBPASS') ? $db_pass=getenv('MYSQL_DBPASS') : $db_pass="";
getenv('MYSQL_DBNAME') ? $db_name=getenv('MYSQL_DBNAME') : $db_name="";

if (strlen( $db_name ) === 0)
  $conn = new mysqli("$db_host:$db_port", $db_user, $db_pass);
else 
  $conn = new mysqli("$db_host:$db_port", $db_user, $db_pass, $db_name);

// Check connection
if ($conn->connect_error) 
    die("Connection failed: " . $conn->connect_error);
 
if (!($result=mysqli_query($conn,'SHOW DATABASES')))
    printf("Error: %s\n", mysqli_error($conn));

echo "<h3>Databases</h3>";

while($row = mysqli_fetch_row( $result ))
    echo $row[0]."<br />";

$result -> free_result();
$conn->close();
?>

Any help is appreciated.



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation