Laravel 讓鏈接不一樣數據庫以及對數據庫進行增刪改查操做:php
參考:http://laravelacademy.org/post/854.htmlhtml
配置讀寫分離mysql
應用的數據庫配置位於 config/database.php
(可是數據庫用戶及密碼等敏感信息位於 .env
文件)。在該文件中你能夠定義全部的數據庫鏈接,並指定哪一個鏈接是默認鏈接。laravel
'mysql' => [ 'driver' => 'mysql', 'read' => [ 'host' => ['39.107.243.232','39.106.201.242',] ], 'write' => [ 'host' => '39.107.243.232' ], 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'test'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', 'aaaa'), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'modes' => [ 'ONLY_FULL_GROUP_BY', 'STRICT_TRANS_TABLES', 'NO_ZERO_IN_DATE', 'NO_ZERO_DATE', 'ERROR_FOR_DIVISION_BY_ZERO', 'NO_ENGINE_SUBSTITUTION', ], ],
Laravel 在讀數據時會從提供的 IP 中隨機選一個進行鏈接。目前Lavavel5.5讀寫分離僅支持單個寫鏈接。sql
1. 原生SQL:數據庫
$id = 2; $users = DB::connection('mysql::read')->select('select * from users where id = ?',[$id]); if( empty($users) ) { print("Record is not exist : id = $id"); return; } $users = json_decode(json_encode($users),true); $user = $users[0]; print($user['name']);
$id = 1; $user['name'] = 'abce21s'; $affected = DB::update('update users set name=? where id=?', [$user['name'],$id]); if(true != $affected) { print("Update data failed! : id = $id"); return; } print('Update data successful!');
$id = 2; $user['name'] = 'aaaa'; $affected = DB::insert('insert into users (id, name) values (?, ?)', [$id, $user['name']]); if(true != $affected) { print("Update data failed! : id = $id"); return; } print('Update data successful!');
能夠強制寫到讀或者寫的數據庫中:json
$id = 1; $user['name'] = 'ab1s'; $affected = DB::connection('mysql::read') ->update('update users set name=? where id=?', [$user['name'],$id]); if(true != $affected) { print("Update data failed! : id = $id"); return; } print('Update data successful!');
$id = 1; $user['name'] = '111'; $affected = DB::connection('mysql::write') ->update('update users set name=? where id=?', [$user['name'],$id]); if(true != $affected) { print("Update data failed! : id = $id"); return; } print('Update data successful!');
2. 查詢構建器:socket
$id = 2; $user['name'] = 'abcde2'; $affected = DB::connection('mysql::read') ->table('users') ->where('id', $id) ->update(['name' => $user['name']]); if(true != $affected) { print("Update data failed! : id = $id"); return; } print('Update data successful!');
$id = 1; $user['name'] = '222'; $affected = DB::connection('mysql::write') ->table('users') ->where('id', $id) ->update(['name' => $user['name']]); if(true != $affected) { print("Update data failed! : id = $id"); return; } print('Update data successful!');
$id = 2; $user['name'] = 'bbb'; $affected = DB::connection('mysql::write') ->table('users') ->insert(['id'=> $id, 'name'=>$user['name']]); if(true != $affected) { print("Update data failed! : id = $id"); return; } print('Update data successful!');
3. Eloquent ORMpost
參考:https://docs.golaravel.com/docs/5.5/eloquent/spa
http://laravelacademy.org/post/8060.html