方法一:SQL 命令检查(最常用)
连接到MySQL后执行:
sql
-- 查看 binlog 状态
SHOW VARIABLES LIKE 'log_bin';
输出示例:
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
Value = ON✅ 表示已开启Value = OFF❌ 表示未开启
方法二:查看详细的 Binlog 配置
sql
-- 查看所有 binlog 相关配置
SHOW VARIABLES LIKE '%bin%';
关键参数说明:
sql
-- 查看 binlog 格式(必须是 ROW)
SHOW VARIABLES LIKE 'binlog_format';
-- 应该显示:ROW
-- 查看 binlog 文件位置
SHOW VARIABLES LIKE 'log_bin_basename';
-- 显示:/var/lib/mysql/mysql-bin
-- 查看当前正在写入的 binlog 文件
SHOW MASTER STATUS;
-- 显示当前文件和位置
方法三:检查 Binlog 文件是否存在(系统级别)
在Linux系统中直接查看binlog文件:
bash
# 查看 MySQL 数据目录
mysql -e "SHOW VARIABLES LIKE 'datadir';"
# 进入数据目录查看 binlog 文件
ls -lh /var/lib/mysql/ | grep mysql-bin
如果看到 mysql-bin.000001 这样的文件,说明binlog已开启。
方法四:使用 Python 脚本检查
python
import pymysql
def check_binlog_status(host, user, password, port=3306):
conn = pymysql.connect(
host=host,
user=user,
password=password,
port=port
)
cursor = conn.cursor()
# 检查 log_bin
cursor.execute("SHOW VARIABLES LIKE 'log_bin'")
log_bin = cursor.fetchone()
# 检查 binlog_format
cursor.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] if log_bin else 'OFF',
'binlog_format': binlog_format[1] if binlog_format else 'Unknown',
'has_binlog_files': master_status is not None
}
# 使用示例
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']}")
方法五:组合检查脚本(推荐)
创建一个完整的检查脚本:
python
#!/usr/bin/env python3
import pymysql
import sys
def check_mysql_binlog(host, user, password, port=3306):
try:
conn = pymysql.connect(
host=host,
user=user,
password=password,
port=port
)
cursor = conn.cursor()
# 1. 检查 log_bin
cursor.execute("SHOW VARIABLES LIKE 'log_bin'")
log_bin = cursor.fetchone()
# 2. 检查 binlog_format
cursor.execute("SHOW VARIABLES LIKE 'binlog_format'")
binlog_format = cursor.fetchone()
# 3. 检查 binlog_row_image
cursor.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] if log_bin else 'OFF'}")
print(f"binlog_format : {binlog_format[1] if binlog_format else 'Unknown'}")
print(f"binlog_row_image : {row_image[1] if row_image else 'Unknown'}")
if master_status:
print(f"当前 binlog 文件 : {master_status[0]}")
print(f"当前位置 : {master_status[1]}")
else:
print("当前 binlog 文件 : 无")
print("=" * 50)
# 判断是否满足 CDC 要求
is_ready = (
log_bin and log_bin[1] == 'ON' and
binlog_format and binlog_format[1].upper() == 'ROW' and
master_status is not None
)
if is_ready:
print("✅ MySQL 已正确配置,可以使用 CDC")
else:
print("❌ MySQL 未正确配置,需要修改配置")
if not log_bin or log_bin[1] != 'ON':
print(" - 需要开启 log_bin")
if not binlog_format or binlog_format[1].upper() != 'ROW':
print(" - 需要设置 binlog_format=ROW")
if not master_status:
print(" - 未找到 binlog 文件")
return is_ready
except Exception as e:
print(f"❌ 连接失败: {e}")
return False
if __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):
ini
[mysqld]
# 开启 binlog
log-bin = mysql-bin
server-id = 1 # 必须设置唯一值
binlog-format = ROW # 必须设置为 ROW
binlog-row-image = FULL # 推荐设置为 FULL
expire-logs-days = 7 # 日志保留7天(可选)
max-binlog-size = 100M # 单个文件大小(可选)
# 如果你只需要监听特定数据库,可以添加
# binlog-do-db = your_database # 只记录指定数据库
2. 重启 MySQL
bash
# Ubuntu/Debian
sudo systemctl restart mysql
# CentOS/RHEL
sudo systemctl restart mysqld
# 或使用 service
sudo service mysql restart
3. 验证配置
sql
SHOW VARIABLES LIKE 'log_bin';
SHOW VARIABLES LIKE 'binlog_format';
SHOW MASTER STATUS;
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