本文介绍了SQLSTATE [HY000] [2002]连接尝试失败,因为一段时间后被连接方未正确响应,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在我的 Laravel 5.6
项目中连接2个数据库,但出现了这样的错误
I try to connect 2 database in my Laravel 5.6
project, but i got error like this
我尝试了以下代码:
.env
第一个数据库连接(在我的本地服务器中)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=thedaldb
DB_USERNAME=root
DB_PASSWORD=
第二个数据库连接(在实时服务器IP 18.188.209.59中)
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND= 18.188.209.59
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=delivery_test_copied
DB_USERNAME_SECOND=testuser
DB_PASSWORD_SECOND=testuser@123
app/config/database.php
'connections' => [
'onlineorder' => [
'driver' => env('DB_CONNECTION_SECOND'),
'host' => env('DB_HOST_SECOND'),
'port' => env('DB_PORT_SECOND'),
'database' => env('DB_DATABASE_SECOND'),
'username' => env('DB_USERNAME_SECOND'),
'password' => env('DB_PASSWORD_SECOND'),
],
]
app/Models/OnlineOrder/OnlineOrder.php
<?php
namespace App\Models\OnlineOrder;
use Illuminate\Database\Eloquent\Model;
class OnlineOrder extends Model
{
protected $connection = 'onlineorder';
}
app/Http/Controllers/Onlineorder/AppOnlineOrderController.php
<?php
namespace App\Http\Controllers\Onlineorder;
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Models\OnlineOrder\OnlineOrder;
use Illuminate\Http\Response;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Validator;
use Config;
use File;
use DB;
class AppOnlineOrderController extends Controller
{
public function test($merchant_id)
{
$db_ext = DB::connection('onlineorder');
$merchant = $db_ext->table('mt_merchant')->get(); //mt_merchant is table name
print_r($db_ext);exit;
}
}
推荐答案
IP地址始终会随时间变化...而不是使用定向IP,而是使用主域就像:
IP address always changes during time ... instead of using directed IP use main domainlike :
DB_HOST_SECOND=mydomain.com.sd
我认为这样做会
这篇关于SQLSTATE [HY000] [2002]连接尝试失败,因为一段时间后被连接方未正确响应,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!