mysql版本5.7
1.查看是否开启bin_log
show global variables like’log_bin’;
off的话需要先开启
在mysql的文件夹目录中找到my.ini
加一行log-bin=”C:/ProgramData/MySQL/MySQL Server 5.7/logs/log-bin”
并提前创建好目录
2.数据库会把日志放进logs目录中
3.查看log日志
SHOW BINARY LOGS;
查看log-bin.000001文件指定时间的log
(要运行mysqlbinlog
命令,您需要在操作系统的命令行界面(例如Windows的命令提示符或PowerShell,Linux或macOS的终端)中执行,而不是在MySQL命令行客户端或任何SQL管理工具中。)
mysqlbinlog –no-defaults –base64-output=decode-rows -v –database=”ezhizao_yzbh_ggy” –start-datetime=”2024-12-04 11:00:00″ –stop-datetime=”2025-07-17 12:00:00″ “log-bin.000001”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | C:ProgramDataMySQLMySQL Server 5.7logs>mysqlbinlog --no-defaults --base64-output=decode-rows -v --database= "ezhizao_yzbh_ggy" -- start-datetime = "2024-12-04 11:10:00" -- stop-datetime = "2025-07-17 12:00:00" "log-bin.000001" /*!50530 SET @ @SESSION .PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE =@ @COMPLETION_TYPE ,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #241203 16:50:52 server id 1 end_log_pos 123 CRC32 0xd1b0f8d9 Start: binlog v 4, server v 5.7.36-log created 241203 16:50:52 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; # at 1247 #241204 11:11:55 server id 1 end_log_pos 1312 CRC32 0x7bbf9070 Anonymous_GTID last_committed=3 sequence_number=4 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @ @SESSION .GTID_NEXT= 'ANONYMOUS' /*!*/; # at 1312 #241204 11:11:55 server id 1 end_log_pos 1396 CRC32 0xd0da4e48 Query thread_id=5 exec_time=0 error_code=0 SET TIMESTAMP=1733281915/*!*/; SET @ @session .pseudo_thread_id=5/*!*/; SET @ @session .foreign_key_checks=1, @ @session .sql_auto_is_null=0, @ @session .unique_checks=1, @ @session .autocommit=1/*!*/; SET @ @session .sql_mode=1436549152/*!*/; SET @ @session .auto_increment_increment=1, @ @session .auto_increment_offset=1/*!*/; /*!C utf8mb4 *//*!*/; SET @ @session .character_set_client=45,@ @session .collation_connection=45,@ @session .collation_server=8/*!*/; SET @ @session .lc_time_names=0/*!*/; SET @ @session .collation_database= DEFAULT /*!*/; BEGIN /*!*/; # at 1396 #241204 11:11:55 server id 1 end_log_pos 1493 CRC32 0xaa53947b Table_map: `ezhizao_yzbh_ggy`.`fxy_financial_voucher_template` mapped to number 140 # at 1493 #241204 11:11:55 server id 1 end_log_pos 1591 CRC32 0x86714204 Update_rows: table id 140 flags: STMT_END_F ### UPDATE `ezhizao_yzbh_ggy`.`fxy_financial_voucher_template` ### WHERE ### @1=12 ### @2='12' ### @3=b'0' ### @4=1 ### @5=1 ### @6=2 ### @7=222 ### SET ### @1=12 ### @2='12' ### @3=b'0' ### @4=1 ### @5=1 ### @6=555 ### @7=222 # at 1591 #241204 11:11:55 server id 1 end_log_pos 1622 CRC32 0x32e198c4 Xid = 292 COMMIT/*!*/; SET @ @SESSION .GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/; DELIMITER ; # End of log file /*!50003 SET COMPLETION_TYPE= @OLD_COMPLETION_TYPE */; /*!50530 SET @ @SESSION .PSEUDO_SLAVE_MODE=0*/; |
整理了下常用的配置项
-
启用二进制日志:
12[mysqld]
log_bin = mysql-bin
log_bin
指定了二进制日志文件的前缀名称。日志文件将以这个前缀开始,后跟一个数字后缀。 -
设置日志格式:
12[mysqld]
binlog_format = ROW | STATEMENT | MIXED
-
ROW
:每行变化都记录在日志中。 -
STATEMENT
:记录执行的SQL语句。 -
MIXED
:结合ROW
和STATEMENT
,根据情况选择最合适的模式。
-
-
指定日志文件大小:
12[mysqld]
max_binlog_size = 100M
max_binlog_size
指定了单个binlog文件的最大尺寸。当达到这个大小时,MySQL将创建一个新的日志文件。 -
二进制日志缓存大小:
12[mysqld]
binlog_cache_size = 32K
binlog_cache_size
指定了事务日志缓存的大小,用于ROW
格式的binlog。 -
二进制日志索引文件:
12[mysqld]
log_bin_index = mysql-bin.index
log_bin_index
指定了二进制日志索引文件的名称,该文件记录了所有当前活跃和旧的binlog文件的位置。 -
同步二进制日志到磁盘:
12[mysqld]
binlog_sync = 1
binlog_sync
控制何时将事务日志从缓存同步到磁盘。1表示每次事务后都同步,这会降低性能但确保数据安全。 -
二进制日志过期天数:
12[mysqld]
expire_logs_days = 7
expire_logs_days
指定了binlog文件在被自动删除前可以保留的天数。 -
最大二进制日志文件数量:
12[mysqld]
max_binlog_files = 100
max_binlog_files
指定了服务器将保留的最大binlog文件数量。当超过这个数量时,最旧的文件将被删除。 -
启用GTID模式:
12[mysqld]
gtid_mode = ON
gtid_mode
启用全局事务标识符(GTID),用于复制和恢复。 -
启用自动位置同步:
12[mysqld]
log_slave_updates = 1
log_slave_updates
允许从服务器将复制的更新记录到自己的binlog中。
总结
到此这篇关于开启mysql的binlog日志的文章就介绍到这了,更多相关开启mysql的binlog日志内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!