Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.

Get Started Now!

Laravel Multi-Database Configuration: Best Practices and Setup Guide

To configure multiple databases in Laravel, you’ll need to modify your .env file as well as the config/database.php file to manage the connections.

Step 1: Update the .env file

In your .env file, define the settings for both databases. You can have a default connection as well as a second connection with a new prefix.

Example .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=default_database
DB_USERNAME=root
DB_PASSWORD=password

DB_SECOND_CONNECTION=mysql
DB_SECOND_HOST=127.0.0.1
DB_SECOND_PORT=3306
DB_SECOND_DATABASE=second_database
DB_SECOND_USERNAME=root
DB_SECOND_PASSWORD=password

Step 2: Configure config/database.php

Now, go to the config/database.php file and configure the connections. Laravel uses the default connection for the database interactions unless specified otherwise. To add multiple database connections, follow these steps:

In the connections array inside config/database.php, add a new connection for the second database like this:

'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,
    ],

    'mysql2' => [
        'driver' => 'mysql',
        'host' => env('DB_SECOND_HOST', '127.0.0.1'),
        'port' => env('DB_SECOND_PORT', '3306'),
        'database' => env('DB_SECOND_DATABASE', 'forge'),
        'username' => env('DB_SECOND_USERNAME', 'forge'),
        'password' => env('DB_SECOND_PASSWORD', ''),
        'unix_socket' => env('DB_SECOND_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
],

Step 3: Using Multiple Databases in Your Code

Now that you have multiple connections, you can specify which connection to use in your models, queries, or migrations.

For example, if you want to use the second database connection in a query:

$users = DB::connection('mysql2')->select('SELECT * FROM users');

Or if you want to specify a connection for a particular model:

class User extends Model
{
    protected $connection = 'mysql2';  // Use the second database connection
}

Step 4: Running Migrations

To run migrations for different connections, you can specify the connection like this:

php artisan migrate --database=mysql2

This will run the migrations on the second database connection.

By following these steps, you can easily configure and use multiple databases in Laravel.

Related Posts

How to Deploy a Laravel Project on a Linux Server Using HTDOCS and GitHub – Complete Step-by-Step Guide

Deploying a Laravel project on a Linux server becomes simple when you follow a structured process. In this guide, we will deploy a Laravel project by cloning Read More

Read More

How to Install and Enable GMP Extension in XAMPP PHP on Linux (Step-by-Step Guide)

Introduction If you are using XAMPP PHP on a Linux server and encounter errors like: then this usually means the GMP extension is not installed, not just Read More

Read More

How to Run an HTML Website Alongside a Laravel Project on the Same Server

Running a Laravel application is common for dynamic web platforms, dashboards, and APIs. However, many businesses and developers also want to run a simple HTML website on Read More

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x