简体   繁体   English

需要多租户数据库体系结构的建议吗?

[英]Need suggestion for Multi-tenant database architecture?

We are developing project using PHP & MySQL in SaaS model, each tenant has the option to create modules for their own, using our Module Creator and Form Builder. 我们正在使用SaaS模型中的PHP和MySQL开发项目,每个租户都可以选择使用我们的模块创建器和表单生成器创建自己的模块。

When they are creating new module dynamically we are creating new table for particular tenant. 当他们动态创建新模块时,我们正在为特定租户创建新表。 So each tenant has different schema. 因此,每个租户都有不同的架构。

I suggested to go with each database of each tenant but my manager wants to keep all the tenant into single DB since we have continues upgrade on our project. 我建议使用每个租户的每个数据库,但是我的经理希望将所有租户保留在单个数据库中,因为我们将继续对项目进行升级。

Now my idea is to have 2 DB for each tenant; 现在我的想法是每个租户有2个DB; one is Master DB its common for all tenant and the next one is secondary DB, it is for each individual's table creation. 一个是主数据库,它对所有租户都通用,下一个是辅助数据库,它用于每个人的表创建。

There are various ways in which a Multi Tenant app can be designed 多租户应用程序可以通过多种方式进行设计

1) Single database, single code-base 1)单一数据库,单一代码库

Use a single database and store all client data in a single database. 使用单个数据库,并将所有客户端数据存储在单个数据库中。 You will have to add a field like “clientid” in all your tables and modify all your SQL queries to select the correct “clientid”. 您将必须在所有表中添加一个字段,例如“ clientid”,并修改所有SQL查询以选择正确的“ clientid”。 If you already have a single tenant app, then heavy modification is required in the code. 如果您已经有一个租户应用程序,则需要对代码进行大量修改。

Advantages: Easy to manage database & updates; 优点:易于管理数据库和更新; no duplication. 没有重复。 Disadvantages: If you miss out a “clientid” in the SQL query, it may result in issues with another client. 缺点:如果您错过了SQL查询中的“ clientid”,则可能导致另一个客户端出现问题。

2)Multiple database, single code-base 2)多个数据库,单个代码库

Create a separate database for every client. 为每个客户端创建一个单独的数据库。 No modification is required to your existing single-tenant codebase. 无需修改您现有的单租户代码库。 You will only have to modify the configuration file to select a database depending on the client. 您只需要修改配置文件即可根据客户端选择数据库。

Advantages: Very little codebase modification, easy to ensure client data does not get mixed. 优点:很少修改代码库,易于确保不会混淆客户端数据。 Disadvantages: Multiple databases to manage so in the event of DB schema updates, you will have to update each and every database. 缺点:需要管理多个数据库,因此,在更新数据库架构时,您将必须更新每个数据库。

3)Multiple database, multiple code-base 3)多个数据库,多个代码库

Create a separate database & duplicate the complete code for every client. 创建一个单独的数据库,并为每个客户端复制完整的代码。 No modification required at all. 完全不需要修改。

Advantages: No codebase modification, easy to ensure client data does not get mixed, easy to perform client specific modifications. 优点:无需修改代码库,易于确保客户端数据不会混合,易于执行客户端特定的修改。 Disadvantages: Multiple databases/code to manage so in the event of DB schema/code updates, you will have to manually update for each client. 缺点:要管理多个数据库/代码,因此,如果数据库模式/代码更新,则必须为每个客户端手动更新。

So now you decide which options suits best for your project requirements. 因此,现在您可以确定最适合您的项目要求的选项。

I would seperate the data of your tenants in seperate DBs. 我会将您的租户的数据分离到单独的数据库中。 This makes it more difficult implementing bugs so that one tenant can accidently access the data of another. 这使得实施错误更加困难,从而一个租户可以意外访问另一个租户的数据。 As far as I know there are data privacy acts in at least one country (Germany) that requires seperation of database schemas in multi tenancy systems. 据我所知,至少一个国家(德国)存在数据隐私行为,需要在多租户系统中分离数据库架构。

For Migrating the database I would suggest a model like flywire (ok, thats not PHP, but the concept is quite easy and can reimplemented in PHP without big effort) 对于迁移数据库,我建议使用像flywire这样的模型(好吧,那不是PHP,但是这个概念很简单,可以不费吹灰之力就可以在PHP中重新实现)

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

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