简体   繁体   English

映射多租户数据库

[英]Mapping Multi Tenant Database

Well we are developing a Multi Tenant Application with Separate Databases. 好吧,我们正在开发带有单独数据库的多租户应用程序。 All the schema have the same structure for the N separate databases. 对于N个独立的数据库,所有模式都具有相同的结构。 We are using MySql with aspx to build a web application. 我们使用带有aspx的MySql来构建Web应用程序。 Now there will be a situation when there may be same users who are on N separate databases. 现在将出现一种情况,即可能有相同的用户位于N个独立的数据库中。 We need a approach to link up those databases where the user have his/her datas and show it on a dropdownlist. 我们需要一种方法来链接用户拥有其数据的那些数据库,并将其显示在下拉列表中。 After linking the database when the user login he/she will able to see all the available database in the dropdownlist where his/her data reside. 在用户登录时链接数据库后,他/她将能够在其数据所在的下拉列表中查看所有可用的数据库。 When the user change the dropdownlist values he/she should be able to change the database instantly and view his/her datas form the linked databases. 当用户更改下拉列表值时,他/她应该能够立即更改数据库并从链接的数据库查看他/她的数据。 We have built the Multi Tenant Application and every thing is going fine. 我们已经构建了多租户应用程序,并且一切正常。 The problem is that how to map all those database from a master database for the particular user. 问题是如何从特定用户的主数据库映射所有这些数据库。 Every user has a separate Unique ID in the separate database. 每个用户在单独的数据库中都有单独的唯一ID。 So user don't have anything common in the Multi Tenant Database other than the data structure. 因此,除了数据结构之外,用户在Multi Tenant Database中没有其他共同之处。 We tried storing all users Unique ID in the master database and linking it with the other Unique ID where the user reside but the solution is not giving fruitful results. 我们尝试将所有用户的唯一ID存储在主数据库中,并将其与用户所在的其他唯一ID关联,但解决方案未获得丰硕的结果。 Any other alternative is most welcome. 任何其他替代方案都非常受欢迎。

The method I went with for multi-tenant mysql is: 我用于多租户mysql的方法是:

  • main database with all of the data all tenants, but every table has a tenantID column which separates out the data by tenant 主数据库包含所有数据的所有租户,但是每个表都有一个tenantID列,该列按租户将数据分隔开
  • create multiple databases for each tenant, but that database is filled with mysql views that reference back to the main database. 为每个租户创建多个数据库,但是该数据库充满了引用回主数据库的mysql视图。 For example: 例如:

.

 # ie. main database has tables: users, and invoices
 # if you have a new tenant named "acme", then:
 create database acme;
 create view acme.users as select * from maindb.users where entityID=2;
 create view acme.invoices as select * from maindb.invoices where entityID=2;

In my situation, this allowed me not have to change any of my previous queries (which were built before I knew I was going to use multi-tenant). 在我的情况下,这使我不必更改以前的任何查询(这些查询是在我知道要使用多租户之前构建的)。 All I had to do was select the proper database, and all the data was automatically separate. 我要做的就是选择合适的数据库,所有数据都会自动分离。

But keeping the actual data all in the same database, this simplified maintenance. 但是将实际数据全部保存在同一数据库中,简化了维护。

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

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