多租户多数据库设置 [英] Multi Tenant Multiple Database Setup

查看:103
本文介绍了多租户多数据库设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Yii2建立一个多租户网站.我们希望允许每个用户在使用同一系统时拥有自己的数据库.我们还将有一个系统数据库,其中包含用户信息和发票等信息.

We're setting up a multi tenant website using Yii2. We want to allow each user to have it's own database while using the same system. We'll also have a system database that holds the users information and information like invoicing.

这是我们的一些问题.

  1. 如何根据存储在第一个数据库连接(db)中的信息使第二个数据库连接(db2)动态化?
  2. 我们如何设置要在所有租户数据库(db2)动态时应用的迁移?
  3. 在初始迁移时,我们将如何定位一个动态数据库?


有些链接有所帮助,但并未回答我们所有的问题.


There are a few links that have helped but haven't answered all our questions.

多个数据库连接和Yii 2.0

http://www.yiiframework.com/doc-2.0/guide-db-migrations.html

推荐答案

1)这完全取决于您的设置方式.您将如何记住哪些用户必须使用什么数据库?您总是可以做类似的事情

1) It all depends on how you set it up. How will you remember what user has to use what DB? You can always do something like

    'components' => [
        'db1' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=db1name', //maybe other dbms such as psql,...
            'username' => 'db1username',
            'password' => 'db1password',
        ],
        'db2' => function() use ($whatever, $variables, $you, $need) {
            return [
               'class' => 'yii\db\Connection',
               'dsn' => 'mysql:host=localhost;dbname=' . GETTHEDATABASEINAWAY, 
               'username' => GETTHEUSERINAWAY,
               'password' => GETTHEPASSINAWAY,
            ],
        }
    ],

];

有关匿名函数的更多信息 http://php.net/manual/en/functions. Anonymous.php

More on anonymous functions http://php.net/manual/en/functions.anonymous.php

2)您可能在某个地方有一个数据库列表.创建您自己的可扩展主控制器的迁移控制器,您可以调用它来代替常规的yii 2控制器进行迁移. 在控制台中,您可以创建对Yii2进行扩展的migrationController.php,而不是调用php yii migrate/up,而是调用php yii migration/up.

2) You probably have a list of databases someplace. Create your own migrate controller that extends the main one and you can call it to migrate instead of the the normal yii 2 controller. in console you can create migrationController.php that extends the the Yii2 one, instead of calling php yii migrate/up you will call php yii migration/up.

您还可以欺骗系统使用您自己的MigrateControllor,以便仍然可以使用php yii migrate/up,尝试使用URL规则执行此操作.我看到一个人以另一种方式这样做,但是我找不到位置.

You can also trick the system to use your own MigrateControllor so you can still use php yii migrate/up , try using the URL rules to do this. I saw a guy doing it another way but I cannot find the location.

3)如果创建自己的迁移控制器(请参见编号2),则只需将参数添加到迁移命令即可.现在,它可以接收--migration-path--db之类的参数,并添加您自己的参数以执行所需的操作.

3) if you create your own migration controller (see number 2) then just add parameters to the migration command. Right now it can receive parameters like --migration-path and --db , add your own parameter to do what you need.

这篇关于多租户多数据库设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆