简体   繁体   English

将MySQL事件日志存储在MongoDB中是否很好?

[英]Is it good to store mysql event logs in MongoDB?

Earlier in our database design, we use to create mandate fields for each of the table and few important fields were: 在数据库设计的早期,我们用于为每个表创建任务字段,而几个重要字段是:

created_by
created_time
created_by_ip
updated_by
updated_time
updated_by_ip

Now, its an era of no-schema design. 现在,它是一个无模式设计的时代。 We prefer mongodb or some other just writing databases. 我们更喜欢mongodb或其他一些仅编写数据库的工具。

My question here is: 我的问题是:

  1. Is it a good practise to maintain logs in a separate database? 在单独的数据库中维护日志是一种好习惯吗?

  2. Do we need to create separate log table for each mysql tables considering mongodb or is it okay to have single mongodb audit table for all mysql tables? 考虑到mongodb,我们是否需要为每个mysql表创建单独的日志表,还是可以为所有mysql表使用单个mongodb审计表?

  3. What things need to be considered in querying the results from mongodb? 从mongodb查询结果时需要考虑什么?

  4. What should be the structure for mongodb table structure? mongodb表结构应该是什么结构?

  5. Any other alternatives to store logs? 还有其他替代方法来存储日志吗?

Considering situation where if we want to delete registered user if not authenticated in specified time(max of 48hrs). 考虑在指定的时间(最长48小时)内未通过身份验证的情况下要删除注册用户的情况。

If all the time logs are handled in mongodb. 如果所有时间日志都在mongodb中处理。 How can we query the same from mysql? 我们如何从mysql查询相同的内容?

You usually want this (audit?) data next to the real data and definitely not in a different DB engine as the number of partial errors to support becomes quite a nightmare (eg someone registered, but you fail to insert audit data - is this ok? should the account become orphan? What happens if the app goes down half way?). 通常,您希望此(审核?)数据位于真实数据旁边,并且绝对不要在其他数据库引擎中使用,因为要支持的部分错误数量已成为噩梦(例如,有人注册,但您无法插入审核数据-可以吗? (该帐户应成为孤立帐户吗?如果该应用程序中途关闭了怎么办?)。

Systems that have this separation usually use messaging and 2 different listeners are responsible for storing the data and storing the audit (eg one in a relational DB and the other in an event store). 具有这种分离的系统通常使用消息传递,并且2个不同的侦听器负责存储数据和存储审核(例如,一个在关系数据库中,另一个在事件存储中)。 In this way you have a higher chance of achieving eventual consistency. 这样,您就有更大的机会实现最终的一致性。

Edit 编辑

There are a few options around using messaging and the assumption here is that both sources of data must be in sync (or as close as possible). 使用消息传递有几种选择,这里的假设是两个数据源必须同步(或尽可能接近)。 Please bear in mind that I still think that storing data+audit together is by far the simplest and more sensible approach. 请记住,我仍然认为将数据+审计一起存储是迄今为止最简单,更明智的方法。

Using messaging, your app can emit a message on certain events (eg user created). 使用消息传递,您的应用程序可以在某些事件(例如,用户创建的事件)上发出消息。 Then 2 different listeners react to this message. 然后2个不同的侦听器对此消息做出反应。 One listener stores the data in one DB engine; 一个侦听器将数据存储在一个数据库引擎中。 Another listener stores the audit data. 另一个侦听器存储审核数据。 The problem with this approach is that you might need to ensure ordering on the messages, which makes it really slow. 这种方法的问题在于,您可能需要确保对消息进行排序,这确实很慢。

Another (scary) approach is to use distributed (XA) transactions between MySQL and a messaging system (as mongo doesn't support transactions). 另一种(可怕的)方法是在MySQL和消息传递系统之间使用分布式(XA)事务(因为mongo不支持事务)。 Then the data to MySQL and the message would be committed together, and a listener can receive the audit data and store it in mongo. 然后,将MySQL数据和消息一起提交,并且侦听器可以接收审核数据并将其存储在mongo中。

I need to emphasize that the 2 approaches above are horrible and should never be implemented. 我需要强调的是,以上两种方法都是可怕的, 绝不应该执行。

There are more sensible approaches but might require a different tech stack. 有更明智的方法,但可能需要不同的技术堆栈。 For example using an EventSourcing+CQRS you can store the events (with the audit data) and store the final read models without the audit data. 例如,使用EventSourcing + CQRS可以存储事件(带有审核数据)并存储没有审核数据的最终读取模型。

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

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