How to configure remote database in laravel 5.4

2

I'm setting up a bank in Laravel 5.4, but the same is hosted on another server, set up in Navicat is accessing normally, but Laravel does not connect.

APP_NAME=Laravel
APP_ENV=local
APP_KEY=.........
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

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

DB_CONNECTION=mysql_gps
DB_HOST=192.168.1.125
DB_PORT=3306
DB_DATABASE=meubanco
DB_USERNAME=root
DB_PASSWORD=senha

Database:

'mysql_gps' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '192.168.1.125'),
            'port' => env('DB_PORT', '3306'),
            'database' => 'meubanco',
            'username' =>'root',
            'password' =>  'senha',
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

One of the banks I need to run local and the other remote.

    
asked by anonymous 03.10.2017 / 16:42

1 answer

1

Set up your .env file:

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

DB_CONNECTION1=mysql_gps
DB_HOST1=192.168.1.125
DB_PORT1=3306
DB_DATABASE1=meubanco
DB_USERNAME1=root
DB_PASSWORD1=senha

That is, two settings, where the difference is the number 1, that is, you can in the file .env put as many as you want but identify each one of them.

Go to file app/config/database.php :

<?php

return [

    'default' => env('DB_CONNECTION', 'mysql'),

    'connections' => [

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'mysql_gps' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST1', '127.0.0.1'),
            'port' => env('DB_PORT1', '3306'),
            'database' => env('DB_DATABASE1', 'forge'),
            'username' => env('DB_USERNAME1', 'forge'),
            'password' => env('DB_PASSWORD1', ''),
            'unix_socket' => env('DB_SOCKET1', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

Two settings also pointing to the .env settings.

How to use this?

In documentation on multiple bank settings , if it will use normal would be:

//conexão padrão
$users = DB::connection('mysql')->select(...);
$users = DB::select(...);

or

//conexão remota
$users = DB::connection('mysql_gps')->select(...);

If you want to create this within model is just set the protected $connection the connection name, example :

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Unidade extends Model
{
    protected $fillable = ['titulo'];
    protected $primaryKey = 'id_unidade';
    protected $connection = 'mysql_gps'; // configurando a conexão remota    
}

with the tag 'eloquent' "> eloquent or models that are not configured take by default the configuration that is set to, DB .

References

03.10.2017 / 18:51