php-Laravel与连接MySQL的Docker问题
作者:互联网
我遵循的使用Docker设置Laravel的步骤:在我的本地系统中,我没有安装PHP,Composer,Apache,MySQL,phpMyAdmin等.在我的系统中仅安装了Git和Docker.
> git克隆https://github.com/laravel/laravel.git
>在项目根目录下创建docker-composer.yml文件.
version: "3"
services:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: db
MYSQL_USER: root
MYSQL_PASSWORD: pass
ports:
- "3306:3306"
web:
image: php:7.2.2-apache
container_name: web_laravel
depends_on:
- db
volumes:
- ./:/var/www/html/
ports:
- "4000:80"
stdin_open: true
tty: true
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- db
external_links:
- db:mysql
ports:
- "9191:80"
environment:
MYSQL_USER: root
MYSQL_PASSWORD: pass
MYSQL_ROOT_PASSWORD: pass
PMA_HOST: db
>从项目根目录运行命令.
docker-compose up
该命令将从本地缓存或Docker Hub中获取所有映像(php:7.2.2-apache,phpmyadmin / phpmyadmin,mysql:5.7),并为这些映像启动三个容器.
现在,我需要与名为web_laravel的php:7.2.2-apache图像容器进行交互(请参见yml文件),以便添加PHP扩展和Composer来运行Laravel项目.
>运行此命令.
docker exec -it web_laravel /bin/bash
现在我可以在运行web_laravel容器中运行任何命令,因此我已经安装了Composer和PHP扩展,例如mbstrings,pdo,pdo_mysql等.
然后使用composer install安装Laravel依赖项,设置存储和bootstrap / cache文件夹的权限,并运行php artisan key:generate.
打开localhost:4000,我可以看到Laravel主页:
至此一切都很好.现在,当我连接到数据库时,问题开始了.
下一条要运行的命令(我仍在容器中):
php artisan migrate
错误是:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)
Illuminate\Database\QueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)
我能够打开phpMyAdmin(http://localhost:9191)并可以创建数据库,表和与操作相关的数据库.我有DB叫做Blog.
MySQL环境变量:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=pass
解决方法:
我认为您有几个问题,首先是laravel .env配置应指向MySQL容器,而不是本地主机
DB_HOST = 127.0.0.1
应该
DB_HOST = db
您谈论的另一个错误与Docker不相关
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] No such
file or directory (SQL: select * from information_schema.tables where
table_schema = blog and table_name = migrations)Illuminate\Database\QueryException : could not find driver (SQL:
select * from information_schema.tables where table_schema = blog and
table_name = migrations)
它可能与缺少依赖项有关,应该运行
作曲家要求教义/ dbal
另外,您说:
now I have access to run any command in running web_laravel container
so I’ve installed composer and php extensions like mbstrings, pdo,
pdo_mysql etc.
您应该在PHP 7 / Apache映像之上构建映像,并将其添加到构建dockerfile中,因为您的更改(php扩展,配置…等)不是持久性的.我建议您使用Laradock或任何其他现有的Laravel / docker环境.
标签:laravel-5,docker,docker-compose,mysql,php 来源: https://codeday.me/bug/20191108/2007271.html