Laravel Seperate Database Connections for Read and Write Queries - MySQL, SQL Server


How to Configure Multiple Database connection with Laravel for separating Select,Read Queries and Insert,Update,Delete Queries

At the end of the last summer, I received the following email in my inbox, from one of my follower on techalyst.com, asking me, whether it's possible to configure two different Database, one database for executing Select/Read SQL Statements and another Database for executing Insert,Update,Delete SQL Statements, his idea was to optimize Query performance by separating two database connections for Data Query Language (Select) and  Data Manipulation Language (Insert,Update,Delete).

Laravel makes it extremely simple to Configure Separate Database Connection for Read and Write Statement respectively. The database configuration for your application is located at Laravel Project Root Folder, config/database.php. In this file you may define Read,Write database connection Configuration for your Laravel Application.

Let's assume we have two different MySQL Database in different servers with similar username, password for Database user,
this is how you will configure it, 

First go to the section where you define your MySQL database connection in config/database.php file, if you haven't already defined it, follow one of my earlier post on how to do it, Laravel Connecting to MySQL database - Laravel 5 .

in the mysql configuration array, add two extra properties read and write arrays,  inside the new array you can provide different configurations for Read SQL statements and write SQL statements respectively.

'mysql' => [
    'read' => [
        'host' => '192.168.1.1',
    ],
    'write' => [
        'host' => '196.168.1.2'
    ],
    'driver'    => 'mysql',
    'database'  => 'database_name',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
],


Let's assume we have two different MySQL Database in different servers with different username, password for Database user,
this is how you will configure it, 

'mysql' => [
    'read' => [
        'host' => '192.168.1.1',
        'username'  => 'username1',
        'password'  => 'password1',
    ],
    'write' => [
        'host' => '196.168.1.2',
        'username'  => 'username2',
        'password'  => 'password2',
    ],
    'driver'    => 'mysql',
    'database'  => 'database_name',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
],


if you have any questions regarding "Laravel Separate Database Connection for Read/Select statements and Insert,Update,Delete statements", please feel free to leave your comment bellow.


Written by Akram Wahid 6 years ago

are you looking for a chief cook who can well craft laravel and vuejs, to make some awsome butterscotch,
yes then it is right time for you to look at my profile.

Do you want to write Response or Comment?

You must be a member of techalyst to proceed!

Continue with your Email ? Sign up / log in

Responses

Be the first one to write a response :(

{{ item.member.name }} - {{ item.created_at_human_readable }}

{{ reply.member.name }} - {{ reply.created_at_human_readable }}