Table of Contents
一、日志文件类型" >一、日志文件类型
2、慢速查询日志(slow_query_log)" >2、慢速查询日志(slow_query_log)
3、二进制日志(log_bin)" >3、二进制日志(log_bin)
1)二进制文件介绍" >1)二进制文件介绍
2)二进制文件的功能" >2)二进制文件的功能
3)二进制文件的存储路径" >3)二进制文件的存储路径
Home Database Mysql Tutorial MySqlStudy之--MySql日志管理

MySqlStudy之--MySql日志管理

Jun 07, 2016 pm 02:55 PM
mysql log manage

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)
Copy after login

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</strong>   
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  
<strong>[root@rh6 ~]# more /var/log/mysqld.log</strong> 
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
Copy after login

2、慢速查询日志(slow_query_log)

    用--log-slow-queries[=file_name]选项启动时,mysqld写一个包含所有执行时间超过long_query_time秒的SQL语句的日志文件。获得初使表锁定的时间不算作执行时间。
Copy after login
    如果没有给出file_name值, 默认未主机名,后缀为-slow.log。如果给出了文件名,但不是绝对路径名,文件则写入数据目录。
Copy after login
语句执行完并且所有锁释放后记入慢查询日志。记录顺序可以与执行顺序不相同。
Copy after login
慢查询日志可以用来找到执行时间长的查询,可以用于优化。但是,检查又长又慢的查询日志会很困难。要想容易些,你可以使用mysqldumpslow命令获得日志中显示的查询摘要来处理慢查询日志。
Copy after login
    在MySQL 5.1的慢查询日志中,不使用索引的慢查询同使用索引的查询一样记录。要想防止不使用索引的慢查询记入慢查询日志,使用--log-short-format选项。
Copy after login
    在MySQL 5.1中,通过--log-slow-admin-statements服务器选项,你可以请求将慢管理语句,例如OPTIMIZE TABLE、ANALYZE TABLE和 ALTER TABLE写入慢查询日志。
Copy after login
用查询缓存处理的查询不加到慢查询日志中,因为表有零行或一行而不能从索引中受益的查询也不写入慢查询日志。
Copy after login
  1. 默认情况下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)  
<pre class="html" name="code">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)
Copy after login

查看慢查询日志:

[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    Argument
Copy after login

3、二进制日志(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  
<strong>开启二进制日志:</strong>
[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
Copy after login


开启二进制日志后:

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)
Copy after login
[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
Copy after login

查看二进制文件内容:

mysql-bin.index 是二进制文件的索引,一般不能更改

[root@rh6 mysql]# cat mysql-bin.index
./mysql-bin.000001
./mysql-bin.000002
Copy after login

通过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 <a target=_blank href="mailto:COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;">COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;  
</a></strong>  
<strong>[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 <a target=_blank href="mailto:COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;">COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
</a></strong>
[html] view plaincopyprint?
<strong>[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]# </strong>  
<strong>[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*/;
Copy after login

如果每天都会生成大量的二进制日志,这些日志长时间不清理的话,将会对磁盘空间带来很大的浪费,所以定期清理日志是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</span>
Copy after login

--可以看到日志又重新从000001开始


2)PURGE MASTER LOGS TO & PURGE MASTER LOGS BEFORE
      执行PURGE MASTER LOGS TO 'mysql-bin.******'命令,是将'******'编号之前的所有日志进行删除
     执行PURGE MASTER LOGS BEFORE 'yyyy-mm-dd hh:mm:ss'命令,是将在'yyyy-mm-dd hh:mm:ss'时间之前的所有日志进行删除

3)EXPIRE_LOGS_DAYS
    此参数是设置日志的过期天数,过期的日志将会被自动删除,这有利于减少我们管理日志的工作量,需要修改my.cnf

    expire_logs_days=3

这里我们设定保存日志为3天,3天之后过期的日志将被自动删除


通过二进制日志恢复数据

   bin-log是记录着mysql所有事件的操作,当mysql发生灾难性错误时,可以通过bin-log做完整恢复,基于时间点的恢复,和基于位置的恢复

完整恢复:

   假定我们每天凌晨2点都会使用mysqldump备份数据库,但在第二天早上9点由于数据库出现了故障,数据无法访问,需要恢复数据,先使用昨天凌晨备份的文件进行恢复到凌晨2点的状态,在使用mysqlbinlog恢复自mysqldump备份以来的binlog
    mysql localhost mysql-bin.000001 | mysql -uroot -p
这样数据库就可以完全的恢复到崩溃前的完全状态

基于时间点的恢复:

  由于误操作,比如说删除了一张表,这时使用上面讲的完全恢复是没有用的,因为日志里面还存在误操作的语句,,我们需要的是恢复到误操作前的状态,然后跳过误操作的语句,再恢复后面操作的语句,假定我们删除了一张表的误操作发生在10:00这个时间点,我们可以使用下面的语句用备份和binlog将数据恢复到故障前

基于位置恢复:

  由于上面提到的,使用基于时间点的恢复可能出现,在一个时间点里面可能存在误操作和其他正确的操作,所以我们需要一种更为精确的恢复方式

主从复制
   mysql的复制是指将主数据库的DDL和DML操作通过二进制日志传到从服务器上,然后在从服务器上对这些日志做重新执行的操作,从而使得从服务器和主服务器保持数据的同步


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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to recover data after SQL deletes rows How to recover data after SQL deletes rows Apr 09, 2025 pm 12:21 PM

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

See all articles