docker mysql问题sqlstate[hy000][2002]php\u network\u getaddresses:getaddrinfo失败

h4cxqtbf  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(233)

我想用php7.2.10-fpm、nginx和mysql 5.7建立一个小型docker环境,以便在其中运行laravel5.7。不幸的是,当我尝试从我的应用程序服务(尝试运行)连接到mysql时,出现了以下错误 docker-composer exec app php artisan migrate ):

In Connector.php
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known

想跑吗 docker-compose exec app mysql -u root -p 返回:error 2002(hy000):无法通过socket'/var/run/mysqld/mysqld.sock'连接到本地mysql服务器(2“没有这样的文件或目录”)
开始构建我的容器 docker-compose up --build 不会抛出错误。它说mysql正在运行并等待连接。我还通过删除并重新创建了所有图像 docker system prune --force --volumes .
也许我在docker-compose.yml中遗漏了什么?

version: '3.3'
services:
  app:
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ./../backend:/var/www
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    depends_on:
      - mysql
    volumes:
      - ./../backend:/var/www
    ports:
        - 8080:80
  mysql:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      - "MYSQL_DATABASE=shop"
      - "MYSQL_USER=shop_u"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"
    ports:
      - 33061:3306
    command: --disable-partition-engine-check
volumes:
  db_data:

app.dockerfile文件:

FROM php:7.2.10-fpm

RUN apt-get update
RUN apt-get install -y libmcrypt-dev
RUN apt-get install -y mysql-client
RUN apt-get install -y libmagickwand-dev --no-install-recommends
RUN pecl install imagick
RUN docker-php-ext-enable imagick
RUN docker-php-ext-install pdo_mysql
RUN pecl install mcrypt-1.0.1
RUN docker-php-ext-enable mcrypt
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php -r "if (hash_file('SHA384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN mv composer.phar /usr/local/bin/composer
RUN apt-get install -y git
RUN apt-get update && apt-get install -y zlib1g-dev
RUN docker-php-ext-install zip

最后是我的.env文件:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=shop
DB_USERNAME=shop_u
DB_PASSWORD=secret

更新 docker-compose images 列出以下容器:

+-----------------+-------------------+----------------+
|    Container    |    Repository     |    ImageID     |
+-----------------+-------------------+----------------+
| docker_app_1    |  docker_app       |   a94dba376130 |
| docker_mysql_1  |  docker_database  | 563a026a1511   |
| docker_web_1    |  docker_web       |   d9024ab40b73 |
+-----------------+-------------------+----------------+

跑步 ping docker_mysql_1docker exec -it docker_app_1 bash 结果 bash: ping: command not found .
跑步 ping docker_mysql_1docker exec -it docker_web_1 bash 很好用。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题