Laravel在Windows Docker运行php和mysql容器时,如何解决连不上数据库的问题?

2026-04-18 10:212阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计471个文字,预计阅读时间需要2分钟。

Laravel在Windows Docker运行php和mysql容器时,如何解决连不上数据库的问题?

由Laravel教程栏目为新手介绍,解决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

Laravel在Windows Docker运行php和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容器时,连不起数据库问题的详细内容,更多请关注自由互联其它相关文章!

本文共计471个文字,预计阅读时间需要2分钟。

Laravel在Windows Docker运行php和mysql容器时,如何解决连不上数据库的问题?

由Laravel教程栏目为新手介绍,解决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

Laravel在Windows Docker运行php和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容器时,连不起数据库问题的详细内容,更多请关注自由互联其它相关文章!