laravel5.8筆記八:數據庫(單庫和多庫)

數據庫配置:根目錄下/.env,php

 

單個數據庫mysql

.env配置redis

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.phpsql

'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配置socket

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.phpspa

'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'),
            ]),
        ],

 

控制器取數據unix

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多庫)配置方法同樣code

相關文章
相關標籤/搜索