Featured image of post Configuring Multiple MySQL Connections in Laravel 13

Configuring Multiple MySQL Connections in Laravel 13

Configure additional MySQL connections in Laravel 13 and select the correct database connection in Eloquent models and transactions.

Configuring Multiple MySQL Connections in Laravel 13

A Laravel application may need to connect to more than one database. For example, the primary application database may be separate from a legacy database.

Laravel supports multiple database connections through config/database.php.

1. Configure the Default MySQL Connection

If your application uses MySQL as its primary database, specify the driver in .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=app_database
DB_USERNAME=root
DB_PASSWORD=your_password

If DB_CONNECTION is omitted, Laravel uses the default configured in config/database.php. In a fresh project, this may be SQLite.

2. Add a Second MySQL Connection

Open config/database.php and add another entry under connections:

'mysql_legacy' => [
    'driver' => 'mysql',
    'host' => env('DB_LEGACY_HOST', '127.0.0.1'),
    'port' => env('DB_LEGACY_PORT', '3306'),
    'database' => env('DB_LEGACY_DATABASE', 'legacy'),
    'username' => env('DB_LEGACY_USERNAME', 'root'),
    'password' => env('DB_LEGACY_PASSWORD', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
],

Add the corresponding environment variables:

DB_LEGACY_HOST=mysql-legacy
DB_LEGACY_PORT=3306
DB_LEGACY_DATABASE=legacy_database
DB_LEGACY_USERNAME=your_username
DB_LEGACY_PASSWORD=your_password

When using Docker Compose, the host should normally be the database service name if both containers share a network.

3. Assign a Connection to an Eloquent Model

You can tell a model to use the additional connection:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class VcodeAdapter extends Model
{
    protected $connection = 'mysql_legacy';

    protected $table = 'vcode_adapter';

    protected $primaryKey = 'autokid';

    public $timestamps = false;
}

The model will now use mysql_legacy instead of the default database connection.

If your primary key is not an auto-incrementing integer, configure $incrementing and $keyType to match the actual column.

4. Use the Selected Connection in Transactions

When working with a model that uses a custom connection, make sure the transaction uses the same connection.

For example:

use Illuminate\Support\Facades\DB;

DB::connection('mysql_legacy')->transaction(function () {
    VcodeAdapter::create([
        // Model attributes.
    ]);
});

If the model uses the default connection instead, you can simply write:

DB::transaction(function () {
    // Database operations.
});

Mixing a model connection with a transaction on another connection can cause the model operations to execute outside the intended transaction.

5. Clear Configuration Cache

After changing .env or config/database.php, clear the configuration cache:

php artisan config:clear

Conclusion

Laravel 13 makes it straightforward to configure multiple MySQL connections.

Define each connection in config/database.php, provide its credentials through environment variables, and explicitly select the appropriate connection in models and transactions.

comments powered by Disqus