下面由laravel教程欄目給大家介紹解決Laravel使用Windows docker運(yùn)行php容器及mysql容器時(shí),連不起數(shù)據(jù)庫(kù)的方法,希望對(duì)需要的朋友有所幫助!
問(wèn)題
Laravel 項(xiàng)目 使用 Windows docker 運(yùn)行php 容器 及 mysql 容器時(shí), 在windows git bash 執(zhí)行 php artisan migrate 發(fā)現(xiàn)找不到數(shù)據(jù)庫(kù)。
報(bào)錯(cuò)信息:
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 下執(zhí)行 php artisan migrate 時(shí)連不到容器里的mysql
解決
重新寫(xiě)一個(gè)shell 命令封裝一下 php artisan 命令, 進(jìn)行容器后執(zhí)行php artisan 如下:
phpartisan 文件內(nèi)容
#!/usr/bin/env sh # 進(jìn)入容器 執(zhí)行相應(yīng)php artisan命令 docker exec -i 容器名稱 sh -c "cd /項(xiàng)目的容器路徑 && php artisan $*"
執(zhí)行 php artisan *
命令 換成 phpartisan *
即可
推薦:最新的五個(gè)Laravel視頻教程