skip to Main Content

They are newbie to docker.
Get phpmyadmin, php, home assistant working. But I can’t configure the docker well to be able to connect from php to a database. Could you help me see what the problem is.

I tried everything. I read many posts with the same error but could not get it to work.

Thank you very much

This is my modified docker-compose

version: '3.4'
services:
  web:
    build:
      context: ./php
      dockerfile: Dockerfile
    container_name: php73
    depends_on:
      - db
    volumes:
      - ./php:/var/www/html/
    environment:
      MYSQL_HOST: mysql8
      MYSQL_USER: pf
      MYSQL_PASSWWORD: 123456
      MYSQL_DB: ha
    ports:
      - 3001:80
  db:
    container_name: mysql8
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_USER: pf
      MYSQL_PASSWWORD: 123456
      MYSQL_DATABASE: ha
    volumes: 
      - /var/lib/mysql
    ports:
      - 6033:3306

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    depends_on: 
      - db
    restart: always
    ports:
      - 8080:80
    environment:
      PMA_ARBITRARY: 1
      PMA_HOST: db
      PMA_PORT: 3306
      MYSQL_USER: pf
      MYSQL_PASSWWORD: 123456
      MYSQL_ROOT_PASSWORD: 123456
    volumes:
      - /sessions

The new php example

<?php
  $host = 'db';
  $user = 'pf';
  $password = '123456';
  $db = 'ha';

  $conn = new mysqli($host,$user,$password,$db,3306);
  if($conn->connect_error) {
    echo 'connection failed' . $conn->connect_error; 
  } 
  echo 'Sucessfully connected msql';
?>

The Dockerfile


FROM php:7.3.3-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install mysqli
EXPOSE 80

My docker-compose

version: '3.3'
services:
  web:
    build:
      context: ./php
      dockerfile: Dockerfile
    container_name: php73
    depends_on:
      - db
    volumes:
      - ./php:/var/www/html/
    ports:
      - 80:80
  db:
    container_name: mysql8
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mediciones
      MYSQL_USER: root
      MYSQL_PASSWWORD: root
    ports:
      - 6033:3306

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    depends_on: 
      - db
    restart: always
    ports:
      - 8080:80
    environment:
      PMA_ARBITRARY: 1
      PMA_HOST: db
      MYSQL_ROOT_PASSWORT: root

  homeassistant:
    container_name: homeassistant
    restart: unless-stopped
    image: homeassistant/home-assistant
    devices:
      - /dev/ttyUSB0:/dev/ttyUSB0
      - /dev/ttyUSB1:/dev/ttyUSB1
      - /dev/ttyACM0:/dev/ttyACM0
    volumes:
      - ${USERDIR}/docker/homeassistant:/config
      - /etc/localtime:/etc/localtime:ro
      - ${USERDIR}/docker/shared:/shared
      - /dev/serial/by-id/:/dev/serial/by-id/
    network_mode: host
    privileged: true
   

My php example

<?php
  $host = '127.0.0.1';
  $user = 'root';
  $password = 'root';
  $db = 'ha';

  $conn = new mysqli($host,$user,$password,$db);
  if($conn->connect_error) {
    echo 'connection failed' . $conn->connect_error; 
  } 
  echo 'Sucessfully connected msql';
?>

2

Answers


  1. You are exposing mysql on host port: 6033

    Did you try:

    $conn = new mysqli($host,$user,$password,$db,6033);
    
    Login or Signup to reply.
  2. Assuming its your example code running in the php container:

    In which case use db as the hostname and mediciones as the database name in the connection.

    For the db container:

    • MYSQL_USER set to non-root as this will cause potential errors. This user is already given access on the MYSQL_DATABASE.
    • use a persistent volume for /var/lib/mysql
    • (optionally), if your home assistant or php doesn’t create the tables, you can use [mysql "Initializing a fresh instance"]https://hub.docker.com/_/mysql) to initialize some tables.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search