Excel表格网

mysql 数据恢复

224 2024-02-28 21:49 admin   手机版

一、mysql 数据恢复

MySQL 数据恢复:保护和恢复您的重要数据

MySQL 数据恢复:保护和恢复您的重要数据

MySQL 数据库被广泛使用于各个行业和领域,无论是大型企业还是小型创业公司,都依赖于 MySQL 来管理和存储关键数据。然而,在网络攻击、硬件故障或人为错误的情况下,数据丢失的风险不可避免。因此,建立一个可靠的 MySQL 数据恢复策略至关重要。

为什么需要 MySQL 数据恢复?

数据丢失可能会对组织带来巨大的损失。数据库中存储的数据对业务运营至关重要,包括客户信息、财务数据、产品信息等。如果这些数据丢失或损坏,可能导致业务中断、财务损失、客户流失等严重后果。

MySQL 数据恢复可以帮助您将数据库恢复到一个可用的状态,最大程度地减少数据丢失的风险。您可以通过备份和恢复措施来保护您的数据库,以防止意外的数据丢失。

MySQL 数据恢复方法

下面介绍一些常用的 MySQL 数据恢复方法:

1. 正常备份和恢复

定期进行完整备份和增量备份是一种常见的 MySQL 数据恢复方法。这种方法可以将数据库恢复到最近的备份点,并通过应用增量备份来还原遗漏的修改。

然而,并非所有组织都有实时备份的能力,因此定期备份的时间间隔可能会导致一定的数据丢失。

2. 二进制日志恢复

MySQL 的二进制日志包含了所有对数据的修改操作,可以用于数据恢复。您可以使用二进制日志来还原到特定的时间点,以恢复丢失或被错误修改的数据。

这种方法通常需要更高级的数据库管理技能,并且可能需要一些时间来定位和还原特定的数据。

3. 第三方工具

除了 MySQL 自带的恢复功能外,还有一些第三方工具可以帮助您更轻松地进行数据恢复。这些工具通常提供了易于使用的界面,并且具有更高级的数据恢复功能。

例如,OneKey Recovery 是一款功能强大且易于使用的 MySQL 数据恢复工具。它可以自动识别数据库问题,并提供可行的解决方案,快速帮助您将数据库恢复到正常状态。

MySQL 数据恢复的最佳实践

除了选择合适的恢复方法外,下面是一些 MySQL 数据恢复的最佳实践:

1. 定期备份

定期进行完整备份和增量备份是保护数据库的关键。备份频率应根据数据变更的速度和重要性而定。

2. 自动化备份和监控

使用自动化工具进行定期备份和监控可以确保备份过程的可靠性和正确性。这样可以减少人为错误导致的数据丢失。

3. 定期测试恢复

仅仅进行备份是不够的,您还应该定期测试恢复过程。通过定期测试,可以确保备份的可用性,并及时发现备份和恢复过程中的问题。

4. 保护数据库的物理安全

除了网络安全措施外,保护数据库的物理安全也非常重要。防止非授权人员物理访问服务器和存储设备可以防止意外数据丢失。

5. 遵循最佳实践

遵循 MySQL 的最佳实践可以减少数据丢失的风险。这包括定期更新 MySQL 的版本、优化查询和索引等。

结论

MySQL 数据恢复策略对于保护和恢复关键数据至关重要。通过选择合适的恢复方法,并遵循最佳实践,可以最大程度地减少数据丢失的风险,并确保您的数据库在意外情况下能够快速恢复。

无论您是一名数据库管理员,还是一个依赖于 MySQL 的企业所有者,了解和实施 MySQL 数据恢复策略都是非常关键的。

希望本文介绍的 MySQL 数据恢复方法和最佳实践对您有所帮助!

二、mysql数据恢复

MySQL数据恢复: 重现数据的生命力

数据库是现代软件系统中至关重要的组成部分。作为一种可靠的数据库管理系统,MySQL在众多开发人员和企业中广泛使用。但是即使是最可靠的系统也无法排除意外发生的可能性,导致数据丢失或损坏。

mysql数据恢复是一项救援数据库中丢失数据的技术,为用户提供了重现数据生命力的机会。它可以救援被错误删除、损坏、丢失的数据,让系统恢复到它们正常操作的状态。

常见导致数据丢失的原因

MySQL数据库的数据丢失可以由各种原因引起,下面让我们看看一些常见的原因:

  1. 意外删除:当用户在不小心之间删除了重要数据时,可能无法通过简单的恢复方式来获取它们。
  2. 硬件故障:磁盘损坏、电源故障或其他硬件故障有可能导致数据库中的数据损坏甚至完全丢失。
  3. 软件错误:错误的数据库操作、不稳定的系统更新或软件冲突等问题可能导致数据丢失。
  4. 病毒攻击:网络安全问题是当今世界不可忽视的挑战,病毒攻击可能损坏数据库。

MySQL数据恢复技术

针对不同的数据丢失原因,有多种方法可以用来恢复MySQL数据库中的丢失数据。以下是一些常见的mysql数据恢复技术:

1. 逻辑数据恢复

逻辑数据恢复是针对非物理损坏的数据恢复方法。它适用于意外删除、表数据损坏等情况。通过使用备份文件、二进制日志或事务日志,可以还原数据库到丢失数据之前的状态。逻辑数据恢复方法相对简单,但对于大型数据库可能会比较耗时。

2. 物理数据恢复

物理数据恢复是针对磁盘损坏或其他硬件故障导致的数据丢失的恢复方法。这种方法通常需要专业的数据恢复工具和技术。通过对磁盘进行修复、重建或外部数据提取,可以尽可能地从受损的磁盘中恢复数据。

3. 在线数据恢复

在线数据恢复是指在MySQL数据库仍然在线运行的情况下进行数据恢复。这种方法可以减少停机时间,对于需要持续运营的业务非常重要。在线数据恢复通常通过日志文件进行,以保证数据的一致性和完整性。

4. 离线数据恢复

离线数据恢复是指在MySQL数据库停机的情况下进行数据恢复。这种方法通过对数据库文件进行直接操作来恢复数据,适用于无法在线进行数据恢复或需要进行更复杂操作的情况。

MySQL数据恢复的最佳实践

无论您是一位开发人员还是企业用户,以下最佳实践都可以帮助您更好地保护和恢复MySQL数据库中的数据:

  • 定期备份数据:定期备份是预防数据丢失的最重要的措施之一。设置一个合理的备份策略,确保您的备份是最新的,并将其存储在可靠的地方。
  • 使用事务:合理使用数据库事务可以帮助您避免数据不一致和损坏。将相关操作封装在事务中,确保数据在出现错误时可以回滚到一致的状态。
  • 监控数据库:定期监控数据库的性能和健康状况,及时发现潜在的问题并采取措施。使用合适的监控工具确保数据库的稳定运行。
  • 定期数据验证:定期验证数据库中的数据完整性和准确性。通过验证可以及时发现问题并采取相应的措施。
  • 寻求专业帮助:如果您遇到了无法处理的数据库丢失问题,及时寻求专业的数据恢复服务提供商的帮助。他们具备专业知识和工具来处理各种复杂情况。

无论使用哪种mysql数据恢复技术,都需要谨慎行事并根据具体情况选择合适的方法。从现在起,确保您的数据库备份策略完善,并遵循最佳实践,以降低数据丢失的风险。

三、什么是日志文件简述利用日志恢复?

 日志文件是用于记录系统操作事件的记录文件或文件集合,操作系统有操作系统日志文件,数据库系统有数据库系统日志文件,等等。系统日志文件是包含关于系统消息的文件,包括内核、服务、在系统上运行的应用程序等。不同的日志文件记载不同的信息。例如,有的是默认的系统日志文件,有的记载特定任务。在数据库中用事务日志文件记录数据的修改操作,其中的每条日志记录或者记录所执行的逻辑操作,或者记录已修改数据的前像和后像。前像是操作执行前的数据复本;后像是操作执行后的数据复本。

四、mysql数据库日志文件怎么管理?

 一.错误日志  错误日志在Mysql数据库中很重要,它记录着mysqld启动和停止,以及服务器在运行过程中发生的任何错误的相关信息。  1.配置信息  --log-error=[file-name]用来指定错误日志存放的位置。  如果没有指定[file-name],默认hostname.err做为文件名,默认存放在DATADIR目录中。  也可以将log-error配置到my.cnf文件中,这样就省去了每次在启动mysqld时都手工指定--log-error.例如:  [mysql@test2]$ vi /etc/my.cnf  # The MySQL server

五、如何查看mysql数据库操作记录日志?

MySQL 8.0 重新定义了错误日志输出和过滤,改善了原来臃肿并且可读性很差的错误日志。比如增加了 JSON 输出,在原来的日志后面以序号以及 JSON 后缀的方式展示。比如我机器上的 MySQL 以 JSON 保存的错误日志 mysqld.log.00.json:[root@centos-ytt80 mysql80]# jq . mysqld.log.00.json{ "log_type": 1, "prio": 1, "err_code": 12592, "subsystem": "InnoDB", "msg": "Operating system error number 2 in a file operation.", "time": "2019-09-03T08:16:12.111808Z", "thread": 8, "err_symbol": "ER_IB_MSG_767", "SQL_state": "HY000", "label": "Error"}{ "log_type": 1, "prio": 1, "err_code": 12593, "subsystem": "InnoDB", "msg": "The error means the system cannot find the path specified.", "time": "2019-09-03T08:16:12.111915Z", "thread": 8, "err_symbol": "ER_IB_MSG_768", "SQL_state": "HY000", "label": "Error"}{ "log_type": 1, "prio": 1, "err_code": 12216, "subsystem": "InnoDB", "msg": "Cannot open datafile for read-only: './ytt2/a.ibd' OS error: 71", "time": "2019-09-03T08:16:12.111933Z", "thread": 8, "err_symbol": "ER_IB_MSG_391", "SQL_state": "HY000", "label": "Error"}以 JSON 输出错误日志后可读性和可操作性增强了许多。这里可以用 Linux 命令 jq 或者把这个字串 COPY 到其他解析 JSON 的工具方便处理。只想非常快速的拿出错误信息,忽略其他信息。[root@centos-ytt80 mysql80]# jq '.msg' mysqld.log.00.json"Operating system error number 2 in a file operation.""The error means the system cannot find the path specified.""Cannot open datafile for read-only: './ytt2/a.ibd' OS error: 71""Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.""Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue."使用 JSON 输出的前提是安装 JSON 输出部件。

INSTALL COMPONENT 'file://component_log_sink_json';

完了在设置变量 SET GLOBAL log_error_services = 'log_filter_internal; log_sink_json';

格式为:过滤规则;日志输出;[过滤规则]日志输出;查看安装好的部件mysql> select * from mysql.component;+--------------+--------------------+---------------------------------------+| component_id | component_group_id | component_urn |+--------------+--------------------+---------------------------------------+| 2 | 1 | file://component_log_sink_json |+--------------+--------------------+---------------------------------------+3 rows in set (0.00 sec)

现在设置 JSON 输出,输出到系统日志的同时输出到 JSON 格式日志。mysql> SET persist log_error_services = 'log_filter_internal; log_sink_internal; log_sink_json';Query OK, 0 rows affected (0.00 sec)

来测试一把。我之前已经把表 a 物理文件删掉了。mysql> select * from a;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`a`.

现在错误日志里有 5 条记录。

[root@centos-ytt80 mysql80]# tailf mysqld.log

2019-09-03T08:16:12.111808Z 8 [ERROR] [MY-012592] [InnoDB] Operating system error number 2 in a file operation.

2019-09-03T08:16:12.111915Z 8 [ERROR] [MY-012593] [InnoDB] The error means the system cannot find the path specified.

2019-09-03T08:16:12.111933Z 8 [ERROR] [MY-012216] [InnoDB] Cannot open datafile for read-only: './ytt2/a.ibd' OS error: 71

2019-09-03T08:16:12.112227Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

2019-09-03T08:16:14.902617Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

JSON 日志里也有 5 条记录。

[root@centos-ytt80 mysql80]# tailf mysqld.log.00.json

{ "log_type" : 1, "prio" : 1, "err_code" : 12592, "subsystem" : "InnoDB", "msg" : "Operating system error number 2 in a file operation.", "time" : "2019-09-03T08:16:12.111808Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_767", "SQL_state" : "HY000", "label" : "Error" }

{ "log_type" : 1, "prio" : 1, "err_code" : 12593, "subsystem" : "InnoDB", "msg" : "The error means the system cannot find the path specified.", "time" : "2019-09-03T08:16:12.111915Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_768", "SQL_state" : "HY000", "label" : "Error" }

{ "log_type" : 1, "prio" : 1, "err_code" : 12216, "subsystem" : "InnoDB", "msg" : "Cannot open datafile for read-only: './ytt2/a.ibd' OS error: 71", "time" : "2019-09-03T08:16:12.111933Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_391", "SQL_state" : "HY000", "label" : "Error" }

{ "log_type" : 1, "prio" : 2, "err_code" : 12049, "subsystem" : "InnoDB", "msg" : "Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.", "time" : "2019-09-03T08:16:12.112227Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_224", "SQL_state" : "HY000", "label" : "Warning" }

{ "log_type" : 1, "prio" : 2, "err_code" : 12049, "subsystem" : "InnoDB", "msg" : "Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.", "time" : "2019-09-03T08:16:14.902617Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_224", "SQL_state" : "HY000", "label" : "Warning" }

那可能有人就问了,这有啥意义呢?只是把格式变了,过滤的规则我看还是没变。那我们现在给第二条日志输出加过滤规则先把过滤日志的部件安装起来

INSTALL COMPONENT 'file://component_log_filter_dragnet';

mysql> SET persist log_error_services = 'log_filter_internal; log_sink_internal; log_filter_dragnet;log_sink_json';

Query OK, 0 rows affected (0.00 sec)

只保留 error,其余的一律过滤掉。SET GLOBAL dragnet.log_error_filter_rules = 'IF prio>=WARNING THEN drop.';

检索一张误删的表mysql> select * from a;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`a`.

查看错误日志和 JSON 错误日志发现错误日志里有一条 Warning,JSON 错误日志里的被过滤掉了。2019-09-03T08:22:32.978728Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

再举个例子,每 60 秒只允许记录一个 Warning 事件mysql> SET GLOBAL dragnet.log_error_filter_rules = 'IF prio==WARNING THEN throttle 1/60.';Query OK, 0 rows affected (0.00 sec)

多次执行mysql> select * from b;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`b`.mysql> select * from b;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`b`.mysql> select * from b;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`b`.

现在错误日志里有三条 warning 信息

2019-09-03T08:49:06.820635Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`b` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

2019-09-03T08:49:31.455907Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`b` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

2019-09-03T08:50:00.430867Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`b` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

mysqld.log.00.json 只有一条{ "log_type" : 1, "prio" : 2, "err_code" : 12049, "subsystem" : "InnoDB", "msg" : "Cannot calculate statistics for table `ytt2`.`b` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.", "time" : "2019-09-03T08:49:06.820635Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_224", "SQL_state" : "HY000", "and_n_more" : 3, "label" : "Warning" }

总结,我这里简单介绍了下 MySQL 8.0 的错误日志过滤以及 JSON 输出。MySQL 8.0 的component_log_filter_dragnet 部件过滤规则非常灵活,可以参考手册,根据它提供的语法写出自己的过滤掉的日志输出。

六、MySQL怎么清除日志?

mysql如果开启binlog日志,长时间就会积累很多占用磁盘空间的binlog日志。

一般清理这些日志就可以达到节省磁盘空间的要求。

首先登录mysql

mysql> show binary logs;

mysql> purge binary logs to 'mysql-bin.000098';

mysql> show binary logs;

这样你就发现mysql-bin.000098之前的mysql-binlog日志都已经删除了。

如果平时用不到mysql-bin log日志,可以在

my.cnf

里面注销关闭,然后重启mysql就不产生binlog日志了。但是平时我们如果遇到数据需要回复,还是最好能开启binlog日志,这样数据回复的时候就有一个保障。

七、MySQL日志管理详解?

MySQL 8.0 重新定义了错误日志输出和过滤,改善了原来臃肿并且可读性很差的错误日志。比如增加了 JSON 输出,在原来的日志后面以序号以及 JSON 后缀的方式展示。比如我机器上的 MySQL 以 JSON 保存的错误日志 mysqld.log.00.json:[root@centos-ytt80 mysql80]# jq . mysqld.log.00.json{ "log_type": 1, "prio": 1, "err_code": 12592, "subsystem": "InnoDB", "msg": "Operating system error number 2 in a file operation.", "time": "2019-09-03T08:16:12.111808Z", "thread": 8, "err_symbol": "ER_IB_MSG_767", "SQL_state": "HY000", "label": "Error"}{ "log_type": 1, "prio": 1, "err_code": 12593, "subsystem": "InnoDB", "msg": "The error means the system cannot find the path specified.", "time": "2019-09-03T08:16:12.111915Z", "thread": 8, "err_symbol": "ER_IB_MSG_768", "SQL_state": "HY000", "label": "Error"}{ "log_type": 1, "prio": 1, "err_code": 12216, "subsystem": "InnoDB", "msg": "Cannot open datafile for read-only: './ytt2/a.ibd' OS error: 71", "time": "2019-09-03T08:16:12.111933Z", "thread": 8, "err_symbol": "ER_IB_MSG_391", "SQL_state": "HY000", "label": "Error"}以 JSON 输出错误日志后可读性和可操作性增强了许多。这里可以用 Linux 命令 jq 或者把这个字串 COPY 到其他解析 JSON 的工具方便处理。只想非常快速的拿出错误信息,忽略其他信息。[root@centos-ytt80 mysql80]# jq '.msg' mysqld.log.00.json"Operating system error number 2 in a file operation.""The error means the system cannot find the path specified.""Cannot open datafile for read-only: './ytt2/a.ibd' OS error: 71""Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.""Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue."使用 JSON 输出的前提是安装 JSON 输出部件。

INSTALL COMPONENT 'file://component_log_sink_json';

完了在设置变量 SET GLOBAL log_error_services = 'log_filter_internal; log_sink_json';

格式为:过滤规则;日志输出;[过滤规则]日志输出;查看安装好的部件mysql> select * from mysql.component;+--------------+--------------------+---------------------------------------+| component_id | component_group_id | component_urn |+--------------+--------------------+---------------------------------------+| 2 | 1 | file://component_log_sink_json |+--------------+--------------------+---------------------------------------+3 rows in set (0.00 sec)

现在设置 JSON 输出,输出到系统日志的同时输出到 JSON 格式日志。mysql> SET persist log_error_services = 'log_filter_internal; log_sink_internal; log_sink_json';Query OK, 0 rows affected (0.00 sec)

来测试一把。我之前已经把表 a 物理文件删掉了。mysql> select * from a;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`a`.

现在错误日志里有 5 条记录。

[root@centos-ytt80 mysql80]# tailf mysqld.log

2019-09-03T08:16:12.111808Z 8 [ERROR] [MY-012592] [InnoDB] Operating system error number 2 in a file operation.

2019-09-03T08:16:12.111915Z 8 [ERROR] [MY-012593] [InnoDB] The error means the system cannot find the path specified.

2019-09-03T08:16:12.111933Z 8 [ERROR] [MY-012216] [InnoDB] Cannot open datafile for read-only: './ytt2/a.ibd' OS error: 71

2019-09-03T08:16:12.112227Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

2019-09-03T08:16:14.902617Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

JSON 日志里也有 5 条记录。

[root@centos-ytt80 mysql80]# tailf mysqld.log.00.json

{ "log_type" : 1, "prio" : 1, "err_code" : 12592, "subsystem" : "InnoDB", "msg" : "Operating system error number 2 in a file operation.", "time" : "2019-09-03T08:16:12.111808Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_767", "SQL_state" : "HY000", "label" : "Error" }

{ "log_type" : 1, "prio" : 1, "err_code" : 12593, "subsystem" : "InnoDB", "msg" : "The error means the system cannot find the path specified.", "time" : "2019-09-03T08:16:12.111915Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_768", "SQL_state" : "HY000", "label" : "Error" }

{ "log_type" : 1, "prio" : 1, "err_code" : 12216, "subsystem" : "InnoDB", "msg" : "Cannot open datafile for read-only: './ytt2/a.ibd' OS error: 71", "time" : "2019-09-03T08:16:12.111933Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_391", "SQL_state" : "HY000", "label" : "Error" }

{ "log_type" : 1, "prio" : 2, "err_code" : 12049, "subsystem" : "InnoDB", "msg" : "Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.", "time" : "2019-09-03T08:16:12.112227Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_224", "SQL_state" : "HY000", "label" : "Warning" }

{ "log_type" : 1, "prio" : 2, "err_code" : 12049, "subsystem" : "InnoDB", "msg" : "Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.", "time" : "2019-09-03T08:16:14.902617Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_224", "SQL_state" : "HY000", "label" : "Warning" }

那可能有人就问了,这有啥意义呢?只是把格式变了,过滤的规则我看还是没变。那我们现在给第二条日志输出加过滤规则先把过滤日志的部件安装起来

INSTALL COMPONENT 'file://component_log_filter_dragnet';

mysql> SET persist log_error_services = 'log_filter_internal; log_sink_internal; log_filter_dragnet;log_sink_json';

Query OK, 0 rows affected (0.00 sec)

只保留 error,其余的一律过滤掉。SET GLOBAL dragnet.log_error_filter_rules = 'IF prio>=WARNING THEN drop.';

检索一张误删的表mysql> select * from a;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`a`.

查看错误日志和 JSON 错误日志发现错误日志里有一条 Warning,JSON 错误日志里的被过滤掉了。2019-09-03T08:22:32.978728Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`a` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

再举个例子,每 60 秒只允许记录一个 Warning 事件mysql> SET GLOBAL dragnet.log_error_filter_rules = 'IF prio==WARNING THEN throttle 1/60.';Query OK, 0 rows affected (0.00 sec)

多次执行mysql> select * from b;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`b`.mysql> select * from b;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`b`.mysql> select * from b;ERROR 1812 (HY000): Tablespace is missing for table `ytt2`.`b`.

现在错误日志里有三条 warning 信息

2019-09-03T08:49:06.820635Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`b` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

2019-09-03T08:49:31.455907Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`b` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

2019-09-03T08:50:00.430867Z 8 [Warning] [MY-012049] [InnoDB] Cannot calculate statistics for table `ytt2`.`b` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.

mysqld.log.00.json 只有一条{ "log_type" : 1, "prio" : 2, "err_code" : 12049, "subsystem" : "InnoDB", "msg" : "Cannot calculate statistics for table `ytt2`.`b` because the .ibd file is missing. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting.html for how to resolve the issue.", "time" : "2019-09-03T08:49:06.820635Z", "thread" : 8, "err_symbol" : "ER_IB_MSG_224", "SQL_state" : "HY000", "and_n_more" : 3, "label" : "Warning" }

总结,我这里简单介绍了下 MySQL 8.0 的错误日志过滤以及 JSON 输出。MySQL 8.0 的component_log_filter_dragnet 部件过滤规则非常灵活,可以参考手册,根据它提供的语法写出自己的过滤掉的日志输出。

八、mysql主从日志格式?

statement:历史比较悠久。会记录完整的sql语句。binlog空间比较小,跨库操作丢失数据。针对一些函数uuid,rand() 不安全性 (SBR,statement based replication)

row:安全性很高,不会出现丢失数据的情况。binlog空间占用比较大,会记录每行操作的语句。mysql5.6 binlog_rows_query_log_events 开启也可以看到完整sql语句了。在生产中建议使用row这种binlog记录格式 (RBR,row based replication)

九、怎么查看mysql日志?

1、首先确认你日志是否启用了mysql>show variables like 'log_bin'。

2、如果启用了,即ON,那日志文件就在mysql的安装目录的data目录下。

3、怎样知道当前的日志mysql> show master status。

4、看二进制日志文件用mysqlbinlog,shell>mysqlbinlog mail-bin.000001或者shell>mysqlbinlog mail-bin.000001 | tail,Windows 下用类似的。

MySQL的日志操作:

1、首先,登陆mysql后,执行sql语句:show variables like 'log_bin'。

2、#错误日志log-errol开启方式:在my.ini的[mysqld]选项下:添加代码:log-error=E:\log-error.txt。

记录内容:主要是记录启动、运行或停止mysqld时出现的致命性问题,都是系统级的错误记录。

3、#查询日志:log,开启方式:在my.ini的[mysqld]选项下:添加代码:log=E:/mysql_log.txt。

4、#二进制日志:log-bin,开启方式:在my.ini的[mysqld]选项下:添加代码:log-bin=E:/mysql_log_bin,记录内容:主要是记录所有的更改数据的语句,可使用mysqlbinlog命令恢复数据。

十、mysql日志

MySQL日志:保障数据库安全的重要措施

随着互联网的快速发展和数据规模的不断扩大,数据库的安全性变得尤为重要。MySQL作为广泛使用的关系型数据库管理系统,具备了很好的稳定性和可靠性。而MySQL日志系统,则是保障数据库安全的重要措施之一。下面,我们将详细了解MySQL日志系统以及它对数据库安全的重要作用。

什么是MySQL日志?

MySQL日志是MySQL数据库管理系统用来记录数据库活动的机制。它记录了对数据库进行的各种操作,包括数据更改、事务操作、错误发生等。MySQL日志有五种类型:

  • 错误日志 (error log):记录了MySQL服务启动和运行过程中出现的错误。
  • 查询日志 (query log):记录对数据库执行的查询语句。
  • 慢查询日志 (slow query log):记录执行时间超过阈值的查询语句。
  • 二进制日志 (binary log):记录了对数据库进行的所有更改操作,用于数据恢复、主从复制等。
  • 事务日志 (transaction log):记录了数据库的事务操作,用于崩溃恢复和回滚。

MySQL日志对数据库安全的重要作用

MySQL日志对数据库安全具有以下重要作用:

1. 数据完整性保护

通过MySQL日志,可以追踪数据库的变更记录,包括数据的插入、更新和删除。当出现数据异常或错误时,可以通过分析日志找到问题的根源,保证数据的完整性。此外,MySQL的事务日志还可以用于回滚操作,确保在事务执行失败或中断时,数据库的一致性得到保障。

2. 故障恢复和数据复原

MySQL的二进制日志是一种高效的恢复机制。当数据库发生故障或系统崩溃时,可以通过二进制日志进行数据恢复。同时,二进制日志还支持数据库的主从复制,通过在主库上生成二进制日志并传递给从库,实现数据的备份和复制,增加数据库的容灾能力。

3. 安全审计与追责

MySQL的查询日志和慢查询日志记录了所有对数据库执行的查询语句,可以用于安全审计和追责。通过分析查询日志,可以发现并定位潜在的安全威胁,尽早采取相应的安全措施。同时,慢查询日志还可以帮助优化数据库性能,提高查询效率。

4. 异常和错误排查

MySQL的错误日志记录了服务启动和运行过程中出现的错误信息,有助于排查异常和错误。当数据库出现问题时,通过分析错误日志可以快速定位和解决问题,减少系统停机时间。

配置MySQL日志

为了充分利用MySQL日志系统,我们需要对其进行适当配置。以下是一些常见的配置技巧:

1. 错误日志配置

通过在MySQL的配置文件中设置log_error选项,可以指定错误日志的路径和文件名。例如:log_error=/var/log/mysql/error.log,将错误日志记录在/var/log/mysql/目录下的error.log文件中。

2. 查询日志配置

查询日志会记录所有执行的查询语句,包括敏感信息。因此,默认情况下是不开启的。可以通过设置general_loggeneral_log_file选项开启查询日志,并指定日志文件的路径。例如:general_log=1general_log_file=/var/log/mysql/query.log

3. 慢查询日志配置

慢查询日志记录执行时间超过阈值的查询语句。可以通过设置slow_query_logslow_query_log_file选项开启慢查询日志,并指定日志文件路径。同时,还可以设置long_query_time选项来定义阈值,默认为10秒。

4. 二进制日志配置

二进制日志记录了所有对数据库的更改操作。可以通过设置log_bin选项开启二进制日志。同时,还可以设置expire_logs_days选项来定义日志保留的天数。

5. 事务日志配置

事务日志是InnoDB存储引擎特有的日志文件。可以通过设置innodb_log_file_sizeinnodb_log_files_in_group选项来配置事务日志的大小和数量。

总结

MySQL日志系统是保障数据库安全的重要措施。通过记录数据库的各种操作和活动,MySQL日志能够保障数据的完整性和一致性,支持故障恢复和数据复原,帮助安全审计和追责,以及异常和错误排查。为了发挥MySQL日志的作用,我们需要适当配置各类日志,并定期对日志进行分析和审查,确保数据库的安全性和稳定性。

顶一下
(0)
0%
踩一下
(0)
0%
相关评论
我要评论
用户名: 验证码:点击我更换图片