二进制日志:记录数据上任何数据修改或潜在影响数据内容的都会被记录在中!
每个用户链接就是一个线程,每个用户请求修改不是同一张表,那意味这第一个用户锁定第一张表执行写操作,第二个用户锁定第二章表执行写操作也一样可以同时进行
SHOW MASTER STATUS;可以查看主从日志状态,已知滚动到00006!
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000006 | 1582 | | |
+------------------+----------+--------------+------------------+
当两个线程同时写数据,二进制日志将成为资源争用,于是当每个线程先写入到缓冲区,通过后台线程自动从缓冲区同步日志信息到日志中!而日志不会过大,取决于滚动的大小,
滚动可根据大小和时间进行滚动,重启mysql或者执行FLUSH LOGS也会滚动!FLUSH LOGS对错误日志么有太大意义,只针对二进制日志
MariaDB [(none)]> FLUSH LOGS;
Query OK, 0 rows affected (0.04 sec)
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 | 245 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
查看服务器当前被使用的,能够被mysql服务管理的二进制文件列表,如下:
MariaDB [(none)]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 30379 |
| mysql-bin.000002 | 1038814 |
| mysql-bin.000003 | 264 |
| mysql-bin.000004 | 245 |
| mysql-bin.000005 | 264 |
| mysql-bin.000006 | 1625 |
| mysql-bin.000007 | 245 |
+------------------+-----------+
7 rows in set (0.00 sec)
MariaDB [(none)]>
二进制日志,可根据时间点做基于时间点的数据恢复,也可以做主从复制,通常我们会备份这些二进制日志!
mysql-bin.index也是二进制文件的索引
[root@mysql-master mysql]# cat mysql-bin.index
./mysql-bin.000001
./mysql-bin.000002
./mysql-bin.000003
./mysql-bin.000004
./mysql-bin.000005
./mysql-bin.000006
./mysql-bin.000007
[root@mysql-master mysql]# file mysql-bin.index
mysql-bin.index: ASCII text
[root@mysql-master mysql]#
MariaDB [(none)]> SHOW BINLOG EVENTS IN 'mysql-bin.000002'G
*************************** 100. row ***************************
Log_name: mysql-bin.000001
Pos: 30360
Event_type: Stop
Server_id: 1
End_log_pos: 30379
Info:
100 rows in set (0.00 sec)
Server_id:服务器身份标识Event_type: 事件类型info:GTID
Mysql记录二进制日志的格式有两种:1,基于语句记录,statement2,基于行,row基于行记录数据较为精确,但是数据量过大,如果基于语句记录数据库较小,但是数据会产生一些不精确的信息, 介于如此则出现了第三中,混合模型:混合模型:Mixed,自动判断基于行还是基于语句
SHOW EVENTS查看
MariaDB [(none)]> SHOW BINLOG EVENTS IN 'mysql-bin.000001'G
*************************** 6129. row ***************************
Log_name: mysql-bin.000002
Pos: 1038587
Event_type: Query
Server_id: 1
End_log_pos: 1038725
Info: use `mysql`; insert into help_relation (help_topic_id,help_keyword_id) values (468,463)
MariaDB [(none)]> SHOW BINLOG EVENTS IN 'mysql-bin.000002' FROM 1038725;
+------------------+---------+------------+-----------+-------------+--------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+---------+------------+-----------+-------------+--------+
| mysql-bin.000002 | 1038725 | Query | 1 | 1038795 | COMMIT |
| mysql-bin.000002 | 1038795 | Stop | 1 | 1038814 | |
+------------------+---------+------------+-----------+-------------+--------+
2 rows in set (0.00 sec)
MariaDB [(none)]>
Mysqlbinlog查看常用选项:
mysqlbinlog --start-time
mysqlbinlog --stop-time
mysqlbinlog --start-position
mysqlbinlog --stop-position
[root@mysql-master mysql]# mysqlbinlog --start-position=1038587 mysql-bin.000002 > /mnt/002.sql
[root@mysql-master mysql]# cat /mnt/002.sql
/*!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
#150917 2:51:35 server id 1 end_log_pos 245 Start: binlog v 4, server v 5.5.44-MariaDB-log created 150917 2:51:35 at startup
ROLLBACK/*!*/;
BINLOG '
J436VQ8BAAAA8QAAAPUAAAAAAAQANS41LjQ0LU1hcmlhREItbG9nAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAnjfpVEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAADfWQRg==
'/*!*/;
# at 1038587
#150917 2:51:35 server id 1 end_log_pos 1038725 Query thread_id=1 exec_time=0 error_code=0
use `mysql`/*!*/;
SET TIMESTAMP=1442483495/*!*/;
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=0/*!*/;
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 help_relation (help_topic_id,help_keyword_id) values (468,463)
/*!*/;
# at 1038725
#150917 2:51:35 server id 1 end_log_pos 1038795 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1442483495/*!*/;
COMMIT
/*!*/;
# at 1038795
#150917 2:51:35 server id 1 end_log_pos 1038814 Stop
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@mysql-master mysql]#
二进制日志文件内容格式:1,事件发生日期和时间2,服务器ID3,事件的结束位置4,事件的类型5,原服务器生产此事件时的线程ID6,语句的时间戳和写入二进制日志文件的时间差 7,错误代码 8,事件内容9,事件位置,相当于下一个时间的开始位置如下:
# at 1038725
#150917 2:51:35 server id 1 end_log_pos 1038795 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1442483495/*!*/;
语句本身COMMIT/!/; 注释# at 1038795
事件结束位置,同样是事件下一次的开始位置
150917 2:51:35 server id 1 end_log_pos 1038814 Stop
分开说明#150917 2:51:35
:时间server id 1
:ID号end_log_pos 1038795
:结束位置
Query
:事件类型
thread_id=1:
时间源服务器线程ID号
线程ID号 SHOW PROCESSLIST;
MariaDB [(none)]> SHOW PROCESSLIST;
+----+------+-----------+------+---------+------+-------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+------+-----------+------+---------+------+-------+------------------+----------+
| 24 | root | localhost | NULL | Query | 1 | NULL | SHOW PROCESSLIST | 0.000 |
+----+------+-----------+------+---------+------+-------+------------------+----------+
1 row in set (0.16 sec)
MariaDB [(none)]>
exec_time=0
语句的时间戳和写入二进制日志文件中的时间差,单位为妙
error_code=0
错误代码
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%log%';
| log_bin | ON
是否开启二进制日志| log_bin_trust_function_creators | OFF
控制创建存储函数时,如果会导致不安全的记录,二进制禁止创建存储函数| sql_log_bin | ON
控制当前会话的二进制日志信息是否记录进日志文件中,默认为ON| sql_log_off | OFF
控制是否禁止将一般查询信息,记录到查询日志中,和sql_log_bin无关| sync_binlog | 0
表示不同步,不依赖时间控制,不基于时间,基于事件同步
| sync_relay_log | 0
| sync_relay_log_info | 0
服务器参数:| log-bin {ON|OFF} ,
也可以是文件路径,修改完路径后需要重新修改为mysql.mysql组,但是这样的日志在第一次安装的时候进行配置的,另外,数据目录和日志目录不宜放在一台设备!
| log_bin_trust_function_creators
| sql_log_bin
| sql_log_off
| sync_binlog
| sync_relay_log
| sync_relay_log_info
| max_binlog_cache_size
二进制日志缓冲空间大小,5.5以后仅用于缓冲事务的语句 | max_binlog_stmt_cache_size
非事务类和事务类公用的大小,单位都是字节
log-bin和sql_log_bin
中继日志| relay_log | relay_log_index | relay_log_info_file | relay-log.info
中继日志信息
| relay_log_purge | ON
设定对不在使用的中继日志是否启用自动清理功能
| relay_log_recovery | OFF
中继日志自动恢复相关
| relay_log_space_limit | 0
空间大小是否有限定