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.
Be the first one to write a response :(
{{ reply.member.name }} - {{ reply.created_at_human_readable }}