Home> PHP Framework> Laravel> body text

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

藏色散人
Release: 2021-03-22 08:58:07
forward
2944 people have browsed it

The following tutorial column oflaravelwill 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.
Copy after login

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 $*"
Copy after login

Executephp artisan *and replace the command withphpartisan *

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!