MySqlStudy之--MySql日志管理
MySql Study之--MySql日志管理 一、日志文件类型 MySQL有几个不同的日志文件,可以帮助你找出 mysqld 内部发生的事情: 日志文件 日志文件信息内容 错误日志 记录启动、运行或停止mysqld时出现的问题。(log_err) 查询日志 记录建立的客户端连接和执行的语句
MySql Study之--MySql日志管理
一、日志文件类型
MySQL有几个不同的日志文件,可以帮助你找出mysqld内部发生的事情:
日志文件 |
日志文件信息内容 |
错误日志 |
记录启动、运行或停止mysqld时出现的问题。(log_err) |
查询日志 |
记录建立的客户端连接和执行的语句。 |
更新日志 |
记录更改数据的语句,不赞成使用该日志。 |
二进制日志 |
记录所有更改数据的语句。还用于复制。(bin_log) |
慢日志 |
记录所有执行时间超过long_query_time秒的所有查询或不使用索引的查询。(slow_query_log) |
二、查询日志存放位置
mysql> show variables like '%log%'; +-----------------------------------------+-----------------------------+ | Variable_name | Value | +-----------------------------------------+-----------------------------+ | back_log | 50 | | binlog_cache_size | 32768 | | binlog_checksum | NONE | | binlog_direct_non_transactional_updates | OFF | | binlog_format | STATEMENT | | binlog_row_image | FULL | | binlog_rows_query_log_events | OFF | | binlog_stmt_cache_size | 32768 | | expire_logs_days | 0 | | general_log | OFF | | general_log_file | /var/lib/mysql/rh6.log | | innodb_flush_log_at_trx_commit | 1 | | innodb_locks_unsafe_for_binlog | OFF | | innodb_log_buffer_size | 8388608 | | innodb_log_file_size | 5242880 | | innodb_log_files_in_group | 2 | | innodb_log_group_home_dir | ./ | | innodb_mirrored_log_groups | 1 | | innodb_undo_logs | 128 | | log_bin | OFF | | log_bin_basename | | | log_bin_index | | | log_bin_trust_function_creators | OFF | | log_error | /var/log/mysqld.log | | log_output | FILE | | log_queries_not_using_indexes | OFF | | log_slave_updates | OFF | | log_warnings | 1 | | max_binlog_cache_size | 18446744073709547520 | | max_binlog_size | 1073741824 | | max_binlog_stmt_cache_size | 18446744073709547520 | | max_relay_log_size | 0 | | relay_log | | | relay_log_basename | | | relay_log_index | | | relay_log_info_file | relay-log.info | | relay_log_info_repository | FILE | | relay_log_purge | ON | | relay_log_recovery | OFF | | relay_log_space_limit | 0 | | slow_query_log | OFF | | slow_query_log_file | /var/lib/mysql/rh6-slow.log | | sql_log_bin | ON | | sql_log_off | OFF | | sync_binlog | 0 | | sync_relay_log | 0 | | sync_relay_log_info | 0 | +-----------------------------------------+-----------------------------+ 47 rows in set (0.02 sec) mysql> show variables like '%log%'; +-----------------------------------------+-----------------------------+ | Variable_name | Value | +-----------------------------------------+-----------------------------+ | back_log | 50 | | binlog_cache_size | 32768 | | binlog_checksum | NONE | | binlog_direct_non_transactional_updates | OFF | | binlog_format | STATEMENT | | binlog_row_image | FULL | | binlog_rows_query_log_events | OFF | | binlog_stmt_cache_size | 32768 | | expire_logs_days | 0 | | general_log | OFF | | general_log_file | /var/lib/mysql/rh6.log | | innodb_flush_log_at_trx_commit | 1 | | innodb_locks_unsafe_for_binlog | OFF | | innodb_log_buffer_size | 8388608 | | innodb_log_file_size | 5242880 | | innodb_log_files_in_group | 2 | | innodb_log_group_home_dir | ./ | | innodb_mirrored_log_groups | 1 | | innodb_undo_logs | 128 | | log_bin | OFF | | log_bin_basename | | | log_bin_index | | | log_bin_trust_function_creators | OFF | | log_error | /var/log/mysqld.log | | log_output | FILE | | log_queries_not_using_indexes | OFF | | log_slave_updates | OFF | | log_warnings | 1 | | max_binlog_cache_size | 18446744073709547520 | | max_binlog_size | 1073741824 | | max_binlog_stmt_cache_size | 18446744073709547520 | | max_relay_log_size | 0 | | relay_log | | | relay_log_basename | | | relay_log_index | | | relay_log_info_file | relay-log.info | | relay_log_info_repository | FILE | | relay_log_purge | ON | | relay_log_recovery | OFF | | relay_log_space_limit | 0 | | slow_query_log | OFF | | slow_query_log_file | /var/lib/mysql/rh6-slow.log | | sql_log_bin | ON | | sql_log_off | OFF | | sync_binlog | 0 | | sync_relay_log | 0 | | sync_relay_log_info | 0 | +-----------------------------------------+-----------------------------+ 47 rows in set (0.02 sec)
1、错误日志(log_err指定了错误日志的位置)
错误日志文件包含了当mysqld启动和停止时,以及服务器在运行过程中发生任何严重错误时的相关信息。
如果mysqld莫名其妙地死掉并且mysqld_safe需要重新启动它,mysqld_safe在错误日志中写入一条restarted mysqld消息。如果mysqld注意到需要自动检查或着修复一个表,则错误日志中写入一条消息。
在一些操作系统中,如果mysqld服务终止,错误日志包含堆栈跟踪信息。跟踪信息可以用来确定mysqld终止的地方。
可以用--log-error[=file_name]选项来指定mysqld保存错误日志文件的位置。如果没有给定file_name值,mysqld使用错误日志名host_name.err 并在数据目录中写入日志文件。如果你执行FLUSH LOGS,错误日志用-old重新命名后缀并且mysqld创建一个新的空日志文件。(如果未给出--log-error选项,则不会重新命名)。
如果不指定--log-error,或者(在Windows中)如果你使用--console选项,错误被写入标准错误输出stderr。通常标准输出为你的终端。
[root@rh6 ~]# more /var/log/mysqld.log 150402 10:26:07 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 150402 10:26:08 InnoDB: The InnoDB memory heap is disabled 150402 10:26:08 InnoDB: Mutexes and rw_locks use GCC atomic builtins 150402 10:26:08 InnoDB: Compressed tables use zlib 1.2.3 150402 10:26:08 InnoDB: Using Linux native AIO 150402 10:26:08 InnoDB: CPU does not support crc32 instructions 150402 10:26:08 InnoDB: Initializing buffer pool, size = 128.0M 150402 10:26:08 InnoDB: Completed initialization of buffer pool 150402 10:26:08 InnoDB: highest supported file format is Barracuda. InnoDB: The log sequence number in ibdata files does not match InnoDB: the log sequence number in the ib_logfiles! 150402 10:26:08 InnoDB: Database was not shut down normally! InnoDB: Starting crash recovery. InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages from the doublewrite InnoDB: buffer... 150402 10:26:10 InnoDB: 128 rollback segment(s) are active. 150402 10:26:10 InnoDB: Waiting for the background threads to start 150402 10:26:11 InnoDB: 1.2.4 started; log sequence number 1602901 150402 10:26:11 [Note] Recovering after a crash using mysql-bin 150402 10:26:11 [Note] Starting crash recovery... 150402 10:26:11 [Note] Crash recovery finished. 150402 10:26:11 [Note] Event Scheduler: Loaded 0 events 150402 10:26:11 [Note] /usr/local/mysql/bin/mysqld: ready for connections. Version: '5.6.4-m7-log' socket: '/tmp/mysql.sock' port: 3306 Source distribution [root@rh6 ~]# more /var/log/mysqld.log 150402 10:26:07 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 150402 10:26:08 InnoDB: The InnoDB memory heap is disabled 150402 10:26:08 InnoDB: Mutexes and rw_locks use GCC atomic builtins 150402 10:26:08 InnoDB: Compressed tables use zlib 1.2.3 150402 10:26:08 InnoDB: Using Linux native AIO 150402 10:26:08 InnoDB: CPU does not support crc32 instructions 150402 10:26:08 InnoDB: Initializing buffer pool, size = 128.0M 150402 10:26:08 InnoDB: Completed initialization of buffer pool 150402 10:26:08 InnoDB: highest supported file format is Barracuda. InnoDB: The log sequence number in ibdata files does not match InnoDB: the log sequence number in the ib_logfiles! 150402 10:26:08 InnoDB: Database was not shut down normally! InnoDB: Starting crash recovery. InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages from the doublewrite InnoDB: buffer... 150402 10:26:10 InnoDB: 128 rollback segment(s) are active. 150402 10:26:10 InnoDB: Waiting for the background threads to start 150402 10:26:11 InnoDB: 1.2.4 started; log sequence number 1602901 150402 10:26:11 [Note] Recovering after a crash using mysql-bin 150402 10:26:11 [Note] Starting crash recovery... 150402 10:26:11 [Note] Crash recovery finished. 150402 10:26:11 [Note] Event Scheduler: Loaded 0 events 150402 10:26:11 [Note] /usr/local/mysql/bin/mysqld: ready for connections. Version: '5.6.4-m7-log' socket: '/tmp/mysql.sock' port: 3306 Source distribution
2、慢速查询日志(slow_query_log)
用--log-slow-queries[=file_name]选项启动时,mysqld写一个包含所有执行时间超过long_query_time秒的SQL语句的日志文件。获得初使表锁定的时间不算作执行时间。
如果没有给出file_name值, 默认未主机名,后缀为-slow.log。如果给出了文件名,但不是绝对路径名,文件则写入数据目录。
语句执行完并且所有锁释放后记入慢查询日志。记录顺序可以与执行顺序不相同。
慢查询日志可以用来找到执行时间长的查询,可以用于优化。但是,检查又长又慢的查询日志会很困难。要想容易些,你可以使用mysqldumpslow命令获得日志中显示的查询摘要来处理慢查询日志。
在MySQL 5.1的慢查询日志中,不使用索引的慢查询同使用索引的查询一样记录。要想防止不使用索引的慢查询记入慢查询日志,使用--log-short-format选项。
在MySQL 5.1中,通过--log-slow-admin-statements服务器选项,你可以请求将慢管理语句,例如OPTIMIZE TABLE、ANALYZE TABLE和 ALTER TABLE写入慢查询日志。
用查询缓存处理的查询不加到慢查询日志中,因为表有零行或一行而不能从索引中受益的查询也不写入慢查询日志。
默认情况下MySql没有开启慢查询日志,开启慢查询日志:
mysql> set global slow_query_log=on; Query OK, 0 rows affected (0.33 sec) mysql> show variables like '%slow_%'; +---------------------+-----------------------------+ | Variable_name | Value | +---------------------+-----------------------------+ | slow_launch_time | 2 | | slow_query_log | ON | | slow_query_log_file | /var/lib/mysql/rh6-slow.log | +---------------------+-----------------------------+ 3 rows in set (0.02 sec)mysql> set global slow_query_log=on; Query OK, 0 rows affected (0.33 sec) mysql> show variables like '%slow_%'; +---------------------+-----------------------------+ | Variable_name | Value | +---------------------+-----------------------------+ | slow_launch_time | 2 | | slow_query_log | ON | | slow_query_log_file | /var/lib/mysql/rh6-slow.log | +---------------------+-----------------------------+ 3 rows in set (0.02 sec)查看慢查询日志:
[root@rh6 mysql]# cat /var/lib/mysql/rh6-slow.log /usr/local/mysql/bin/mysqld, Version: 5.6.4-m7 (Source distribution). started with: Tcp port: 3306 Unix socket: /tmp/mysql.sock Time Id Command Argument [root@rh6 mysql]# cat /var/lib/mysql/rh6-slow.log /usr/local/mysql/bin/mysqld, Version: 5.6.4-m7 (Source distribution). started with: Tcp port: 3306 Unix socket: /tmp/mysql.sock Time Id Command Argument3、二进制日志(log_bin)
1)二进制文件介绍
二进制日志以一种更有效的格式,并且是事务安全的方式包含更新日志中可用的所有信息。
二进制日志包含了所有更新了数据或者已经潜在更新了数据(例如,没有匹配任何行的一个DELETE)的所有语句。语句以“事件”的形式保存,它描述数据更改。
备注:二进制日志已经代替了老的更新日志,更新日志在MySQL 5.1中不再使用。
2)二进制文件的功能
二进制日志还包含关于每个更新数据库的语句的执行时间信息。它不包含没有修改任何数据的语句。如果你想要记录所有语句(例如,为了识别有问题的查询),你应使用一般查询日志。
二进制日志的主要目的是在恢复使能够最大可能地更新数据库,因为二进制日志包含备份后进行的所有更新。
二进制日志还用于在主复制服务器上记录所有将发送给从服务器的语句。
运行服务器时若启用二进制日志则性能大约慢1%。但是,二进制日志的好处,即用于恢复并允许设置复制超过了这个小小的性能损失。
3)二进制文件的存储路径
当用--log-bin[=file_name]选项启动时,mysqld写入包含所有更新数据的SQL命令的日志文件。如果未给出file_name值, 默认名为-bin后面所跟的主机名。如果给出了文件名,但没有包含路径,则文件被写入数据目录。建议指定一个文件名.
如果你在日志名中提供了扩展名(例如,--log-bin=file_name.extension),则扩展名被悄悄除掉并忽略。
mysqld在每个二进制日志名后面添加一个数字扩展名。每次你启动服务器或刷新日志时该数字则增加。如果当前的日志大小达到max_binlog_size,还会自动创建新的二进制日志。如果你正使用大的事务,二进制日志还会超过max_binlog_size:事务全写入一个二进制日志中,绝对不要写入不同的二进制日志中。
为了能够知道还使用了哪个不同的二进制日志文件,mysqld还创建一个二进制日志索引文件,包含所有使用的二进制日志文件的文件名。默认情况下与二进制日志文件的文件名相同,扩展名为'.index'。你可以用--log-bin-index[=file_name]选项更改二进制日志索引文件的文件名。当mysqld在运行时,不应手动编辑该文件;如果这样做将会使mysqld变得混乱。
开启二进制日志: [root@rh6 mysql]# cat /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/tmp/mysql.sock #socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-bin=mysql-bin binlog_format=mixed [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid 开启二进制日志: [root@rh6 mysql]# cat /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/tmp/mysql.sock #socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-bin=mysql-bin binlog_format=mixed [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
开启二进制日志后:mysql> show variables like '%log_bin%'; +---------------------------------+--------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------+ | log_bin | ON | | log_bin_basename | /var/lib/mysql/mysql-bin | | log_bin_index | /var/lib/mysql/mysql-bin.index | | log_bin_trust_function_creators | OFF | | sql_log_bin | ON | +---------------------------------+--------------------------------+ 5 rows in set (0.00 sec) mysql> show variables like '%log_bin%'; +---------------------------------+--------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------+ | log_bin | ON | | log_bin_basename | /var/lib/mysql/mysql-bin | | log_bin_index | /var/lib/mysql/mysql-bin.index | | log_bin_trust_function_creators | OFF | | sql_log_bin | ON | +---------------------------------+--------------------------------+ 5 rows in set (0.00 sec)[root@rh6 ~]# ls -l /var/lib/mysql/ total 28704 -rw-rw---- 1 mysql mysql 56 Jan 28 17:25 auto.cnf -rw-rw---- 1 mysql mysql 18874368 Apr 2 10:26 ibdata1 -rw-rw---- 1 mysql mysql 5242880 Apr 2 10:26 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 Jan 28 17:21 ib_logfile1 drwx------ 2 mysql root 4096 Jan 28 17:21 mysql -rw-rw---- 1 mysql mysql 114 Apr 2 10:26 mysql-bin.000001 -rw-rw---- 1 mysql mysql 114 Apr 2 10:26 mysql-bin.000002 -rw-rw---- 1 mysql mysql 38 Apr 2 10:26 mysql-bin.index drwx------ 2 mysql mysql 4096 Jan 28 17:21 performance_schema -rw-rw---- 1 mysql mysql 173 Mar 31 11:05 rh6-slow.log drwx------ 2 mysql root 4096 Jan 28 17:21 test
查看二进制文件内容:
mysql-bin.index 是二进制文件的索引,一般不能更改
[root@rh6 mysql]# cat mysql-bin.index ./mysql-bin.000001 ./mysql-bin.000002通过mysqlbinlog工具读取二进制文件内容:
[root@rh6 mysql]# mysqlbinlog mysql-bin.000002 /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #150402 10:26:11 server id 1 end_log_pos 114 Start: binlog v 4, server v 5.6.4-m7-log created 150402 10:26:11 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' w6gcVQ8BAAAAbgAAAHIAAAABAAQANS42LjQtbTctbG9nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAADDqBxVEzgNAAgAEgAEBAQEEgAAVgAEGggAAAAICAgCAAAAAJLUw68= '/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; [root@rh6 mysql]# mysqlbinlog mysql-bin.000002 /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #150402 10:26:11 server id 1 end_log_pos 114 Start: binlog v 4, server v 5.6.4-m7-log created 150402 10:26:11 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' w6gcVQ8BAAAAbgAAAHIAAAABAAQANS42LjQtbTctbG9nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAADDqBxVEzgNAAgAEgAEBAQEEgAAVgAEGggAAAAICAgCAAAAAJLUw68= '/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; [html] view plaincopyprint? [root@rh6 mysql]# mysqlbinlog mysql-bin.000001 /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #150331 11:11:39 server id 1 end_log_pos 114 Start: binlog v 4, server v 5.6.4-m7-log created 150331 11:11:39 at startup ROLLBACK/*!*/; BINLOG ' axAaVQ8BAAAAbgAAAHIAAAAAAAQANS42LjQtbTctbG9nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAABrEBpVEzgNAAgAEgAEBAQEEgAAVgAEGggAAAAICAgCAAAAALen85c= '/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; [root@rh6 mysql]# [root@rh6 mysql]# mysqlbinlog mysql-bin.000001 /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #150331 11:11:39 server id 1 end_log_pos 114 Start: binlog v 4, server v 5.6.4-m7-log created 150331 11:11:39 at startup ROLLBACK/*!*/; BINLOG ' axAaVQ8BAAAAbgAAAHIAAAAAAAQANS42LjQtbTctbG9nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAABrEBpVEzgNAAgAEgAEBAQEEgAAVgAEGggAAAAICAgCAAAAALen85c= '/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;如果每天都会生成大量的二进制日志,这些日志长时间不清理的话,将会对磁盘空间带来很大的浪费,所以定期清理日志是DBA维护mysql的一个重要工作
1)reset master
在上面查看日志存放的文件夹中,二进制日志命名的格式是以mysql-bin.*,*代表日志的序号,序号是递增的,其中还有mysql-bin.index是日志的索引文件,记录了日志的最大序号
mysql> reset master; Query OK, 0 rows affected (0.08 sec) [root@rh6 mysql]# ls -l total 28700 -rw-rw---- 1 mysql mysql 56 Jan 28 17:25 auto.cnf -rw-rw---- 1 mysql mysql 18874368 Apr 2 10:26 ibdata1 -rw-rw---- 1 mysql mysql 5242880 Apr 2 10:26 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 Jan 28 17:21 ib_logfile1 drwx------ 2 mysql root 4096 Jan 28 17:21 mysql -rw-rw---- 1 mysql mysql 114 Apr 2 11:20 mysql-bin.000001 -rw-rw---- 1 mysql mysql 19 Apr 2 11:20 mysql-bin.index drwx------ 2 mysql mysql 4096 Jan 28 17:21 performance_schema -rw-rw---- 1 mysql mysql 173 Mar 31 11:05 rh6-slow.log drwx------ 2 mysql root 4096 Jan 28 17:21 test
下一篇: 一个统计数据的有关问题 求指点
推荐阅读
-
Mysql数据库之Binlog日志使用总结(必看篇)
-
MySQL InnoDB 日志管理机制中的MTR和日志刷盘
-
MySQL中间件之ProxySQL(6):管理后端节点
-
解说mysql之binlog日志以及利用binlog日志恢复数据的方法
-
Android之崩溃日志管理
-
MySQL中间件之ProxySQL(3):Admin管理接口
-
Mysql数据库高级用法之视图、事务、索引、自连接、用户管理实例分析
-
MySQL慢查询之pt-query-digest分析慢查询日志
-
mysql 开发进阶篇系列 33 工具篇(mysqlbinlog日志管理工具)
-
MySQL 日志系统之 redo log 和 binlog