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

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

下面由laravel教程欄目給大家介紹解決Laravel使用Windows docker運行php容器及mysql容器時,連不起數據庫的方法,希望對需要的朋友有所幫助!

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

問題

Laravel 項目 使用 Windows docker 運行php 容器 及 mysql 容器時, 在windows ?git ?bash 執行 php artisan migrate 發現找不到數據庫。

報錯信息:

   IlluminateDatabaseQueryException  : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 未知  (SQL: select * from ***)    at E:projectvendorlaravelframeworksrcIlluminateDatabaseConnection.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   DoctrineDBALDriverPDOException::("SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 知 ")       E:projectvendordoctrinedballibDoctrineDBALDriverPDOConnection.php:31    2   PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: 知 ")       E:projectvendordoctrinedballibDoctrineDBALDriverPDOConnection.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 *即可

立即學習PHP免費學習筆記(深入)”;

推薦:laravel

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享