How to set up remote database in Laravel 5.4

Asked

Viewed 1,104 times

2

I am setting up bank in Laravel 5.4, but the same is hosted on another served, configured 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 need to rotate local and the other remote.

  • How are you doing? Put the code

  • 1

    See the database.php file as configured.

  • This Line means something: APP_ENV=local

  • 1

    @Carlosalexwe ran this setup on .env will work only for a database, what you really need to do???

  • I am developing, but in the remote bank I receive information at all times and I need to test some events, and the location and only configuration, authentication and everything else

  • 1

    @Carlosalexwe can point out in the model a certain connection a standard for your system and one according to your need and on .env, can only have one configuration, because one overrides the other, already in config/database.php can have several and use in certain models

  • Maybe I’ll try, more local the way it’s working, but I’ll try anyway

Show 2 more comments

1 answer

1

Configure your file .env:

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

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

Goes in the 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 file settings .env.

How to use this?

In the documentation on multiple bank configurations, if used normally, it 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 it within model just configure the protected $connection the name of the connection, 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    
}

Observing: the models or DB that are not configured take by default the configuration that is configured in,'default' => env('DB_CONNECTION', 'mysql'),.

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.