运行docker-compose exec app php artisan migration时连接被拒绝

时间:2019-01-13 13:12:27

标签: laravel docker

在终端上运行docker-compose exec应用php artisan迁移时,我得到PDOException::(“ SQLSTATE [HY000] [2002]连接被拒绝”)

这是我在.env文件上的数据库配置

DB_CONNECTION=mysql
DB_HOST=172.20.0.4
DB_PORT=3306
DB_DATABASE=queue_db
DB_USERNAME=root
DB_PASSWORD=root

我还检查了容器ip

docker inspect -f'{{range .NetworkSettings.Networks}} {{。IPAddress}} {{end}}'worker-db

输出:172.20.0.4

这是我的docker-compose.yml

version: '3'
services:

#PHP Service
app:
    build:
    context: .
    dockerfile: ./docker/Dockerfile
    image: digitalocean.com/php
    container_name: worker-app
    restart: unless-stopped
    tty: true
    environment:
    SERVICE_NAME: app
    SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
    - ./:/var/www
    - ./docker/config/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
    - app-network

#Nginx Service
webserver:
    image: nginx:alpine
    container_name: worker-webserver
    restart: unless-stopped
    tty: true
    ports:
    - "8090:80"
    - "443:443"
    volumes:
    - ./:/var/www
    - ./docker/config/nginx/conf.d/:/etc/nginx/conf.d/
    networks:
    - app-network

#MySQL Service
db:
    image: mysql
    container_name: worker-db
    # restart: unless-stopped
    tty: true
    ports:
    - "3306:3306"
    environment:
    MYSQL_DATABASE: queue_db
    MYSQL_ROOT_PASSWORD: root
    SERVICE_TAGS: dev
    SERVICE_NAME: mysql
    networks:
    - app-network

#Docker Networks
networks:
app-network:
    driver: bridge

#Volumes
volumes:
dbdata:
    driver: local

在堆栈跟踪上有一个奇怪的错误,其中ip不同,我不确定为什么要获得不同的ip,但是在我的.env文件中,我使用的容器ip是“ 172.20.0.4”

Exception trace:
1   PDOException::("SQLSTATE[HY000] [2002] Connection refused")
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

2   PDO::__construct("mysql:host=172.20.0.3;port=3306;dbname=queue_db", "root", "root", [])
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

谢谢。

1 个答案:

答案 0 :(得分:0)

好吧,在我尝试运行php artisan config之后:清除它现在可以正常工作。