How to use multiple database in Lumen

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Multi-Database Access in Lumen: A Developer's Guide

Building robust APIs often requires interacting with disparate data sources—perhaps one database for core user management and another for logging or analytics. When working with Lumen, which is a lightweight framework based on Laravel, accessing multiple databases seamlessly requires understanding how the underlying database abstraction layer handles configuration.

If you are struggling to configure multiple connections within your .env file and need a strategy for reading secondary connections, you are not alone. This guide will walk you through the correct, developer-centric approach to managing several distinct database connections in your Lumen application.

Understanding Database Configuration in Lumen

By default, Laravel and Lumen are configured to manage a single primary database connection defined in the configuration files. To handle multiple databases, we need to leverage the framework's ability to define named connections explicitly. While you can define environment variables for each connection, the actual structure resides in the configuration files, which dictates how the framework interacts with those sources.

The key is defining these connections within your application's configuration structure, typically located in config/database.php (or its equivalent setup in Lumen). Environment variables are perfect for storing sensitive credentials, but they serve as placeholders that feed into the structured configuration.

Step 1: Configuring Multiple Connections

Instead of relying solely on .env entries to define the connections directly (which is often insufficient for complex setups), we structure the definitions for clarity. We will define two separate database configurations: one for the primary application data and one for a secondary service.

In your Lumen setup, you would typically modify or create configuration files to list these connections. For demonstration purposes, let's assume we are setting up mysql_primary and mysql_secondary.

Example Configuration Logic (Conceptual):

// This logic is conceptual, mirroring how Laravel/Lumen structures config
return [
    'connections' => [
        'mysql_primary' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_PRIMARY'),
            'database' => env('DB_DATABASE_PRIMARY'),
            'username' => env('DB_USERNAME_PRIMARY'),
            'password' => env('DB_PASSWORD_PRIMARY'),
        ],

        'mysql_secondary' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_SECONDARY'),
            'database' => env('DB_DATABASE_SECONDARY'),
            'username' => env('DB_USERNAME_SECONDARY'),
            'password' => env('DB_PASSWORD_SECONDARY'),
        ],
    ],
];

Step 2: Accessing the Second Connection Dynamically

Once configured, accessing these connections requires explicitly telling Eloquent (or the underlying DB facade) which connection to use for a specific operation. You don't just rely on the default connection; you specify the connection name.

To read data from your secondary database, you access it via the facade or an Eloquent model instance specifying the connection name:

use Illuminate\Support\Facades\DB;
use App\Models\SecondaryModel; // Assuming you have a model for the secondary DB

class DataService
{
    public function fetchPrimaryData()
    {
        // Accessing the primary database (usually default)
        $users = DB::table('users')->get();
        return $users;
    }

    public function fetchSecondaryData()
    {
        // Explicitly targeting the secondary connection defined in config/database.php
        $secondaryRecords = DB::connection('mysql_secondary')->table('logs')->get();
        return $secondaryRecords;
    }
}

Notice how we use DB::connection('connection_name'). This method allows you to bypass the default connection and directly interact with any named connection you have defined. This pattern ensures that your code remains decoupled from the specific database credentials, following excellent architectural principles championed by resources like https://laravelcompany.com.

Conclusion: Best Practices for Multi-Database Systems

Managing multiple databases in a Lumen environment is entirely achievable and highly effective for complex applications. The key takeaway is to move beyond relying on implicit connections. Instead, treat each database as a distinct service with its own configuration profile.

Best Practices Summary:

  1. Separate Configuration: Keep all connection details separate in your environment variables but structure the framework configuration files (config/database.php) to define named connections explicitly.
  2. Use DB::connection(): Always use the explicit connection method when you need to switch context between databases within your service layer or controllers.
  3. Service Layer Abstraction: Encapsulate database logic within dedicated Service classes (as shown above) rather than scattering raw database calls throughout your controllers. This keeps your business logic clean and testable, which is crucial for any scalable application architecture.

By adopting this approach, you ensure that your Lumen API remains flexible, scalable, and adheres to the robust design principles found in modern PHP development.