跟踪MySQL更改历史记录的简单方法?

问题描述 投票:0回答:2

我想在我的MySQL数据库中记录所有更改(插入/更新/删除)。

可能我还希望该记录易于执行,即,如果我选择执行所有记录的语句,则可以将其用作备份工具。

我的理解是,MySQL的日志格式可能包含不必要的内容,并且我需要进行额外的处理才能使语句可批量执行。

正在创建一个简单的文本文件并向其中写入SQL语句吗?还是MySQL中已经有一些本机功能支持此功能?还是我可以利用Redis等其他软件来创建更有效的解决方案的方法?

mysql sql logging redis backup
2个回答
1
投票

“我的理解是,MySQL的日志格式可能包含不必要的内容,并且我需要进行额外的处理才能使语句可批量执行。”

当您谈论MySQL General Log时,这很有意义。

使用MySQL Bin Log,您无需转换日志格式。

首先签出binlog_format

有3种binlog_formathttp://dev.mysql.com/doc/refman/5.5/en/binary-log-setting.html

使用基于STATEMENT的二进制日志,您可以获取INSERT / DELETE / UPDATE / etc语句,用于

mysql> SHOW VARIABLES LIKE 'binlog_format';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)

使用mysqlbinlog获取UPDATE语句

使用mysqlbinlog和mysql二进制日志文件,您可以按以下方式获取更新语句:

$ mysqlbinlog mysql-bin.000002
# at 2112
#131223 19:48:48 server id 1  end_log_pos 2233  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1387795728/*!*/;
DELETE ... /* generated by server */
/*!*/;
# at 2233
#131223 19:48:48 server id 1  end_log_pos 2352  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1387795728/*!*/;
INSERT .... /j* generated by server */
/*!*/;
# at 2352
#131223 19:48:48 server id 1  end_log_pos 2468  Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1387795728/*!*/;
DROP  /* generated by server */

myqlbinlog的选项

这里有一些有关如何获取特定日期(或头寸)日志的选项

  --start-datetime=name
                      Start reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  -j, --start-position=#
                      Start reading the binlog at position N. Applies to the
                      first binlog passed on the command line.
  --stop-datetime=name
                      Stop reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  --stop-position=#   Stop reading the binlog at position N. Applies to the
                      last binlog passed on the command line.

0
投票

您可以尝试使用maxwell守护程序(https://github.com/zendesk/maxwell),它可以有效地查看binlog并以JSON格式生成更改数据。我使用它来处理更新日志已经很长时间了。我强烈建议这是一个非常稳定的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.