DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Spring Microservice Tip: Abstracting the Database Hostname With Environment Variable
  • Spring Microservice Application Resilience: The Role of @Transactional in Preventing Connection Leaks
  • Building and Integrating REST APIs With AWS RDS Databases: A Node.js Example
  • Address Non-Functional Requirements: How To Improve Performance

Trending

  • Orchestrating Edge Computing with Kubernetes: Architectures, Challenges, and Emerging Solutions
  • The AWS Playbook for Building Future-Ready Data Systems
  • Seata the Deal: No More Distributed Transaction Nightmares Across (Spring Boot) Microservices
  • Stop Prompt Hacking: How I Connected My AI Agent to Any API With MCP
  1. DZone
  2. Data Engineering
  3. Data
  4. Configuring Multiple Database Connections in Lumen

Configuring Multiple Database Connections in Lumen

Connecting to one database in Lumen is easy enough, but what about connecting to multiple databases? Come find out how to handle this situation.

By 
Paul Underwood user avatar
Paul Underwood
·
Jul. 08, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
23.2K Views

Join the DZone community and get the full member experience.

Join For Free

In Lumen you can create a database connection by filling out the default information you can find in .env file. But if you need multiple database connections you can do so by adding a new database config file and creating a list of the new database connections.

Create a database config file /config/database.php.

Add an array of database information.

return [
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],
        'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB2_HOST'),
            'database' => env('DB2_DATABASE'),
            'username' => env('DB2_USERNAME'),
            'password' => env('DB2_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],
    ]
];

By setting a default element will allow you to choose which database to use when you call DB::connection();. By placing the env() function inside the configuration file you can manage the database credentials on a per environment basis.

To use the different databases in your code you need to define which connection you want by passing in the name of the connection by either using the app() helper function or the DB facade.

// Use default connection
app('db')->connection()->select('xx');
DB::connection()->select('yy');

// Use mysql2 connection
app('db')->connection('mysql2')->select('xx');
DB::connection('mysql2')->select('yy');
Database connection

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Microservice Tip: Abstracting the Database Hostname With Environment Variable
  • Spring Microservice Application Resilience: The Role of @Transactional in Preventing Connection Leaks
  • Building and Integrating REST APIs With AWS RDS Databases: A Node.js Example
  • Address Non-Functional Requirements: How To Improve Performance

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: