I have 2 Laravel apps hosted on different servers,
App 1 is built with Laravel 8
and app2 with Laravel 9
, I want get/set data from app1 to app2
So I implemented multiple DB connection in app1 like this :
databese.php
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_SECOND', 'localhost'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'forge'),
'username' => env('DB_USERNAME_SECOND', 'forge'),
'password' => env('DB_PASSWORD_SECOND',''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
],
.env :
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=https://domain2.com
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=db_name
DB_USERNAME_SECOND=db_username
DB_PASSWORD_SECOND="db_password" // I tried also without ""
PurchaiseCode.php
model : this is the model which I will used to get/set data in app2 ( there is a table in DB app2 named purchaise_codes)
class PurchaiseCode extends Model
{
use HasFactory;
protected $connection = 'mysql2';
protected $table = 'purchaise_codes';
protected $fillable = [
'code',
'name',
'email',
];
}
Issue
SQLSTATE[HY000] [2002] Connection refused
when I try to insert data to purchaise_codes
table from app1
I tested the connection with a route :
Route::get('test/db', function() {
$db = DB::connection('mysql2');
return response()->json([
'res' => $db->getPDO(),
]);
});
and I have this error also.