警告:mysqli :: __ construct():(HY000 / 2002):连接被拒绝

时间:2020-08-21 23:32:56

标签: php mysql docker docker-compose

他们是Docker的新手。 获取phpmyadmin,php,家庭助理的工作。但是我无法很好地配置docker使其能够从php连接到数据库。你能帮我看看是什么问题。

我尝试了一切。我读了很多帖子,但都出现了相同的错误,但无法正常工作。

非常感谢您

这是我修改的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

新的php示例

<?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';
?>

Dockerfile


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

我的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
   

我的php示例

<?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 个答案:

答案 0 :(得分:0)

您正在mysql主机端口上暴露6033

您尝试过吗:

$conn = new mysqli($host,$user,$password,$db,6033);

答案 1 :(得分:0)

假设您的示例代码在php容器中运行:

在这种情况下,请在连接中使用db作为主机名,并使用mediciones作为数据库名。

对于db容器:

  • MYSQL_USER设置为非root用户,因为这将导致潜在的错误。该用户已经在MYSQL_DATABASE上具有访问权限。
  • 为/ var / lib / mysql使用持久卷
  • (可选),如果您的家庭助理或php未创建表,则可以使用[mysql“初始化新实例”] https://hub.docker.com/_/mysql)初始化一些表。