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' => '',
        ],
    ],
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值