您现在的位置是:网站首页>软件开发>开发终端>yii2yii2
yii2实现多个不同数据库连接
风口下的猪2023-03-15【yii2】
简介
yii2的多数据库连接,主要是两个部分main-local连接配置和模型申明哪个数据库连接
1.在common/config/main-local中设置多个db
return [
'components' => [
//线上
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=rm-wz9d7t51392x5543n7o.mysql.rds.aliyuncs.com;dbname=equity',
'username' => 'equity',
'password' => 'lI4oZ8mO0w',
'charset' => 'utf8',
'tablePrefix' => 'jdc_'
],
//贵扁鹊业绩数据源
'db_gbq' => [
'class' => 'yii\db\Connection',
'dsn' => 'sqlsrv:server=47.109.42.163,1433;database=测试',
'username' => 'gzh',
'password' => 'ASDqwe123',
'charset' => 'utf8'
],
不同的数据库连接写法遵循以下
MySQL, MariaDB: mysql:host=localhost;dbname=mydatabase
SQLite: sqlite:/path/to/database/file
PostgreSQL: pgsql:host=localhost;port=5432;dbname=mydatabase
CUBRID: cubrid:dbname=demodb;host=localhost;port=33000
MS SQL Server (via sqlsrv driver): sqlsrv:Server=localhost;Database=mydatabase
MS SQL Server (via dblib driver): dblib:host=localhost;dbname=mydatabase
MS SQL Server (via mssql driver): mssql:host=localhost;dbname=mydatabase
Oracle: oci:dbname=//localhost:1521/mydatabase
2.在要使用的模型中使用getDb()申明连接的数据库
public static function getDb(){
return Yii::$app->get('db_gbq');
}
很赞哦! (0)
/ponder/index.php/index/catelist/catelist/cateid/10.html
相关阅读 (同一栏目)
- yii控制器及路由
- Class yii\filters\VerbFilter ----操作过滤器
- Class yii\filters\Cors ----CORS 跨域资源共享控制筛选器
- Class yii\filters\auth\CompositeAuth
- Class yii\filters\auth\HttpBearerAuth
- yii2请求获取参数
- yii2目录结构说明
- yii2 系统定义的路径别名
- yii2模型中attributeLabels()的作用
- yii2模型中定义对应数据库和验证规则
- yii2模型中块赋值$model -> attributes = $data;
- 理解yii2模型中findOne()、findAll()
- yii2模型增加数据
- yii2模型修改数据
- yii2模型删除数据
- yii2实现多个不同数据库连接
- yii2连接sqlsever的一些注意点 (windows解法)
- yii2和tp链式查询时where的区别
- yii2 关联查询总结
栏目目录
标签云
站点信息
- 文章统计:528篇
- 移动端访问:扫码进入SQ3R
