0

I have two database connections. One is postgresql (no problem) and the other is mysql.

I' ve been able to connect to the database and dump data from a file with this command:

cat cosmo.sql | docker exec -i edb9f60b1531 /usr/bin/mysql -u sail --password=password testing

But when I try to use eloquent it hangs up. This is a test I did:

use App\Models\Access;
Access::find(1);

   Illuminate\Database\QueryException  SQLSTATE[HY000] [2006] MySQL server has gone away (Connection: mysql, SQL: select * from `access` where `access`.`id` = 1 limit 1).

This is from my .env file:

DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=entropia_api
DB_USERNAME=sail
DB_PASSWORD=password

DB_CONNECTION2=mysql
DB_HOST2=mysql
DB_PORT2=3306
DB_DATABASE2=testing
DB_USERNAME2=sail
DB_PASSWORD2=password

This is my model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DisposAcceso extends Model
{
    use HasFactory;

    protected $connection = 'mysql';

    protected $table = 'access';
}

I haven't changed config/database.php, so I have the default connections.

Luis Urán
  • 49
  • 4

1 Answers1

0

To connect to multiple databases using Laravel Sail, follow these steps:

  1. Add additional database configurations to the docker-compose.yml file. For example, to add a MySQL and a PostgreSQL database, add the following lines to the docker-compose.yml file:

    mysql:
    image: 'mysql:8.0'
    ports:
      - '${FORWARD_DB_PORT:-3306}:3306'
    environment:
      MYSQL_DATABASE: '${DB_DATABASE}'
      MYSQL_USER: '${DB_USERNAME}'
      MYSQL_PASSWORD: '${DB_PASSWORD}'
      MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
    volumes:
      - 'sailmysql:/var/lib/mysql'
    networks:
      - sail
    container_name: sail-mysql
    
    pgsql:
    image: 'postgres:13'
    ports:
      - '${FORWARD_DB_PORT:-5432}:5432'
    environment:
      POSTGRES_DB: '${DB_DATABASE}'
      POSTGRES_USER: '${DB_USERNAME}'
      POSTGRES_PASSWORD: '${DB_PASSWORD}'
    volumes:
      - 'sailpgsql:/var/lib/postgresql/data'
    networks:
      - sail
    container_name: sail-pgsql
    
  2. Add the additional database configurations to the .env file. For example:

    DB_CONNECTION=mysql
    DB_HOST=mysql
    DB_PORT=3306
    DB_DATABASE=my_database
    DB_USERNAME=my_username
    DB_PASSWORD=my_password
    
    DB_CONNECTION_PGSQL=pgsql
    DB_HOST_PGSQL=pgsql
    DB_PORT_PGSQL=5432
    DB_DATABASE_PGSQL=my_database_pgsql
    DB_USERNAME_PGSQL=my_username_pgsql
    DB_PASSWORD_PGSQL=my_password_pgsql
    
  3. In your Laravel application, add the additional database connections to the config/database.php file. For example:

    'connections' => [
    
        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            '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', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
        ],
    
        'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL_PGSQL'),
            'host' => env('DB_HOST_PGSQL', '127.0.0.1'),
            'port' => env('DB_PORT_PGSQL', '5432'),
            'database' => env('DB_DATABASE_PGSQL', 'forge'),
            'username' => env('DB_USERNAME_PGSQL', 'forge'),
            'password' => env('DB_PASSWORD_PGSQL', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],
    
    ],
    
  4. To use the additional database connection, specify the connection name in your database queries. For example:

    $users = DB::connection('pgsql')->select('select * from users');
    
halfer
  • 19,824
  • 17
  • 99
  • 186
Nileshsinh Rathod
  • 948
  • 1
  • 17
  • 37