尝试从应用程序连接到数据库时出现 MariaDB Docker 错误 2002

在 cli 中尝试从 php 应用程序连接数据库时,我的应用程序出现问题,我收到此错误:

ERROR 2002 (HY000): 无法通过 socket '/var/run/mysqld/mysqld.sock' (111) 连接到本地 MySQL 服务器

当我运行网站时,我得到:

Illuminate\Database\QueryException SQLSTATE[HY000] [2002] 连接被拒绝(SQL:select * from sessions where id = TKks6w5zbHk8bZsm6BEgFyKLJGXj05eyYHQfJsaB limit 1)

我用 docker-compose.yml 和 .Dockerfile 设置了我的 docker 镜像,还有带有 app.conf 的 nginx 文件夹和带有 my.cnf 的 mysql 文件夹,我会在这里弹出这些

version: '3.4'

services:
  mariadb: 
    image: mariadb/server:latest
    container_name: db
    restart: unless-stopped
    tty: true
    ports: 
      - "3306:3306"
    environment:
      MYSQL_DATABASE: piapp
      MYSQL_ROOT_PASSWORD: rootpass
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes: 
      - dbdata:/var/lib/mysql/
      - .\mysql/my.cnf:/etc/mysql/my.cnf
    networks: 
      - Pi-Wallet-Network

  phpmyadmin: 
    image: phpmyadmin/phpmyadmin
    ports:
      - "8080:80"
    restart: unless-stopped
    environment:
      - PMA_HOST=mariadb
    links:
      - mariadb
    networks: 
      - Pi-Wallet-Network

  app:
    image: php:7.4-fpm
    build:
      context: .
      dockerfile: ./Dockerfile
    environment:
      NODE_ENV: production
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - .\:/var/www
      - .\php/local.ini:/usr/local/etc/php/conf.d/local.ini
    links:
      - mariadb
    networks:
      - Pi-Wallet-Network

  webserver:
    image: nginx:latest
    container_name: webserver 
    tty: true
    ports: 
      - "80:80"
      - "443:443"
    volumes:
      - .\:/var/www
      - .\nginx/conf.d/:/etc/nginx/conf.d/
    links:
      - mariadb
    networks:
      - Pi-Wallet-Network

networks:
  Pi-Wallet-Network:
    driver: bridge

volumes:
  dbdata:
   driver: local

我的 .Dockerfile:

FROM php:7.4-fpm
# FROM mariadb/server:latest

# Copy composer.lock and composer.json into the working directory
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www/

# Install dependencies for the operating system software
RUN printf '#!/bin/sh\nexit 0' > /usr/sbin/policy-rc.d
RUN apt-get update && apt-get upgrade -y 
RUN apt-get install -y mariadb-server 
RUN apt-get install -y build-essential 
RUN apt-get install -y libpng-dev
RUN apt-get install -y libjpeg-dev
RUN apt-get install -y libjpeg62-turbo-dev
RUN apt-get install -y libfreetype6-dev 
RUN apt-get install -y locales 
RUN apt-get install -y zip 
RUN apt-get install -y jpegoptim optipng pngquant gifsicle 
RUN apt-get install -y vim 
RUN apt-get install -y libzip-dev 
RUN apt-get install -y unzip 
RUN apt-get install -y git 
RUN apt-get install -y libonig-dev 
RUN apt-get install -y curl

# Install extensions for php
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd

# Install composer (php package manager)
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin 
filename=composer

# Copy existing application directory contents to the working directory
COPY . /var/www/

# Assign permissions of the working directory to the www-data user
# RUN chown -R www-data:www-data /var/www/*
RUN chown -R www-data:www-data /var/www//storage

# Expose port 9000 and start php-fpm server (for FastCGI Process Manager)
EXPOSE 9000
CMD ["php-fpm"]
yanglibaoqi 回答:尝试从应用程序连接到数据库时出现 MariaDB Docker 错误 2002

我通过将 DB_HOST=127.0.0.1 更改为 DB_HOST=host.docker.internal 来解决此问题

作为这个Quesion

本文链接:https://www.f2er.com/2352.html

大家都在问