连接到数据库时的Laravel连接超时 [英] Laravel connection timeout when connecting to database

查看:964
本文介绍了连接到数据库时的Laravel连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将应用程序从开发计算机移至测试服务器.当连接到我的本地开发mysql数据库时,一切正常.尝试连接到我们的测试服务器时,请求在45秒后超时,并返回500错误.

I am moving an application from my development machine to a test server. When connecting to my local development mysql database everything works as expected. When attempting to connect to our test server, the requests time out after 45 seconds and a 500 error is returned.

我测试了服务器可以通信,并且php可以使用基本的mysqli php功能获得结果,并且按预期返回结果:

I tested that the servers can communicate and php can get results by using the basic mysqli php functionality, and results are returned as expected:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo var_export($row, true);
    }
} else {
    echo "0 results";
}
$conn->close();

以下在laravel中失败和命中的超时限制:

The following both fail and hit timeout limits in laravel:

$users = DB::table('users')->get();

$users = User::all();

有什么想法吗?有想法吗?意见?

Thoughts? Ideas? Opinions?

推荐答案

我遇到了同样的问题.我做了var_dump(DB::connection());,发现主机值是错误的,因为加载了错误的.env文件.

I had the same issue. I did var_dump(DB::connection()); and found out that the host value was wrong because a wrong .env file was loaded.

这篇关于连接到数据库时的Laravel连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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