douqian1517 2018-05-09 13:11
浏览 1005
已采纳

使用Docker运行Laravel artisan命令时连接被拒绝

I'm running Laravel 5.4 in Docker. This is my docker-compose.yml file:

version: '2'

services:
  app:
    container_name: laravel_app
    image: webdevops/php-apache-dev:ubuntu-16.04
    links:
      - mysql
    depends_on:
      - mysql
    ports:
      - 8888:80
    volumes:
      - .:/app
    environment:
      docker: 'true'
      WEB_DOCUMENT_ROOT: '/app/public'
      WEB_NO_CACHE_PATTERN: '\.(.*)$$'
      working_dir: '/app'
  mysql:
    image: mariadb:latest
    ports:
      - 8889:80
    environment:
      MYSQL_ROOT_PASSWORD: 'dev'
      MYSQL_DATABASE: 'dev'
      MYSQL_USER: 'dev'
      MYSQL_PASSWORD: 'dev'

This is the relevant part of my .env file:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=8889
DB_DATABASE=dev
DB_USERNAME=dev
DB_PASSWORD=dev

I am able to see the Laravel welcome page - that side of things works. But when I run php artisan migrate I get this error:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = dev and table_name = migrations)

I have tried fiddling with the host and port parameters in the .env file.

  • 写回答

2条回答 默认 最新

  • dongzan1970 2018-05-09 13:29
    关注

    First edit your docker-compose.yml.

    mysql:
        image: mariadb:latest
        ports:
          - 8889:3306
    

    After that set the correct DB port in .env.

    Your DB port is wrong. You are trying to connect the exposed port inside the docker. In this case you should use DB_PORT=3306 in your .env.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化