问题
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