简体   繁体   English

Laravel 数据库 SQLSTATE[HY000] [2002] 连接被拒绝

[英]Laravel Database SQLSTATE[HY000] [2002] Connection refused

I'm having trouble connecting to database with my laravel project.我无法使用我的 laravel 项目连接到数据库。 Using docker and sail.使用码头工人和帆。 I'm hosting a XAMPP MySQL database, on localhost, instead of the built in.我在 localhost 上托管了一个 XAMPP MySQL 数据库,而不是内置的。

My .env:我的.env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=site_data
DB_USERNAME=dbUser
DB_PASSWORD=PassWord

The interesting thing is that the first migration attempt worked, and created the tables.有趣的是,第一次迁移尝试成功了,并创建了表。 But when I created an other migration, it refused to connect, and is still not connecting.但是当我创建另一个迁移时,它拒绝连接,并且仍然没有连接。 Using TablePlus, given these credentials I can connect, and I see all the tables.使用 TablePlus,给定这些凭据,我可以连接,并且可以看到所有表。

I've tried: Restarting Clearing cache, route, view, config Rebuilding the whole sail, multiple times我试过:重新启动清除缓存,路线,视图,配置重建整个帆,多次

Any ideas what could have happened?任何想法可能会发生什么?

Thanks in advance!提前致谢!

It seems like you might be connecting to the wrong Database.看来您可能连接到错误的数据库。 If in your env you need to connect to db which is hosted by docker, then you should follow next approach: docker ps -a it will list all of the available containers.如果在您的环境中您需要连接到由 docker 托管的 db,那么您应该遵循下一个方法: docker ps -a它将列出所有可用的容器。 You can reference do actual container name in your env.您可以在环境中引用实际容器名称。 Just as an example:举个例子:

DB_HOST=docker-mysql
DB_PORT=3306

In this case you need to specify port inside of the docker network :)在这种情况下,您需要在 docker 网络中指定端口 :)

Could you specify your actual development env?你能指定你的实际开发环境吗? Since i didnt get it based on the question.因为我没有根据这个问题得到它。

You have XAMP - which is running and something in docker?你有 XAMP - 它正在运行并且在 docker 中有东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM