数据库连接
通过配置name、user、pass即可实现常规数据库连接,详见数据库配置
'DATABASE' => [
'name' => 'boa',
'user' => 'test',
'pass' => '12345678'
]
数据库类型
mysqli仅支持mysql数据库,pdo可以支持多种数据库,可以通过type来配置,默认为mysql
type | 数据库 | DSN模板 |
---|---|---|
mysql | MySQL | (port > 0)mysql:host={host};port={port};dbname={name} (port = 0)mysql:unix_socket={host};dbname={name} |
sqlsrv | SQL Server/Azure | (port > 0)sqlsrv:Server={host},{port};Database={name} (port = 0)sqlsrv:Server={host};Database={name} |
oci | Oracle | (port > 0)oci:dbname=//{host}:{port}/{name} (port = 0)oci:dbname={name} |
ibm | IBM DB2 | (port > 0)ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE={name};HOSTNAME={host};PORT={port};PROTOCOL=TCPIP (port = 0)ibm:DSN={name} |
sqlite | SQLite | (port > 0)sqlite:{name} (port = 0)sqlite::memory: |
sqlite2 | SQLite2 | (port > 0)sqlite2:{name} (port = 0)sqlite2::memory: |
access | Access | (port > 0)odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq={name} (port = 0)odbc:{name} |
excel | Excel | (port > 0)odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dbq={name};Readonly=0 (port = 0)odbc:{name} |
firebird | Firebird | (port > 0)firebird:dbname={host}/{port}:{name} (port = 0)firebird:dbname={name} |
pgsql | PostgreSQL | pgsql:host={host};port={port};dbname={name} |
cubrid | CUBRID | cubrid:host={host};port={port};dbname={name} |
mssql | SQL Server | mssql:host={host};dbname={name} |
sybase | Sybase | sybase:host={host};dbname={name} |
dblib | DbLib | dblib:host={host};dbname={name} |
informix | Informix | informix:DSN={name} |
连接方式
默认为tcp连接,当port=0时使用socket、pipe等方式连接,详见上表DSN模板
持久连接
当persist=true时使用持久化连接,默认false
驱动选项
可以通过option为每个驱动设置属性,具体参见PHP官方文档PDO::setAttribute()和mysqli::options()
读写分离
通过配置master和slave开启读写分离模式,无须改动SQL或程序。支持一组或多组(子数组)配置信息,每组配置信息省略的字段由外层对应字段替代
'DATABASE' => [
'name' => 'boa',
'user' => 'test',
'pass' => '12345678',
'slave' => [
['host' => '192.168.0.2'],
['host' => '192.168.0.3'],
['host' => '192.168.0.4']
],
'master' => [
'host' => '192.168.0.1'
]
]