Home  >  Article  >  PHP Framework  >  Solve the problem that Laravel cannot connect to the database when using Windows docker to run php containers and mysql containers

Solve the problem that Laravel cannot connect to the database when using Windows docker to run php containers and mysql containers

藏色散人
藏色散人forward
2021-03-19 17:06:103042browse

The following tutorial column of laravel will introduce to you the method to solve the problem that Laravel cannot connect to the database when using Windows docker to run the php container and mysql container. I hope it will help you if you need it. Friends help!

Solve the problem that Laravel cannot connect to the database when using Windows docker to run php containers and mysql containers

Problem

When the Laravel project uses Windows docker to run the php container and mysql container, execute php in windows git bash artisan migrate found that the database could not be found.

Error message:

   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.

Cause

When executing php artisan migrate under windows, the mysql in the container cannot be connected

Solution

Rewrite a shell command to encapsulate the php artisan command, and execute php artisan after creating the container as follows:

phpartisan file content

#!/usr/bin/env sh

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

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

Execute php artisan * and replace the command with phpartisan *

Recommendation: Latest Five Laravel Video Tutorials

The above is the detailed content of Solve the problem that Laravel cannot connect to the database when using Windows docker to run php containers and mysql containers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete