简体   繁体   English

多租户 - 为每个租户动态创建数据库

[英]Multi tenant - create database dynamically for each tenant

We are doing a multi tenant web application and the architecture is single code base - multiple database. 我们正在做一个多租户Web应用程序,该架构是单一代码库 - 多个数据库。

We have a master database which will store all the tenant and their corresponding user information. 我们有一个主数据库,它将存储所有租户及其相应的用户信息。

Whenever a new tenant register with us, we need to have separate DB for each of them. 每当新租户向我们注册时,我们都需要为每个租户分别设置DB。 There are around 15 tables for each tenant. 每个租户大约有15张桌子。

There are two options visible for us to handle this scenario 我们可以通过两种方式来处理这种情况

  1. Can have a sql file which will be part of the project and whenever a new tenant registers, we can run the script to create new DB and the tables. 可以有一个sql文件,它将成为项目的一部分,每当新租户注册时,我们都可以运行脚本来创建新的数据库和表。 Here I need to maintain all the schema modifications and there are few static table. 在这里,我需要维护所有架构修改,并且几乎没有静态表。 Static table data will get modified as we move on. 随着我们的继续,静态表数据将被修改。 So all these history has to be maintained and we need to run the entire script when a tenant registers. 因此,所有这些历史记录都必须保持,我们需要在租户注册时运行整个脚本。

  2. There can be a empty database with the latest schema. 可以有一个包含最新模式的空数据库。 So when a tenant registers, I can dynamically create script from the db and run it. 因此,当租户注册时,我可以从db动态创建脚本并运行它。 I don't need to maintain the history as the empty db schema will be the latest always. 我不需要维护历史记录,因为空的db模式将始终是最新的。

Please let me know if there are any other ways to do it. 如果有其他方法,请告诉我。

As features grow, we also need to modify the schema(new column, new table) for the existing tenant tables. 随着功能的增长,我们还需要修改现有租户表的模式(新列,新表)。 When considering this scenario, We believe the first option could be the best as we will have the alter queries but in second option, I need to compare the schema which could be tedious. 在考虑这种情况时,我们认为第一个选项可能是最好的,因为我们将有更改查询,但在第二个选项中,我需要比较可能乏味的模式。

Please share your thoughts on how design it. 请分享您对如何设计的想法。

We do something similar. 我们做类似的事情。 We have a shared code base with lots of configuration options. 我们有一个共享的代码库,有很多配置选项。 Each tenant has their own database and storage and we run most of them as a single web application. 每个租户都有自己的数据库和存储,我们将其中大部分作为单个Web应用程序运行。

We have a shared configuration database that manages the tenant information. 我们有一个共享配置数据库来管理租户信息。 We also have a portal that creates Tenants and manages them eg enables certain features, provisions storage accounts, databases etc. Each tenant configuration is a database row in our configuration database. 我们还有一个创建租户并管理它们的门户网站,例如启用某些功能,配置存储帐户,数据库等。每个租户配置都是我们配置数据库中的数据库行。 Then we provision a Database per tenant and generate a tenant user and create a connection string which is stored in the configuration database. 然后,我们为每个租户配置一个数据库并生成一个租户用户并创建一个存储在配置数据库中的连接字符串。

At app startup the application identifies which tenant it is - gets the connection string to use for tenant database from the configuration. 在应用程序启动时,应用程序会识别它所在的租户 - 从配置中获取用于租户数据库的连接字符串。 We are using Entity Framework migrations migrate to the latest version so the first time a client uses their database the schema is created. 我们正在使用Entity Framework迁移迁移到最新版本,因此客户端第一次使用其数据库时会创建架构。 If we update schema the migrations are added and next time client connects to the database the new schema is applied. 如果我们更新模式,则会添加迁移,下次客户端连接到数据库时,将应用新模式。 Works fine for us. 对我们来说很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM