ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

判断mysql 是否开启Binlog,如果没开启Binlog 该如何开启

判断mysql 是否开启Binlog,如果没开启Binlog 该如何开启

方法一:SQL 命令检查(最常用)

连接到MySQL后执行:

-- 查看 binlog 状态SHOWVARIABLESLIKE'log_bin';

输出示例:

+---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+
  • Value = ON✅ 表示已开启
  • Value = OFF❌ 表示未开启

方法二:查看详细的 Binlog 配置

-- 查看所有 binlog 相关配置SHOWVARIABLESLIKE'%bin%';

关键参数说明:

-- 查看 binlog 格式(必须是 ROW)SHOWVARIABLESLIKE'binlog_format';-- 应该显示:ROW-- 查看 binlog 文件位置SHOWVARIABLESLIKE'log_bin_basename';-- 显示:/var/lib/mysql/mysql-bin-- 查看当前正在写入的 binlog 文件SHOWMASTERSTATUS;-- 显示当前文件和位置

方法三:检查 Binlog 文件是否存在(系统级别)

在Linux系统中直接查看binlog文件:

# 查看 MySQL 数据目录mysql-e"SHOW VARIABLES LIKE 'datadir';"# 进入数据目录查看 binlog 文件ls-lh/var/lib/mysql/|grepmysql-bin

如果看到mysql-bin.000001这样的文件,说明binlog已开启。

方法四:使用 Python 脚本检查

importpymysqldefcheck_binlog_status(host,user,password,port=3306):conn=pymysql.connect(host=host,user=user,password=password,port=port)cursor=conn.cursor()# 检查 log_bincursor.execute("SHOW VARIABLES LIKE 'log_bin'")log_bin=cursor.fetchone()# 检查 binlog_formatcursor.execute("SHOW VARIABLES LIKE 'binlog_format'")binlog_format=cursor.fetchone()# 检查 binlog 文件cursor.execute("SHOW MASTER STATUS")master_status=cursor.fetchone()conn.close()return{'log_bin':log_bin[1]iflog_binelse'OFF','binlog_format':binlog_format[1]ifbinlog_formatelse'Unknown','has_binlog_files':master_statusisnotNone}# 使用示例result=check_binlog_status('127.0.0.1','root','your_password')print(f"log_bin:{result['log_bin']}")print(f"binlog_format:{result['binlog_format']}")print(f"Has binlog files:{result['has_binlog_files']}")

方法五:组合检查脚本(推荐)

创建一个完整的检查脚本:

#!/usr/bin/env python3importpymysqlimportsysdefcheck_mysql_binlog(host,user,password,port=3306):try:conn=pymysql.connect(host=host,user=user,password=password,port=port)cursor=conn.cursor()# 1. 检查 log_bincursor.execute("SHOW VARIABLES LIKE 'log_bin'")log_bin=cursor.fetchone()# 2. 检查 binlog_formatcursor.execute("SHOW VARIABLES LIKE 'binlog_format'")binlog_format=cursor.fetchone()# 3. 检查 binlog_row_imagecursor.execute("SHOW VARIABLES LIKE 'binlog_row_image'")row_image=cursor.fetchone()# 4. 检查是否有 binlog 文件cursor.execute("SHOW MASTER STATUS")master_status=cursor.fetchone()conn.close()print("="*50)print("MySQL Binlog 状态检查")print("="*50)print(f"log_bin :{log_bin[1]iflog_binelse'OFF'}")print(f"binlog_format :{binlog_format[1]ifbinlog_formatelse'Unknown'}")print(f"binlog_row_image :{row_image[1]ifrow_imageelse'Unknown'}")ifmaster_status:print(f"当前 binlog 文件 :{master_status[0]}")print(f"当前位置 :{master_status[1]}")else:print("当前 binlog 文件 : 无")print("="*50)# 判断是否满足 CDC 要求is_ready=(log_binandlog_bin[1]=='ON'andbinlog_formatandbinlog_format[1].upper()=='ROW'andmaster_statusisnotNone)ifis_ready:print("✅ MySQL 已正确配置,可以使用 CDC")else:print("❌ MySQL 未正确配置,需要修改配置")ifnotlog_binorlog_bin[1]!='ON':print(" - 需要开启 log_bin")ifnotbinlog_formatorbinlog_format[1].upper()!='ROW':print(" - 需要设置 binlog_format=ROW")ifnotmaster_status:print(" - 未找到 binlog 文件")returnis_readyexceptExceptionase:print(f"❌ 连接失败:{e}")returnFalseif__name__=="__main__":# 修改为你的 MySQL 配置check_mysql_binlog(host='127.0.0.1',user='root',password='your_password',port=3306)

如果未开启,如何开启 Binlog

1. 修改 MySQL 配置文件

找到 MySQL 配置文件(通常在/etc/mysql/my.cnf或/etc/my.cnf):

[mysqld] # 开启 binlog log-bin = mysql-bin server-id = 1 # 必须设置唯一值 binlog-format = ROW # 必须设置为 ROW binlog-row-image = FULL # 推荐设置为 FULL expire-logs-days = 7 # 日志保留7天(可选)8.4.11 以后无效 binlog_expire_logs_seconds=604800 # 新版本用这个存储保留时间 max-binlog-size = 100M # 单个文件大小(可选) # 如果你只需要监听特定数据库,可以添加 # binlog-do-db = your_database # 只记录指定数据库

2. 重启 MySQL

# Ubuntu/Debiansudosystemctl restart mysql# CentOS/RHELsudosystemctl restart mysqld# 或使用 servicesudoservicemysql restart

3. 验证配置

SHOWVARIABLESLIKE'log_bin';SHOWVARIABLESLIKE'binlog_format';SHOWMASTERSTATUS;

SHOW VARIABLES LIKE ‘%bin%’;

bind_address *
binlog_cache_size 32768
binlog_checksum CRC32
binlog_direct_non_transactional_updates OFF
binlog_encryption OFF
binlog_error_action ABORT_SERVER
binlog_expire_logs_seconds 2592000
binlog_format ROW
binlog_group_commit_sync_delay 0
binlog_group_commit_sync_no_delay_count 0
binlog_gtid_simple_recovery ON
binlog_max_flush_queue_time 0
binlog_order_commits ON
binlog_rotate_encryption_master_key_at_startup OFF
binlog_row_event_max_size 8192
binlog_row_image FULL
binlog_row_metadata MINIMAL
binlog_row_value_options
binlog_rows_query_log_events OFF
binlog_stmt_cache_size 32768
binlog_transaction_dependency_history_size 25000
binlog_transaction_dependency_tracking COMMIT_ORDER
innodb_api_enable_binlog OFF
log_bin ON
log_bin_basename /var/lib/mysql/binlog
log_bin_index /var/lib/mysql/binlog.index
log_bin_trust_function_creators OFF
log_bin_use_v1_row_events OFF
log_statements_unsafe_for_binlog ON
max_binlog_cache_size 18446744073709547520
max_binlog_size 1073741824
max_binlog_stmt_cache_size 18446744073709547520
mysqlx_bind_address *
sql_log_bin ON
sync_binlog 1

RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进