laravel5.8笔记八:数据库(单库和多库)
数据库配置:根目录下/.env,
单个数据库
.env配置
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=shop DB_USERNAME=root DB_PASSWORD=root DB_PREFIX=ds_
config/database.php
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => env('DB_PREFIX', ''), 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]),
控制器取数据
public function index(){ $res = DB::table('goods')->get(); print_r($res); }
多个数据库
.env配置
DB_WRITE_CONNECTION=read DB_WRITE_HOST=192.168.0.132 DB_WRITE_PORT=3306 DB_WRITE_DATABASE=shop DB_WRITE_USERNAME=root DB_WRITE_PASSWORD=root DB_WRITE_PREFIX=st_ DB_READ_CONNECTION=read DB_READ_HOST=192.168.0.134 DB_READ_PORT=3306 DB_READ_DATABASE=shop DB_READ_USERNAME=root DB_READ_PASSWORD=root DB_READ_PREFIX=st_
config/database.php
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => env('DB_PREFIX', ''), 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]), ], 'read' => [ 'driver' => 'mysql', 'host' => env('DB_READ_HOST', '127.0.0.1'), 'port' => env('DB_READ_PORT', '3306'), 'database' => env('DB_READ_DATABASE', 'forge'), 'username' => env('DB_READ_USERNAME', 'forge'), 'password' => env('DB_READ_PASSWORD', ''), 'unix_socket' => env('DB_READ_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => env('DB_PREFIX', ''), 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]), ], 'write' => [ 'driver' => 'mysql', 'host' => env('DB_WRITE_HOST', '127.0.0.1'), 'port' => env('DB_WRITE_PORT', '3306'), 'database' => env('DB_WRITE_DATABASE', 'forge'), 'username' => env('DB_WRITE_USERNAME', 'forge'), 'password' => env('DB_WRITE_PASSWORD', ''), 'unix_socket' => env('DB_WRITE_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => env('DB_PREFIX', ''), 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]), ],
控制器取数据
public function index(){ // 默认数据库 (mysql) $res = DB::table('goods')->get(); // 从read取数据 $read = DB::connection('read')->table('goods')->get(); // 从write取数据 $write = DB::connection('write')->table('goods')->get(); print_r($res); }
同理,缓存(redis、memcache多库)配置方法一样