简体   繁体   English

获取所有数据库更改

[英]Getting all database changes

I'm developing an application in rails 3.2 with mongodb as database. 我正在用mongodb作为数据库在Rails 3.2中开发一个应用程序。 I am using the mongoid gem. 我正在使用蒙古型宝石。

I want to track all the changes to the database during runtime, but I have no idea how to do it. 我想在运行时跟踪对数据库的所有更改,但是我不知道该怎么做。
Any clues on this? 有什么线索吗?

There are several approaches you could use to track changes to your data, depending on whether you are just interested in monitoring current activity or need to create some sort of transactional history. 可以使用多种方法来跟踪数据的更改,具体取决于您是仅对监视当前活动感兴趣还是需要创建某种交易历史记录。

In rough order of least-to-most effort: 以最少到最大的努力:

1) Enable the MongoDB Query Profiler 1) 启用MongoDB查询分析器

If you enable the query profiler at a level of " 2 " it will collect profiling data for all operations (reads as well as writes) to a specific database. 如果以“ 2 ”级别启用查询分析器,它将为特定数据库的所有操作(读取和写入)收集分析数据。 You can also enable this in your configuration options ) to change the profiling default for all databases on mongod startup. 您还可以在配置选项中启用此功能 ),以更改mongod启动时所有数据库的分析默认设置。 The profile data is saved in a capped collection per database and will only contain a recent snapshot of queries. 概要文件数据保存在每个数据库的上限集合中,并且仅包含最近的查询快照。 Query profiling status can also be changed at runtime, so you can easily enable or disable as required. 查询概要分析状态也可以在运行时更改,因此您可以根据需要轻松地启用或禁用。

2) Add Mongoid callbacks to your application 2) 将Mongoid回调添加到您的应用程序

Add appropriate logic Mongoid callbacks such as after_insert , after_save , after_upsert depending on what information you are trying to capture. 根据您要捕获的信息,添加适当的逻辑Mongoid回调,例如after_insertafter_saveafter_upsert

3) Create a tailable cursor on the MongoDB oplog 3) 在MongoDB oplog上创建一个可尾光标

If you run MongoDB as part of a replica set (or with the --replSet option), it creates a capped collection called the oplog (operations log) . 如果您将MongoDB作为副本集的一部分(或使用--replSet选项)运行,它将创建一个名为oplog (操作日志)--replSet集合。 You can use a tailable cursor to follow changes as they are committed to the oplog . 您可以使用tailable光标跟随变化,因为它们致力于oplog The oplog details all changes to the database, and is the mechanism MongoDB uses for replication. 该操作oplog详细说明了对数据库的所有更改,并且是MongoDB用于复制的机制。

I am not familiar with Mongodb nor mongoid , but here is my idea for someone using MySQL (I hope it gives you some clue): 我对Mongodbmongoid都不熟悉,但这是我对使用MySQL的人的想法(希望它能给您一些线索):

First you take a snapshot of your database (using a tool like mysqldump ). 首先,为数据库拍摄快照(使用mysqldump类的工具)。

Then at certain intervals, you check (audit) for those records which have an updated_at value greater (later) than the time you took the snapshot, or your last audit time. 然后,以一定间隔检查(审核)那些updated_at值大于(大于)快照时间或上次审核时间的记录。

Now, you have two versions of your data, which you can check for changes. 现在,您有两个版本的数据,可以检查它们的更改。

As I said, this is just an idea, and it needs to be more refined. 正如我所说的,这只是一个想法,需要进一步完善。 I hope it gives you a clue. 我希望它能给您一个线索。

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

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