config/database.php

...
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
/* test master/slave */
'laravel_test_ms'=>array(
'read'=>array(
'host' => '10.0.13.46',
'username' => 'laravel_reader',
'password' => 'qwerqwer123',
),
'write'=>array(
'host' => '10.0.13.46',
'username' => 'laravel_writer',
'password' => '1qaz1qaz123',
),
'driver' => 'mysql',
'database' => 'laravel',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
),
/* 多数据库连接-读写分离-手动 */
'laravel_reader'=>array(
'driver' => 'mysql',
'host' => '10.0.13.46',
'database' => 'laravel',
'username' => 'laravel_reader',
'password' => 'qwerqwer123',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
),
'laravel_writer'=>array(
'driver' => 'mysql',
'host' => '10.0.13.46',
'database' => 'laravel',
'username' => 'laravel_writer',
'password' => '1qaz1qaz123',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
),
...

app/Http/routes.php

Route::get('tongji/info', 'TongjiController@info');
Route::get('tongji/multi', 'TongjiController@multi');
Route::get('tongji/ms', 'TongjiController@ms');

app/Http/Controllers/TongjiController.php

<?php
namespace App\Http\Controllers;
use App\Tongji;
class TongjiController extends Controller
{ /**
* 多数据库连接-读写分离-手动
* @access public
* @author zhaoyingnan 2016-09-18 17:05
* @return mix
* @note
**/
public function multi()
{
return Tongji::mMultiDBConfTest(2);
}
/**
* 框架自动读写分离
* @access public
* @author zhaoyingnan 2016-09-18 17:53
* @param int $iVar
* @return mix
* @note
**/
public function ms()
{
return Tongji::mMasterSlaveTest(1);
}
}
?>

app/Tongji.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
#use Illuminate\Support\Facades\DB;
use DB;
class Tongji extends Model
{
/**
* 1.读写分离-多数据库连接(database.php)-手动
* @access public
* @author zhaoyingnan 2016-09-18 17:00
* @param int $iType
* @return mix
* @note
**/
static function mMultiDBConfTest($iType = 1)
{
# 数据库权限设置
# grant select on laravel.* to laravel_reader@'%' identified by 'qwerqwer123';
# grant select,insert,update,delete on laravel.* to laravel_writer@'%' identified by '1qaz1qaz123';
# flush privileges;
if($iType == 1)
{
# reader
return DB::connection('laravel_reader')->select('select * from student limit 1');
}
else
{
# writer
return DB::connection('laravel_writer')->update('update student set `age`=:age where id=:id', array('age'=>22, 'id'=>1));
}
}
/**
* 2.读写分离-框架自动
* @access public
* @author zhaoyingnan 2016-09-18 17:41
* @param int $iType
* @return mix
* @note
**/
static function mMasterSlaveTest($iType = 1)
{
if($iType == 1)
{
# reader
return DB::connection('laravel_test_ms')->select('select * from student limit 1');
}
else
{
# writer
return DB::connection('laravel_test_ms')->update('update student set `age`=:age where id=:id', array('age'=>20, 'id'=>3));
}
}
}
?>

随机推荐

  1. UVA&amp;&amp;POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  2. hdoj 2075 A|B?

    A|B? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. Android获取APK包名的几种方法

    Android获取APK包名的几种方法:1.adb shell pm list package -f | findstr 关键字 #只能获取到包名,主Activity名无法获取到 2.使用aapt-- ...

  4. Unity3d 模型导入选项

  5. 机器学习之K近邻算法(KNN)

    机器学习之K近邻算法(KNN) 标签: python 算法 KNN 机械学习 苛求真理的欲望让我想要了解算法的本质,于是我开始了机械学习的算法之旅 from numpy import * import ...

  6. Android中的Glide加载图片

    注意:在Android Studio的项目的build.gradle中添加: compile 'com.github.bumptech.glide:glide:3.6.1' 然后同步一下 目录: 使用 ...

  7. iOS推送,看这篇足够了

    一.注册推送: - (void)registerNotification { if ([[[UIDevice currentDevice] systemVersion] floatValue] > ...

  8. python批量启动多线程

    还未了解多线程的请查看博文 python3多线程趣味详解 python3多线程趣味详解 只是介绍了 python 多线程的使用,对于批量启动线程来说有些不适用,于是出现如下方法: 建立一个线程池,并将 ...

  9. 2.5 time 模块

  10. kSum问题总结

    1.2Sum 题目: 方法一:两次迭代 public class TwoSum { public static int[] twoSum(int[] nums, int target) { int[] ...