本節涉及以下目錄或文件:
> * config/database.php
> * .env
> * .gitignore
[TOC]
## 配置文件
在 `config/database.php` 文件中,可以定義所有的數據庫連接,并指定默認使用哪個連接。
默認文件內容如下:
~~~php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
| 默認使用的連接
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
| 數據庫連接
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
// SQLite 配置
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
// MySQL 配置
'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' => '',
'strict' => true,
'engine' => null,
],
// Postgres 配置
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
// SQL Server 配置
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
];
~~~
## 讀寫分離
配置一個數據庫連接只用于 SELECT ,另一個用于 INSERT、UPDATE 和 DELETE ,來實現讀寫分離。
例子:
~~~php
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'sticky' => true,
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
~~~
在配置數組中新增兩個鍵:`read` 和 `write`,這兩個鍵對應值均為一個數組,數組內包含單個鍵“host”,其所映射的 IP 值分別就是讀連接和寫連接,讀/寫連接的其它數據庫配置項都共用 mysql 的主數組配置。
如果我們想要覆蓋主數組中的配置,只需要將相應配置項放到 `read` 和 `write` 數組中即可。在本例中,`192.168.1.1` 將被用作「讀」連接,而 `192.168.1.2` 將被用作「寫」連接。兩個數據庫連接的憑證(用戶名/密碼)、前綴、字符集以及其它配置將會共享 `mysql` 數組中的設置,同理,如果不一樣的話,分別在 `read` 或 `write` 數組中單獨配置即可。
### 配置多個「讀」連接,一個「寫」連接
可以這樣寫:
~~~php
'mysql' => [
'driver' => 'mysql',
'read' => [
'host' => ['193.168.1.1', '194.168.1.1']
],
'write' => [
'host' => '196.168.1.2'
],
],
~~~
Laravel 在“讀”數據時,會從提供的 IP 中**隨機**選一個進行連接。
>[info] 實現原理可以查看 `Illuminate\Database\Connectors\ConnectionFactory` 底層源碼。
>[danger] 注:目前讀寫分離僅支持單個「寫」連接。
### `sticky` 選項
`sticky` 是一個可選的選項,具體作用是:若在當前的請求周期內,數據庫曾經被寫入過一些數據,`sticky` 選項會立即將這些數據讀出來。
如果 `sticky` 選項是 `true`,而且在當前的請求周期內對數據庫執行過 “寫入” 操作,那么任何“讀取”的操作都會使用「寫」連接。這樣就可以確保同一個請求生命周期內寫入的數據都可以立即被讀取到,從而避免主從延遲導致的數據不一致,是否啟用這一功能取決于你的需求。
當然,這只是一個針對分布式數據庫系統中主從數據同步延遲的一個非常初級的解決方案,訪問量不高的中小網站可以這么做,大流量高并發網站肯定不能這么干,主從讀寫分離本來就是為了解決單點性能問題,這樣其實是把問題又引回去了,造成所有讀寫都集中到寫數據庫,對于高并發頻繁寫的場景下,后果可能是不堪設想的,但是話說回來,對于并發量不那么高,寫操作不那么頻繁的中小型站點來說,`sticky` 這種方式不失為一個初級的解決方案。
## `env` 讀取環境變量
在 `config/database.php` 文件中,使用了輔助函數 `evn()` 來獲取環境變量,這些環境變量在項目根目錄下的 `.env` 文件中定義。
`.env` 文件用以存儲一些依賴環境的變量,比如數據庫配置。因為它不會被加入到版本庫中, 根目錄下的 `.gitignore` 文件已經將其排除在外。所以還可以用來配置一些敏感信息:比如正式環境的一些第三方應用賬號,token 等。
傳遞給 `env` 函數的第二個值是「默認值」。如果給定的環境變量不存在,則會使用該值。
- 日常命令
- 第一章 開發環境部署
- 第一節 安裝 VirtualBox
- 第二節 安裝 Vagrant
- 第三節 安裝 Git for Windows
- 命令行提示符的寫作約定
- Git Bash 使用技巧
- 第四節 安裝和配置 Homestead
- 第五節 啟動 Homestead 虛擬機
- 第六節 創建 Laravel 應用
- 第七節 使用 Git 進行版本控制
- 總結 本章命令概覽
- 第二章 前端工作流
- 第一節 Yarn 安裝前端擴展包
- 第二節 Laravel Mix 前端資源編譯
- (1)配置 webpack.mix.js 指定 Mix 任務
- (2)修改 resources/assets 前端資源文件
- (3)運行 Mix 任務編譯和監控代碼
- 總結 本章命令概覽
- 第三章 數據庫
- 第一節 Config 數據庫配置信息
- 第二節 Migrations 數據庫遷移文件
- 第三節 Schema 數據庫結構生成器
- (1) 數據表操作
- (2) 字段操作
- (3) 索引操作
- 第四節 Seeding 數據填充
- 第五節 DB 數據庫查詢構建器
- (1)獲取結果集