You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Logstash JDBC插件同步MySQL到ES时遇时区错误求助

解决Logstash JDBC同步MySQL到Elasticsearch的时区配置错误

核心解决方案步骤

1. 配置Windows系统时区环境变量

错误提示明确指出无法从nil确定时区,Windows系统下需显式设置TZ环境变量:

  • 临时生效:启动Logstash前,在命令行执行:
    set TZ=Asia/Kathmandu
    
    再运行启动命令:
    .\logstash.bat -f ./config/elastic_mysql_sample.conf
    
  • 永久生效:打开系统属性→高级→环境变量,新建系统变量TZ,值设为Asia/Kathmandu,重启Logstash后生效。

2. 修正MySQL JDBC驱动类

MySQL 8.0版本的驱动类已更新,替换旧驱动类配置:
将input块中的jdbc_driver_class => "com.mysql.jdbc.Driver"改为:

jdbc_driver_class => "com.mysql.cj.jdbc.Driver"

3. 移除或修正错误的Date过滤器

Logstash自动生成的@timestamp是ISO标准格式(如2023-07-25T12:23:41.023Z),你的date过滤器试图匹配YYYY-MM-DD HH:mm:ss,SSS格式的@timestamp属于错误用法:

  • 如果无需处理数据库中的时间字段,直接删除整个filter块;
  • 如果需要解析数据库中的时间字段(如create_time),修改filter为:
    filter {
      date {
        match => ["create_time", "YYYY-MM-dd HH:mm:ss,SSS"] # 替换为你数据库中的时间字段名
        timezone => "Asia/Kathmandu"
        target => "@timestamp"
      }
    }
    

4. 修正Elasticsearch输出的hosts配置

输出块中hosts末尾的斜杠多余,改为:

hosts => ["localhost:9200"]

修正后的完整配置文件

input {
  jdbc {
    clean_run => true
    jdbc_driver_library => "D:/softs/mysql-connector-java-8.0.30.jar"
    jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
    jdbc_connection_string => "jdbc:mysql://localhost:3306/elastic_mysql_sample?serverTimezone=Asia/Kathmandu"
    jdbc_user => "root"
    jdbc_password => "root"
    statement => "SELECT * FROM elastic_mysql_sample.employee_sample WHERE logstash_tracking_id > :sql_last_value"
    use_column_value => true
    tracking_column => "logstash_tracking_id"
    tracking_column_type => "numeric"
    jdbc_default_timezone => "Asia/Kathmandu"
  }
}

# 若无需处理时间字段,可删除以下filter块
filter {
  date {
    match => ["create_time", "YYYY-MM-dd HH:mm:ss,SSS"] # 替换为实际数据库时间字段
    timezone => "Asia/Kathmandu"
    target => "@timestamp"
  }
}

output {
  elasticsearch {
    hosts => ["localhost:9200"]
    index => "employee_idx"
  }
}

问题根源说明

  1. Windows系统下Logstash依赖TZ环境变量识别时区,默认未设置时会触发时区解析错误;
  2. MySQL 8.0驱动类与旧版本不兼容,使用错误驱动类会引发初始化异常;
  3. Date过滤器错误处理Logstash自动生成的@timestamp字段,格式不匹配进一步加剧时区解析问题。

内容的提问来源于stack exchange,提问作者Satya Raj Awasthi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 07:26:11