简体   繁体   English

一个用于多个客户端的大型MySQL数据库

[英]One large MySQL database for several clients

NB 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`? 使用一个大的MySQL数据库为多个客户端(相同的结构,等等),与user列到客户分开,是否有可能限制,以匹配用户表的访问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. 我对Microsoft的SQL Server不太熟悉,但是我读到一些有关“多租户数据体系结构”的东西,似乎提供了这一点。

Does MySQL have something similar? MySQL有类似的东西吗?

I would recommend against having 1 large DB for all your clients for multiple reasons: 由于多种原因,我建议不要为所有客户使用1个大型数据库:

  1. I've worked at a startup where the AWS instance for one of our DB just gave away (for no fault of ours). 我曾在一家初创公司工作过,该公司的一个数据库的AWS实例刚刚泄露了(这对我们没有错)。 Let's just say that was a hectic day for everyone in the office. 让我们说这对办公室里的每个人来说都是忙碌的一天。 If this happens to your ONLY DB, how are you going to ensure customer satisfaction? 如果您的唯一数据库发生这种情况,您将如何确保客户满意? How are you going to maintain a robust system? 您将如何维护一个强大的系统? There's a saying that at google, a server goes does every 2 seconds. 俗话说,在Google,服务器每隔2秒就会运行一次。 While that may not happen when you're starting off, know that you're not immune from your servers failing. 当您开始时可能不会发生这种情况,但请注意,您无法避免服务器故障。 Sharding your DB into multiple instances is a good way of hedging against a major failure. 将数据库分成多个实例是避免重大故障的一种好方法。
  2. If you have many transactions, your thread pool is always going to get locked up. 如果您有很多事务,那么线程池总是会被锁定。 You don't want this because it's going to increase the amount of time it takes to perform CRUD operations on your database, which in turn is going to effect customer satisfaction. 您不希望这样做,因为这将增加在数据库上执行CRUD操作所需的时间,从而影响客户满意度。

A better mult-tenant archticture you want to have is: 您想要拥有一个更好的多租户架构:

  1. Have 1 master DB for every client (maybe every 2 clients at the beginning), then have slaves for every master DB you have. 每个客户端都有一个主数据库(开始时可能是两个客户端),然后每个客户端都有一个从数据库。 Now you can perform all your writes on the slaves and perform all your reads off of the master. 现在,您可以在从属设备上执行所有写操作,并在主设备上执行所有读操作。 As long as you keep your DB's in sync, this is the best way to ensure your thread pool maintains availability and quickly performs all the transactions. 只要您使数据库保持同步,这就是确保线程池保持可用性并快速执行所有事务的最佳方法。

Here's a great article titled " High Availability, Load Balancing, and Replication ," which will give you a great primer into building a robust multi-tenant system. 这是一篇很棒的文章,标题为“ 高可用性,负载平衡和复制 ”,它将为您构建强大的多租户系统奠定基础。 I would also recommend reading the articles on High Scalability 我还建议阅读有关高可伸缩性的文章

Please let me know if you have any questions! 请让我知道,如果你有任何问题!

MySQL has table-level and column-level privileges, but not row-level privileges. MySQL具有表级和列级特权,但没有行级特权。

The closest thing would be to define a VIEW through which a user accesses the table. 最接近的事情是定义一个VIEW,用户可以通过该VIEW访问表。 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 |
+----------------+------+

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

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