首页 > 解决方案 > Docker容器无法连接到mysql

问题描述

您好,我有一个正在运行的 docker 容器,其中包含我的 laravel 应用程序,我正在使用本地机器上的数据库。我的数据库没有额外的容器。但不幸的是,我无法将我的 docker 容器与我的 mysql DB 连接起来。
这是码头文件

FROM ubuntu:bionic

RUN apt-get update && apt-get -y upgrade && apt-get install -y software-properties-common tzdata
RUN echo "Asia/Dhaka" > /etc/timezone && rm -f /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
RUN add-apt-repository -y ppa:ondrej/php
RUN apt-get update
RUN apt install -y wget php8.0 php8.0-fpm php8.0-common php8.0-mysql php8.0-xml php8.0-dev php8.0-xmlrpc php8.0-curl php8.0-mbstring php8.0-zip
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php
RUN mv composer.phar /usr/local/bin/composer
RUN apt-get install -y php8.0-mongodb unzip
RUN apt-get install -y mysql-client
WORKDIR /app
COPY . .

EXPOSE 3306

这是我的 docker-compose.yml文件

version: "3"

services:
  client:
    build: ./
    command: tail -f /dev/null
    ports:
      - 5500:8000
    volumes:
      - ./:/app
    entrypoint: ./init.sh


当我尝试从我的容器中访问我的数据库时,它说

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

当我尝试通过邮递员运行我的 **api 时,它给了我这个
{
    "status": "failure",
    "data": [],
    "error_code": 400,
    "error_message": "Something went Wrong",
    "errors": "SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `users` where `email` = example@gmail.com limit 1)"
}

可能是什么问题?我的 laravel 应用程序应该可以从容器访问,因为我已经在容器上暴露了端口 3306。我已经配置了 .env 文件,但我仍然无法从我的应用程序访问数据库。我通过 docker exec 进入容器并尝试连接数据库但无法连接。那么谁能告诉我我错过了什么?
提前致谢

标签: phpmysqldocker

解决方案


推荐阅读