多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
本节涉及以下目录或文件: > * 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` 函数的第二个值是「默认值」。如果给定的环境变量不存在,则会使用该值。