php Laravel 5.1 - 检查数据库连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33432752/
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 5.1 - Checking a Database Connection
提问by Enijar
I am trying to check if a database is connected in Laravel.
我正在尝试检查 Laravel 中是否连接了数据库。
I've looked around the documentation and can't find anything. The closest thing I've found is this, but this doesn't solve my problem.
我环顾了文档,找不到任何东西。我发现的最接近的是this,但这并不能解决我的问题。
I have three instances of MySQL that are set up on different machines. Below is a simplified version of what I am trying to achieve.
我在不同的机器上设置了三个 MySQL 实例。下面是我想要实现的简化版本。
- If database 1 is connected, save data to it
- If database 1 is not connected, check if database 2 is connected
- If database 2 is connected save data to it
- If database 2 is not connected, check if database 3 is connected
- If database 3 is connected, save data to it
- 如果数据库 1 已连接,则将数据保存到其中
- 如果数据库1未连接,检查数据库2是否连接
- 如果连接了数据库 2,则将数据保存到其中
- 如果数据库2没有连接,检查数据库3是否连接
- 如果数据库 3 已连接,则将数据保存到其中
To be clear, is there a way to check that a database is connected in Laravel 5.1?
需要明确的是,有没有办法在 Laravel 5.1 中检查数据库是否已连接?
回答by alexw
Try just getting the underlying PDO instance. If thatfails, then Laravel was unable to connect to the database!
尝试只获取底层 PDO 实例。如果是失败,那么Laravel无法连接到数据库!
// Test database connection
try {
DB::connection()->getPdo();
} catch (\Exception $e) {
die("Could not connect to the database. Please check your configuration. error:" . $e );
}
回答by Lubo? Mi?atsky
You can use alexw'ssolution with the Artisan. Run following commands in the command line.
您可以将alexw 的解决方案与 Artisan 一起使用。在命令行中运行以下命令。
php artisan tinker
DB::connection()->getPdo();
If connection is OK, you should see
如果连接正常,您应该看到
CONNECTION_STATUS: "Connection OK; waiting to send.",
CONNECTION_STATUS: "连接正常;等待发送。",
near the end of the response.
接近响应结束。
回答by Luca C.
You can use this, in a controller method or in an inline function of a route:
您可以在控制器方法或路由的内联函数中使用它:
try {
DB::connection()->getPdo();
if(DB::connection()->getDatabaseName()){
echo "Yes! Successfully connected to the DB: " . DB::connection()->getDatabaseName();
}else{
die("Could not find the database. Please check your configuration.");
}
} catch (\Exception $e) {
die("Could not open connection to database server. Please check your configuration.");
}
回答by Dawlatzai Ghousi
You can use this query for checking database connection in laravel:
您可以使用此查询来检查 laravel 中的数据库连接:
$pdo = DB::connection()->getPdo();
if($pdo)
{
echo "Connected successfully to database ".DB::connection()->getDatabaseName();
} else {
echo "You are not connected to database";
}
For more information you can checkout this page https://laravel.com/docs/5.0/database.
有关更多信息,您可以查看此页面https://laravel.com/docs/5.0/database。
回答by ArtisanBay
Another Approach:
另一种方法:
When Laravel tries to connect to database, if the connection fails or if it finds any errors it will return a PDOException
error. We can catch this error and redirect the action
当 Laravel 尝试连接数据库时,如果连接失败或发现任何错误,它将返回PDOException
错误。我们可以捕获此错误并重定向操作
Add the following code in the app/filtes.php
file.
在app/filtes.php
文件中添加以下代码。
App::error(function(PDOException $exception)
{
Log::error("Error connecting to database: ".$exception->getMessage());
return "Error connecting to database";
});
Hope this is helpful.
希望这是有帮助的。