连接到数据库时 Laravel 连接超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42303802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel connection timeout when connecting to database
提问by whitwhoa
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.
我正在将应用程序从我的开发机器移动到测试服务器。当连接到我的本地开发 mysql 数据库时,一切都按预期工作。尝试连接到我们的测试服务器时,请求会在 45 秒后超时并返回 500 错误。
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:
我测试了服务器可以通信并且php可以通过使用基本的mysqli php功能获取结果,并且结果按预期返回:
// 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();
The following both fail and hit timeout limits in laravel:
以下在 Laravel 中失败并达到超时限制:
$users = DB::table('users')->get();
$users = User::all();
Thoughts? Ideas? Opinions?
想法?想法?意见?
回答by Konrad
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.
我遇到过同样的问题。我做了var_dump(DB::connection());
并发现主机值错误,因为加载了错误的 .env 文件。