php - SQLSTATE [HY000] [2002]在Docker中运行多个laravel项目时,连接被拒绝

标签 php laravel docker

  • 我有2个Laravel项目,我的结构文件夹是(我使用win10)
  • - docker-compose.yml
    - config_projects
      - app1.cnf
      - app2.cnf
    - laravel
      - app1 (domain app1.local)
      - app2 (domain app2.local)
    
  • docker-compose.yml的内容
  • version: '3.7'
    services:
    
      web:
        image: nginx
        container_name: webserver_laravel
        restart: unless-stopped
        tty: true
        ports:
          - "8080:80"
        volumes:
          - ./:/var/www/laravel
          - ./config_projects/app1.conf:/etc/nginx/conf.d/app1.conf
          - ./config_projects/app2.conf:/etc/nginx/conf.d/app2.conf
        networks:
          - laravel
    
      php:
        image: php:7.2.5
        container_name: php_laravel
        restart: unless-stopped
        tty: true
        volumes:    
          - ./:/var/www/laravel     
        networks:
          - laravel
    
      db:
        image: mysql:5.7
        container_name: mysql_laravel
        restart: unless-stopped
        tty: true
        command: --default-authentication-plugin=mysql_native_password   
        environment:
          MYSQL_ROOT_PASSWORD: root
        ports:
          - "3306:3306"
        volumes:
          - ./database/mysql:/var/lib/mysql
        networks:
          - laravel
    
    networks:
      laravel:
    
  • app1.cnf的内容
  • server {
        listen       80;
        server_name  app1.local;
        root /var/www/laravel/app1/public;
        index index.php index.html;
    
        charset utf8;
        access_log  /var/log/nginx/host.access.log  main;    
    
        location / {
            index  index.php index.html index.htm;
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }    
    
        if (!-e $request_filename) {
            rewrite ^/(.+)$ /index.php?url=$1 last;
            break;
        }
    
        location ~ \.php$ {  
            fastcgi_pass   php:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    
  • app2.cnf的内容与app1.cnf相同,只是将app1替换为app2
  • app1中的.env内容
  • DB_CONNECTION=mysql
    DB_HOST=192.168.56.1 ===> My computer's ip
    DB_PORT=3306
    DB_DATABASE=app1 ===> is app2 in app2's env
    DB_USERNAME=root
    DB_PASSWORD=root
    
  • 运行docker-compose up -d --build
  • 打开app1.local:8080->正常工作
  • 打开app2.local:8080-> SQLSTATE [HY000] [2002]连接被拒绝

  • 是什么问题?非常感谢

    最佳答案

    谢谢你们我修好了它。这是Laravel不是Docker的问题
    在将问题发布到stackoverflow之前,我已经运行了许多下面的命令

    php artisan clear-compiled
    php artisan cache:clear
    php artisan key:generate
    php artisan config:clear
    php artisan view:clear
    php artisan optimize
    

    但这不起作用。现在我重新运行,它可以正常工作。好奇怪

    关于php - SQLSTATE [HY000] [2002]在Docker中运行多个laravel项目时,连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61136794/

    相关文章:

    php - Symfony 查询错误 : Expected Literal, 得到 '"'

    php - Laravel 工厂辅助函数如何在命名空间方面工作?

    laravel - 如何使用添加的列作为查询正确更新数据透视表?

    php - Laravel 中如何将消息传递到另一个页面?

    docker - 将编译后的Go二进制文件放在哪里?

    javascript - 将 js knockout 到 mysql 数据库,使动态值不起作用

    php - 尝试连接到其他文件中的数据库时无法使用常量

    docker - Jenkins docker pipeline 未传递退出代码

    php - jquery 不使用 if 语句

    docker - 为什么我在 Docker 桥接网络中获取网关 IP 作为源地址?