Mysql – How to isolate client data on the same application using MySQL

multi-tenantMySQLschema

I have made an application which has a companies table at the moment. It would work fine for a few clients (~100 companies, according to my scaling calculations). The application is a simple CRUD app accessed on the web using REST protocols. Very few (if none) concurrent transactions take place at the same time.

At the moment the companies table is the most independent table of all. All others like employees, benefits, time_off, etc. are related to each other via the companies table.

Someone suggested that I could use MySql's schemas to isolate one company from another, and clients would see the same exact app. Essentially, the moment I sign up a new company, a new schema would fire up and create the same tables as it has for any other company. How does this work? Could I have multiple schemas on the same database? I'm working with AWS RDS so I don't wish to pay for multiple databases here as the total size of the data even after half a year's use has not exceeded half a GB.

Best Answer

In MySQL schema and database are synonymous: you use CREATE DATABASE or CREATE SCHEMA with equal effect. There's little physical isolation between schemas (databases), workload against one schema can potentially affect all schemas on the same server. However, there is logical access isolation: you can allow certain user(s) access to a specific schema only using GRANT ... ON schema.* TO ....