Home Database Mysql Tutorial 了解和正确维护MySQL日志_MySQL

了解和正确维护MySQL日志_MySQL

May 31, 2016 am 08:48 AM

前段时间自己给自己挖了坑,为了优化MySQL数据库配置在运行有InnoDB的MySQL数据库中修改了my.cnf文件中关于InnoDB的部分配置,尽管是在关机的情况下操作的,但依旧忽略了InnoDB对日志文件(如ibdata1、ib_logfile0、ib_logfile1、ib_logfile2)强大的依赖性,删掉这些特殊的日志文件意味着对InnoDB的破坏,可能导致数据库无法启动和恢复。从MySQL中文手册上摘录了部分有用的知识点整理如下。

MySQL日志简介

MySQL共有5种不同的日志类型,分别为错误日志、通用查询日志、中继日志、二进制日志和慢日志。其中错误日志、

日志文件 记入文件中的信息类型
错误日志 记录启动、运行或停止mysqld时出现的问题。
查询日志 记录建立的客户端连接和执行的语句。
中继日志 从主复制服务器上接受到的数据变化。
更新日志 记录更改数据的语句。不赞成使用该日志。(新版本已经废弃)
二进制日志 记录所有更改数据的语句。还用于复制。
慢日志 记录所有执行时间超过long_query_time秒的所有查询或不使用索引的查询。

错误日志文件包含了当mysqld启动和停止时,以及服务器在运行过程中发生任何严重错误时的相关信息。

查询日志包含了所有连接和语句,能确切地知道某一个客户端发送给mysqld哪些语句。

查询日志还包含所有语句,而二进制日志不包含只查询数据的语句,如果想要记录所有语句(例如,为了识别有问题的查询),你应使用查询日志。

二进制日志以一种更有效的格式,并且是事务安全的方式包含更新日志中可用的所有信息,还包含关于每个更新数据库的语句的执行时间信息。

二进制日志的主要目的是在恢复使能够最大可能地更新数据库,因为二进制日志包含备份后进行的所有更新。

二进制日志还用于在主复制服务器上记录所有将发送给从服务器的语句。

image

如果当前的二进制日志大小达到max_binlog_size,还会自动创建新的二进制日志,但一个事务产生的二进制日志有可能会超过max_binlog_size。

image

可以用RESET MASTER语句删除所有二进制日志文件,或用PURGE MASTER LOGS只删除部分二进制文件。例如先查看现有的二进制文件,SHOW BINARY LOGS,再删除某些二进制文件,PURGE BINARY LOGS TO 'mysql-bin.000009'。

慢查询日志可以用来找到执行时间长的查询,可以用于优化。但是,检查又长又慢的查询日志会很困难。要想容易些,你可以使用 mysqldumpslow 命令获得日志中显示的查询摘要来处理慢查询日志。

日志维护

MySQL服务器可以创建各种不同的日志文件,从而可以很容易地看见所进行的操作。但是,你必须定期清理这些文件,确保日志不会占用太多的硬盘空间,否则很短的时间内就会把数据库磁盘吃满。

ZZ73UA401@1MZ5X)5R%S9DD

数据库备份经验

内容较多,占用空间较大的数据库在导出的过程中可能仅需要比较短的时间,通常一小时内就能备份完成,但恢复时会花掉很多时间,往往超过数小时。因此有必要及时删减数据库中的无用的信息,甚至设计数据库时考虑和标注清楚,那些数据是相对不重要的。如果备份时能将日志文件切断就更好了。

可以使用mysql-log-rotate脚本将日志文件进行切割。你可以通过mysqladmin flush-logs或SQL语句FLUSH LOGS来强制MySQL开始使用新的日志文件(不适用于二进制日志,要清除二进制日志参考上面的RESET MASTER或PURGE BINARY LOGS语法)。

image

image

image

当CPU为单核或性能不足时,iowaite的值会很大,表明CPU在等待磁盘写入的过程中花费了太多的时间,这时就有必要增加CPU的配置。如下图所示,%wa数值是%us的近10倍,说明磁盘IO开销比较严重。

image  

image

附:关于InnoDB存储引擎

存储引擎比较:

Storage engine comparison

   

   

不一定要使用相同的存储引擎,每个表可以使用不同的存储引擎。

InnoDB的中文参考资料可以参见 http://dev.mysql.com/doc/refman/5.1/zh/storage-engines.html#innodb

InnoDB是为处理巨大数据量时的最大性能设计,被用在众多需要高性能的大型数据库站点上。

被InnoDB存储引擎管理的两个重要的基于磁盘的资源是InnoDB表空间数据文件和它的日志文件。

如果你指定无InnoDB配置选项,MySQL将在数据目录下创建一个名为ibdata1的10MB大小的自动扩展数据文件,以及两个名为ib_logfile0和ib_logfile1的5MB大小的日志文件。

要为一个主服务器建立一个新服务器,你不得不复制InnoDB表空间和日志文件,以及InnoDB表的.frm文件,并且移动复件到从服务器。

如果你可以关闭主服务器或者一个存在的从服务器。你可以采取InnoDB表空间和日志文件的冷备份,并用它来建立一个从服务器。要不关闭任何服务器来建立一个新的从服务器,你也可以使用非免费(商业的) InnoDB热备份工具 。

关于移动、备份和恢复InnoDB,可以参考InnoDB的中文参考资料: http://dev.mysql.com/doc/refman/5.1/zh/storage-engines.html#backing-up

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

See all articles