laravel实现数据库多库配置,读写分离配置或者多读写分离配置
'connections' => array( //默认mysql配置,访问test库 'mysql' => array( 'driver' => 'mysql', 'host' => '192.168.16.70', 'port' => '3306', 'database' => 'test', 'username' => 'test', 'password' => '123456', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ), //自定义mysql连接,访问media库 'media' => array( 'read' => array( array('host' => '192.168.16.74','port' => 3306), array('host' => '192.168.16.70','port' => 3306), ), 'write' => array( 'host' => '192.168.16.70', 'port'=> 3306 ), 'driver' => 'mysql', 'database' => 'media', 'username' => 'test', 'password' => '123456', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ), )
这样使用,新建一模型User.php
class User extends Eloquent{ //不设置属性,默认连接mysql配置 protected $connection = 'media';
}