解决Laravel使用Windows docker运行php容器及mysql容器时,连不起数据库问题

下面由laravel教程栏目给大家介绍解决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的dd函数不生效怎么办
看不懂laravel文档咋办
安装laravel框架出现command怎么办
Laravel开发API怎么使用事务
laravel怎么构建复杂查询条件
laravel如何实现防止被下载