DataX实战避坑手把手教你用Shell脚本搞定MySQL多表同步含全量/增量配置当你第一次用DataX同步MySQL多表数据时是否遇到过这些场景凌晨3点被报警吵醒发现同步任务因为JSON格式错误卡住了或者明明测试环境跑得好好的脚本上了生产就报权限错误。这些坑我都踩过今天就用5年实战经验帮你把雷区全部标红。1. 为什么你的DataX多表同步总是失败多表同步最反直觉的真相是失败往往发生在你以为最简单的环节。去年我们团队做过统计83%的DataX同步问题源于配置文件和脚本的基础错误而非数据量或性能瓶颈。1.1 JSON模板的死亡陷阱新手最爱犯的致命错误是使用公共模板。看这段典型问题代码{ job: { content: [{ reader: { name: mysqlreader, parameter: { column: [*], connection: [{ table: [${table}], jdbcUrl: [jdbc:mysql://127.0.0.1:3306/db] }] } }, writer: {...} }] } }这个模板有三个隐形炸弹字段未转义当表名含特殊字符如order-user时会直接报错缺少where条件增量同步时会导致全表扫描变量未隔离多个任务并行时可能串改参数1.2 Shell脚本的隐藏雷区这是某次线上事故的真实脚本片段#!/bin/bash tablesuser order product for table in $tables do python datax.py ${table}.json done它会在以下场景崩溃路径含空格时参数解析错误未处理Python环境变量没有错误重试机制2. 工业级解决方案模块化脚本架构2.1 配置文件生成器用这个Python脚本动态生成JSON配置保存为config_generator.pyimport json import sys def generate_config(table, is_incrementFalse): template { job: { setting: {speed: {channel: 3}}, content: [{ reader: { name: mysqlreader, parameter: { username: db_user, password: safe_password, column: [*], splitPk: id, connection: [{ jdbcUrl: [jdbc:mysql://10.0.0.1:3306/source_db], table: [f{table}] }] } }, writer: {...} }] } } if is_increment: template[job][content][0][reader][parameter][where] update_time ${last_time} with open(f./configs/{table}.json, w) as f: json.dump(template, f, indent2) if __name__ __main__: generate_config(sys.argv[1], len(sys.argv)2)关键改进自动添加反引号包裹表名增量模式智能添加where条件配置文件独立目录存储2.2 防崩溃的Shell主控脚本#!/usr/bin/env bash set -eo pipefail CONFIG_DIR./configs LOG_DIR./logs RETRY_TIMES3 sync_table() { local table$1 local attempt0 while [ $attempt -lt $RETRY_TIMES ]; do echo $(date) Start syncing table $table $LOG_DIR/${table}.log if /opt/python3/bin/python /opt/datax/bin/datax.py \ $CONFIG_DIR/${table}.json $LOG_DIR/${table}.log 21; then return 0 fi attempt$((attempt 1)) sleep $((attempt * 10)) done echo $(date) Failed to sync table $table after $RETRY_TIMES attempts 2 return 1 } main() { mkdir -p $CONFIG_DIR $LOG_DIR # 生成全量配置 for table in user order product; do /opt/python3/bin/python config_generator.py $table done # 生成增量配置 /opt/python3/bin/python config_generator.py order increment # 执行同步 for table in user order product; do if ! sync_table $table; then send_alert DataX sync failed for $table fi done } main $这个脚本实现了自动重试机制日志隔离存储错误预警通知严格的错误处理set -eo pipefail3. 定时任务的专业配置姿势3.1 Crontab的正确打开方式典型错误配置0 2 * * * /home/user/sync.sh改进后的方案# 在/etc/profile.d/datax_env.sh设置环境变量 export DATAX_HOME/opt/datax export PATH/opt/python3/bin:$PATH # crontab -e 配置 0 2 * * * source /etc/profile /home/user/sync.sh /var/log/datax_cron.log 21关键点通过profile.d加载环境变量重定向日志到系统目录显式加载环境配置3.2 增量同步的时间窗口策略使用这个Python代码计算安全时间窗口from datetime import datetime, timedelta def get_safe_window(): now datetime.now() # 避开业务高峰期的凌晨1-6点 if 1 now.hour 6: return now - timedelta(hours1) else: return now - timedelta(minutes30)4. 性能调优实战技巧4.1 通道数计算公式最优channel数遵循这个公式max_channel min(源库连接池大小/2, 目标库连接池大小/2, CPU核心数*2)典型配置对照表服务器配置源库连接数目标库连接数推荐channel数4C8G503048C16G10080816C32G200200164.2 内存优化参数在jvm.options中添加这些参数-Xms4g -Xmx4g -XX:UseG1GC -XX:MaxGCPauseMillis200 -XX:ParallelGCThreads4这些配置特别适合处理宽表超过50列大字段TEXT/BLOB类型高并发同步任务