Laradock不适用于mysql [英] Laradock doesn't work with mysql

查看:281
本文介绍了Laradock不适用于mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白如何使Laradock与mysql数据库一起正常工作。

I cannot understand how to get Laradock to work correctly with the mysql db.

我已经关注了laradock文档并安装了所有内容,并使用

I have followed laradock docs and installed everything, spin up containers using

docker-compose up -d nginx mysql

我有多个项目版本布局,例如

I have the multiple project version layout like such

project
    +laradock-spa

php端似乎正常工作,我可以打开laravel欢迎页面,但是连接数据库是导致我出现问题。

The php side seems to work, I can get the laravel welcome page up, however getting the DB connected is causing me issues.

首先,我应该在哪里运行php artisan命令,例如 php artisan migration ?应该从我的机器在项目文件夹中运行,还是在docker容器中运行?

Firstly, where should I be running php artisan commands like php artisan migrate ? Should that be run from my machine within the project folder, or from within the docker container ?

当我从项目文件夹运行它时,它可以工作,并且我可以进入mysql容器并查看初始数据库表,例如 migration user

When I run it from my project folder, it works, and I can go into the mysql container and see the initial db tables, like migration, and user.

但是,我无法向数据库获取初始POST在邮递员内工作-我收到错误 SQLSTATE [HY000] [2002]没有这样的文件或目录(SQL:插入用户(...

BUT, I cannot get an initial POST to the db to work within Postman - I get error SQLSTATE[HY000] [2002] No such file or directory (SQL: insert into users (...

所以我想也许我应该从工作区容器中运行migration命令,所以我猛扑到工作区中,但是 php artisan migration code>命令错误,并带有 [Illuminate\Database\QueryException]
SQLSTATE [HY000] [2002]没有这样的文件或目录(SQL:从information_schem
中选择* a.tables,其中table_schema = spa,table_name =迁移)

So I thought maybe I'm supposed to be running the migrate command from within the workspace container, so I bash into the workspace, but from here the php artisan migrate command errors with [Illuminate\Database\QueryException] SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schem a.tables where table_schema = spa and table_name = migrations)

正如我所说,我可以成功进入mysql容器并登录到使用根密码的数据库,并且在运行首次迁移之后,我可以看到其中的表那里。

As I said, I can successfully get into the mysql container, and login to the DB using root password, and after running first migrations, I can see the tables inside there.

docker ps 显示所有容器。

有人可以解释它的工作原理以及如何解决此问题吗?我不确定应该在哪里运行哪些命令,以及各个容器之间如何通信。但基本上,如何使mysql数据库在laravel项目中工作?

Can someone explain how it works and how to troubleshoot this ? I'm not sure where I should be running which commands, and how the containers talk to each other. But basically, how can I get the mysql db to work in the laravel project?

Btw,我也在PC上运行本地mysql,也许那会引起一些冲突/

Btw, I have local mysql running on the PC too, maybe thats causing some conflict/confusion?

我正在Win10上运行所有这些。

I'm running all this on Win10.

推荐答案

使用Laradock时,您需要考虑将容器视为一个共享网络中的独立服务器/计算机。下图的每个矩形代表一个服务器,该服务器具有自己的IP地址,自己的Linux系统等。因此,要从另一个容器连接到MySQL,您需要知道该容器的IP地址或主机名。

when you're using Laradock, you need to think about containers as if they were separate servers/computers within one shared network. Each rectangle at the picture below represents one server which has it's own IP address, own Linux system etc. Therefore to connect to MySQL from another container, you need to know an IP address or hostname of this container.

幸运的是,Laradock在幕后提供了一些魔术来缓解这种情况,您可以使用MySQL主机名代替在配置中提供IP地址。如果要启用Redis,只需要做的是,使用docker-compose启动Redis,并在Laravel配置中提供Redis主机名。

Luckily Laradock provides a little bit of magic under the hood to ease this, and you can use MySQL hostname instead of providing an IP address in your config. If you want to enable Redis, all you have to do is, start Redis with docker-compose up and provide Redis hostname in Laravel config.

如果要进行故障排除, docker-compose ps是检查正在发生的事情的最佳方法。其余的是对多容器概念的理解。

If it comes to troubleshooting, docker-compose ps is the best way to check what is going on. The rest is an understanding of a multi-container concept.

请注意,如果不暴露docker-compose.yml中的容器端口,就无法从主机PC连接到容器。 。容器IP对主机不可见,因为它们属于虚拟网络,而不是计算机所连接的真实IP。默认端口已在docker-compose.yml中公开,因此您可以访问IP 127.0.0.1和端口3306上的数据库,与Nginx在端口80上相同,依此类推。

Beware you can't connect to your containers from your host PC without exposing containers port in docker-compose.yml. Containers IPs are not visible for a host, as they belong to a virtual network, not the real one where your computer is connected. Default ports are already exposed in docker-compose.yml so you can access DB at IP 127.0.0.1 and port 3306, same with Nginx at port 80 and so on.

希望现在可以更清楚了:)

Hope this is a bit more clear now :)

这篇关于Laradock不适用于mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆