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多库)配置方法一样

转载于:https://www.cnblogs.com/wesky/p/10444470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值