Yii2数据库完整配置

Yii2.0框架,mysql数据库,一主多从的经典数据库配置。

return [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=1.1.2.3;dbname=db_test',
        'username' => 'u_test',
        'password' => '123456',
        'charset' => 'utf8',
        //表名前缀
        'tablePrefix' => 't_',
        //启用schema缓存,schema缓存用于AR,如果使用asArray(),则不会使用缓存
        'enableSchemaCache' => true,
        //schema缓存有效时间,单位为秒
        'schemaCacheDuration' => 600,
        //用来存储schema信息的缓存组件名称
        'schemaCache' => 'schemaCache',
        'attributes' => [
        	//长连接
            PDO::ATTR_PERSISTENT => true,
        ],
        //从库列表
        'slaves' => [
            ['dsn' => 'mysql:host=1.1.3.1;dbname=db_test'],
            ['dsn' => 'mysql:host=1.1.3.2;dbname=db_test'],
        ],
        //从库配置
        'slaveConfig' => [
            'username' => 'u_test',
            'password' => '123456',
            'charset' => 'utf8',
            'attributes' => [
            	//与数据库的连接超时时间,单位为秒
                PDO::ATTR_TIMEOUT => 10,
                PDO::ATTR_PERSISTENT => true,
            ],
        ],
    ];

cache组件的配置

'components' => [
        'cache' => [
            'class'=>'yii\caching\FileCache',
        ],
        'schemaCache' => [
			'class' => 'yii\redis\Cache',
		],
		'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => 'redis.abibi.com',
            'port' => 6379,
            'database' => 0,
            'password' => '',
        ],
    ],

你可能感兴趣的:(PHP)