Search code examples
mysqlprivilegessql-grantmulti-tenant

One large MySQL database for several clients


N.B. This is not another "One large DB or several small DBs" question.

Using one large MySQL database for several clients (same structure, etc), with a user column to separate the clients, is it possible limit the table access for the user with the matching user column`?

I'm not too familiar with Microsoft's SQL Server, but I read something about "multi-tenant data architecture" that seemed to offer this.

Does MySQL have something similar?


Solution

  • MySQL has table-level and column-level privileges, but not row-level privileges.

    The closest thing would be to define a VIEW through which a user accesses the table. The view has privilege to access to the base table, and each user has privileges to access the view.

    Define the view to restrict access to the current user.

    mysql> create table base (user varchar(16), x int);
    mysql> insert into base values ('root@localhost', 123), ('bill@localhost', 456);
    mysql> create view v as select * from base where user = USER() with check option;
    mysql> select * from v;
    +----------------+------+
    | user           | x    |
    +----------------+------+
    | root@localhost |  123 |
    +----------------+------+