SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time,

陌路散爱 提交于 2021-01-29 11:34:39

问题


I try to connect 2 database in my Laravel 5.6 project, but i got error like this

SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (SQL: select * from mt_merchant)

I tried following code:

.env

1st db connection (this is in my local server)

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=thedaldb
DB_USERNAME=root
DB_PASSWORD=

2nd db connection (this is in live server 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;
    }
}

Simply i want to connect live server database in my local sever project


回答1:


At the "Connectivty and Security" of your RDS instance page check if the configuration have "Public Accesibility" on "Yes" option. Maybe you don't have a public IP associated to your instance.

https://aws.amazon.com/premiumsupport/knowledge-center/rds-connectivity-instance-subnet-vpc/



来源:https://stackoverflow.com/questions/54418521/sqlstatehy000-2002-a-connection-attempt-failed-because-the-connected-party-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!