首頁 > php框架 > Laravel > 主體

解決Laravel使用Windows docker運行php容器及mysql容器時,連不起資料庫問題

藏色散人
發布: 2021-03-22 08:58:07
轉載
3006 人瀏覽過

下面由laravel教學欄位介紹解決Laravel使用Windows docker運行php容器及mysql容器時,連不起資料庫的方法,希望對需要的朋友有幫助!

解決Laravel使用Windows docker運行php容器及mysql容器時,連不起資料庫問題

問題

Laravel 專案使用Windows docker 執行php 容器及mysql 容器時, 在windows  git  bash 執行php artisan migrate 發現找不到資料庫。

錯誤訊息:

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 未知  (SQL: select * from ***)

  at E:\project\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e    671|             );
    672|         }
    673|

  Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 知 ")
      E:\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:31

  2   PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: 知 ")
      E:\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:27

  Please use the argument -v to see more details.
登入後複製

原因

windows 下執行php artisan migrate 時連不到容器裡的mysql

解決

重新寫一個shell 指令封裝一下php artisan 指令,  進行容器後執行php artisan 如下:

phpartisan 檔案內容

#!/usr/bin/env sh

# 进入容器 执行相应php artisan命令

docker exec -i 容器名称 sh -c "cd /项目的容器路径 && php artisan $*"
登入後複製

執行php artisan * 指令換成phpartisan *即可

##推薦:

最新的五個Laravel影片教學

#

以上是解決Laravel使用Windows docker運行php容器及mysql容器時,連不起資料庫問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:learnku.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!