Linux学习-MySQL日志管理
Posted 丢爸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习-MySQL日志管理相关的知识,希望对你有一定的参考价值。
日志:
错误日志:
一般查询日志:
慢查询日志:
二进制日志:记录任何能够引起或可能引起数据库改变的语句
复制、即时点恢复
中继日志:从主服务器的二进制日志文件中复制而来的事件,并保存为日志文件
事务日志:ACID,将随机IO转换为顺序IO
#binlog二进制日志
#general_log一般日志
#innodb事务日志
#slow_query_log慢查询日志
#sync_binlog是否将缓存日志同步到磁盘文件
mysql> show global variables like '%log%';
+-----------------------------------------+------------------------+
| Variable_name | Value |
+-----------------------------------------+------------------------+
| back_log | 80 |
| binlog_cache_size | 32768 |
| binlog_checksum | CRC32 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_format | STATEMENT |
| binlog_max_flush_queue_time | 0 |
| binlog_order_commits | ON |
| 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 | /data/mail.log |
| innodb_api_enable_binlog | OFF |
| innodb_flush_log_at_timeout | 1 |
| innodb_flush_log_at_trx_commit | 1 |
| innodb_locks_unsafe_for_binlog | OFF |
| innodb_log_buffer_size | 8388608 |
| innodb_log_file_size | 50331648 |
| innodb_log_files_in_group | 2 |
| innodb_log_group_home_dir | ./ |
| innodb_mirrored_log_groups | 1 |
| innodb_online_alter_log_max_size | 134217728 |
| innodb_undo_logs | 128 |
| log_bin | OFF |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| log_error | /data/mail.tye.com.err |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | OFF |
| log_throttle_queries_not_using_indexes | 0 |
| 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 | /data/mail-slow.log |
| sql_log_bin | ON |
| sql_log_off | OFF |
| sync_binlog | 0 |
| sync_relay_log | 10000 |
| sync_relay_log_info | 10000 |
+-----------------------------------------+------------------------+
54 rows in set (0.00 sec)
错误日志
服务器启动和关闭过程中的信息
服务器运行过程中的错误信息
事务高度器运行一个事件时产生的信息
在从服务器上启动从服务器进程时产生的信息
错误日志位置: | log_error | /data/mail.tye.com.err |
是否记录服务产生的警告信息【1表示记录,0表示不记录】 | log_warnings | 1 |
一般查询日志
| general_log | OFF | 是否启用查询日志(默认关闭,如开启,每次操作,记录一次日志会产生大量IO操作)
| general_log_file | /data/mail.log | 一般日志记录位置
| log_output | FILE | 定义一般查询日志和慢查询日志的保存方式,可以是TABLE,FILE,NONE,也可以是TABLE,FILE组合(使用逗号分隔),默认为TABLE,如果组合中出现NONE,要么其它设定都将失效,同时,无论是否启用日志功能,也不会记录任何相关的日志信息,作用范围为全局级别,可用于配置文件,属动态变量。
慢查询日志
| long_query_time | 10.000000 | 查询超出此变量的设定的时间都称为慢查询(单位:秒钟)
| slow_query_log | OFF | 是否记录慢查询(0或OFF表示禁用,1或者ON表示启用),日志信息的输出位置取决于log_output变量的定义,如果log_output设定为NONE,即使slow_query_log为ON,仍不会记录慢查询信息,作用范围为全局变量,可用于选项文件,属动态变量。
| slow_query_log_file | /data/mail-slow.log | 慢查询日志记录位置,可以通过–slow_query_log_file选项进行修改,作用范围全局级别,可用于选项文件,属动态变量。
二进制日志 注:不要将日志和数据库文件放入同一块硬盘
每次重新启动服务器,二进制日志会进行滚动
----即时点恢复
mysqlbinlog
格式:
基于语句:statement
基于行:row
混合方式:mixed
日志事件:
产生的时间(starttime)
相对位置(position)
日志文件:
| binlog_format | STATEMENT | 二进制日志
log_bin | ON | 是否开启二进制日志
| log_bin_basename | /data/mysql-bin | 二进制日志的基本名称(真正的名称后面加00001)
| log_bin_index | /data/mysql-bin.index | 二进制日志的索引文件
| sql_log_bin | ON | 控制二进制日志信息是否记录进日志文件,默认为ON,表示启用此功能,用户可以在会话级别修改此变量的值,但其必须有SUPER权限,作用范围为全局和会话级别,属动态变量
| sync_binlog | 0 | 设置多久同步一次二进制日志至磁盘文件,0表示不同步,任何正数表示对二进制每多少次写操作之后同步一次,当autocommit的值为1时,每条语句的执行都会引起二进制日志同步,否则每个事务的提交引起二进制日志同步。
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| expire_logs_days | 0 | 二进制日志过期时间
事务日志
| innodb_flush_log_at_trx_commit | 1 |
0:每秒同步,并执行磁盘flush操作
1:每事务同步,并执行磁盘flush操作
2:每事务同步,但不执行磁盘flush操作
| innodb_log_buffer_size | 8388608 | 内存缓存大小
| innodb_log_file_size | 50331648 | 日志大小
| innodb_log_files_in_group | 2 | 日志组中文件的数量
| innodb_log_group_home_dir | ./ | 日志文件的存储位置
#事务日志
[root@mail ~]# ll /data
total 176260
...
-rw-rw---- 1 mysql mysql 50331648 Jul 1 04:25 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Apr 14 07:16 ib_logfile1
...
#查看当前正在使用的二进制日志,position记录当前日志的位置
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#向表中插入数据后,二进制日志的position位置发生改变
mysql> use students;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> insert into tutors(Tname,Gender,Age)values('tye','M',34);
Query OK, 1 row affected (0.20 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set
+------------------+----------+--------------+------------------+-------------------
| mysql-bin.000001 | 408 | | |
+------------------+----------+--------------+------------------+-------------------
1 row in set (0.00 sec)
#查看二进制日志记录的内容
mysql> show binlog events in 'mysql-bin.000001';
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.10-log, Binlog ver: 4 |
| mysql-bin.000001 | 120 | Query | 1 | 207 | BEGIN |
| mysql-bin.000001 | 207 | Intvar | 1 | 239 | INSERT_ID=15 |
| mysql-bin.000001 | 239 | Query | 1 | 377 | use `students`; insert into tutors(Tname,Gender,Age)values('tye','M',34) |
| mysql-bin.000001 | 377 | Xid | 1 | 408 | COMMIT /* xid=15 */ |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------+
5 rows in set (0.00 sec)
#从指定位置查询二进制日志
mysql> show binlog events in 'mysql-bin.000001' from 207;
+------------------+-----+------------+-----------+-------------+--------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+------------+-----------+-------------+--------------------------------------------------------------------------+
| mysql-bin.000001 | 207 | Intvar | 1 | 239 | INSERT_ID=15 |
| mysql-bin.000001 | 239 | Query | 1 | 377 | use `students`; insert into tutors(Tname,Gender,Age)values('tye','M',34) |
| mysql-bin.000001 | 377 | Xid | 1 | 408 | COMMIT /* xid=15 */ |
+------------------+-----+------------+-----------+-------------+--------------------------------------------------------------------------+
3 rows in set (0.00 sec)
#通过mysqlbinlog查看二进制日志
[root@mail data]# mysqlbinlog mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#210630 19:30:31 server id 1 end_log_pos 120 CRC32 0xb0eb839b Start: binlog v 4, server v 5.6.10-log created 210630 19:30:31 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
11XcYA8BAAAAdAAAAHgAAAABAAQANS42LjEwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAADXVdxgEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAZuD
67A=
'/*!*/;
# at 120
#210630 19:34:38 server id 1 end_log_pos 207 CRC32 0xb42e4f72 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1625052878/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1073741824/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 207
# at 239
#210630 19:34:38 server id 1 end_log_pos 239 CRC32 0xcdacb444 Intvar
SET INSERT_ID=15/*!*/;
#210630 19:34:38 server id 1 end_log_pos 377 CRC32 0x137bdf73 Query thread_id=1 exec_time=0 error_code=0
use `students`/*!*/;
SET TIMESTAMP=1625052878/*!*/;
insert into tutors(Tname,Gender,Age)values('tye','M',34)
/*!*/;
# at 377
#210630 19:34:38 server id 1 end_log_pos 408 CRC32 0x54668fa0 Xid = 15
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
#从指定位置开始查看二进制日志
root@mail data]# mysqlbinlog --start-position=239 mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#210630 19:30:31 server id 1 end_log_pos 120 CRC32 0xb0eb839b Start: binlog v 4, server v 5.6.10-log created 210630 19:30:31 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
11XcYA8BAAAAdAAAAHgAAAABAAQANS42LjEwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAADXVdxgEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAZuD
67A=
'/*!*/;
# at 239
#210630 19:34:38 server id 1 end_log_pos 377 CRC32 0x137bdf73 Query thread_id=1 exec_time=0 error_code=0
use `students`/*!*/;
SET TIMESTAMP=1625052878/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1073741824/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
insert into tutors(Tname,Gender,Age)values('tye','M',34)
/*!*/;
# at 377
#210630 19:34:38 server id 1 end_log_pos 408 CRC32 0x54668fa0 Xid = 15
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
#通过时间查询二进制日志
[root@mail data]# mysqlbinlog --start-datetime='2021-06-30 19:34:38' mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#210630 19:30:31 server id 1 end_log_pos 120 CRC32 0xb0eb839b Start: binlog v 4, server v 5.6.10-log created 210630 19:30:31 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
11XcYA8BAAAAdAAAAHgAAAABAAQANS42LjEwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAADXVdxgEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAZuD
67A=
'/*!*/;
# at 120
#210630 19:34:38 server id 1 end_log_pos 207 CRC32 0xb42e4f72 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1625052878/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1073741824/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 207
# at 239
#210630 19:34:38 server id 1 end_log_pos 239 CRC32 0xcdacb444 Intvar
SET INSERT_ID=15/*!*/;
#210630 19:34:38 server id 1 end_log_pos 377 CRC32 0x137bdf73 Query thread_id=1 exec_time=0 error_code=0
use `students`/*!*/;
SET TIMESTAMP=1625052878/*!*/;
insert into tutors(Tname,Gender,Age)values('tye','M',34)
/*!*/;
# at 377
#210630 19:34:38 server id 1 end_log_pos 408 CRC32 0x54668fa0 Xid = 15
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
#通过flush logs滚动二进制日志,从服务器会滚动中继日志,不需要重启mysqld服务器
mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#删除二进制日志文件之前的文件
mysql> purge binary logs to 'mysql-bin.000002';
Query OK, 0 rows affected (0.01 sec)
#查看当前所拥有的二进制日志文件
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000002 | 120 |
+------------------+-----------+
1 row in set (0.00 sec)
以上是关于Linux学习-MySQL日志管理的主要内容,如果未能解决你的问题,请参考以下文章