0
votes

I am trying to connect remote database from my localhost, the cause of I want to store data localhost to remote server. Now I want to show simply users table info it get form my localhost and different variable show remote server users table info . I write the code example here :

In my Config/database.php:

'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'realstate'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

    'shahin' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', '104.219.248.3'),
        'database'  => env('DB_DATABASE', 'laraveldb'),
        'username'  => env('DB_USERNAME', 'laraveldb_username'),
        'password'  => env('DB_PASSWORD', 'secret'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

In Route.php :

Route::get('mpdb',function(){
      $userArray = DB::table('users')->get();
      echo "<pre>";
      print_r($userArray);
      echo "</pre><br>";           
      $users2 = DB::connection('shahin');
      $u = $users2->table('users')->get();
      echo "<pre>";
      print_r($u);
});

Local Database work's properly but Remote database get error!!

ERROR : 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.

1
I guess your remote is hosted some where out, most web hoster block remote access to mysql. You can ask your host provider for solution. If it is your server, then you have to check few things, ip accessibility, user created, database created and it should works. - maytham-ɯɐɥʇʎɐɯ
If your provider has blocked access so you have talk to your provider, there is no solution as standard. - maytham-ɯɐɥʇʎɐɯ
I have the same issue and I am using local server... any ideas?? - Brane

1 Answers

0
votes

To allow external web servers to access your MySQL databases, add their domain names to the list of hosts that are able to access databases on your web site. see a hosted cpanel demo(Image)

If you want to all incoming hosts you may access like this %.%.%.% You can see this picture How to permit other incoming host(Image)