使用logstash-6.2.2和logstash-input-jdbc插件实现mysql数据同步到Elasticsearch

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010180738/article/details/79468673

首先需要安装Elasticsearch,安装方法在上一篇文章中有了
http://blog.csdn.net/u010180738/article/details/79423341

logstash的安装和elasticsearch版本需要一致,这里安装6.2.2最新版本的,需要jdk1.8版本及以上,注意elasticsearch插件2.X和6.X版本相差较大,并且没有向下兼容。
参数和配置需要以官网为准。
logstash下载
https://www.elastic.co/cn/downloads/logstash

下载6.2.2版本的,tar.GZ架包
上传至Linux服务器安装目录下,这里是/opt/elasticsearch/logstash-6.2.2
tar -xvf logstash-6.2.2.tar.gz
测试是否安装成功:
在logstash的bin目录下运行
./logstash -e ‘input { stdin { } } output { stdout {} }’
这里写图片描述
如果出现上图内容,则安装logstash成功

安装logstash-input-jdbc,logstash-input-jdbc插件使用ruby语音开发的,所以需要安装ruby环境,将镜ruby仓库镜像替换淘宝的。
判断是否安装gem

gem –v

提示非法命令则未安装
安装gem

sudo yum install gem 

(使用root用户不需要加sudo)建议切换到root账户安装
出现提示输入y即可

Total download size: 3.8 M
Installed size: 13 M
Is this ok [y/d/N]: y

安装成功后查看gem版本号
gem -v
这里安装的是2.0.14.1
ruby仓库镜像替换淘宝

gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/

查看当前镜像源

gem sources -l

这里写图片描述
显示ruby.taobao.org则正常
请确保只有 ruby.taobao.org
如果 还是显示 https://rubygems.org/ 进入 home的 .gemrc 文件
sudo vim ~/.gemrc
手动删除 https://rubygems.org/
退出root账户,登录Elasticsearch账户es,上一篇文章介绍了如何新增账户。

修改Gemfile的数据源地址
logstash 文件下的配置Gemfile,修改 source 的值 为: “https://ruby.taobao.org
vim Gemfile

这里写图片描述
修改为:
这里写图片描述

修改 Gemfile.lock配置,找到 remote 修改它的值为: https://ruby.taobao.org
vim Gemfile.lock

这里写图片描述
修改为
这里写图片描述
到logstash的bin目录下运行下面的命令,查看可用的插件

./logstash-plugin list --verbose

这里写图片描述
这里写图片描述
这里可以看到可以使用的logstash-input-jdbc版本是4.3.3
elk对版本要求很严格,必须使用正确的版本。
logstash-input-jdbc可用版本为4.3.3
对应官方文档
https://www.elastic.co/guide/en/logstash-versioned-plugins/current/v4.3.3-plugins-inputs-jdbc.html
https://github.com/logstash-plugins/logstash-input-jdbc/archive/v4.3.3.zip

按照成功后,测试下,需要一个mysql驱动包,sql文件,以及conf配置文件
新建一个sql文件
bank_sync.sql

SELECT
        t.id,
        t.`code`,
        t.`name`,
        t.per_day_limit
FROM
        tb_bank_type t

mysql.conf文件

input {

    jdbc {
      # mysql jdbc connection string to our backup databse
      jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/carinsurance"
      # the user we wish to excute our statement as
      jdbc_user => "carinsurance"
      jdbc_password => "123456"
      # the path to our downloaded jdbc driver
      jdbc_driver_library => "/opt/elasticsearch/logstash-6.2.2/sql/mysql-connector-java-5.1.40.jar"
      # the name of the driver class for mysql
      jdbc_driver_class => "com.mysql.jdbc.Driver"
      jdbc_paging_enabled => "true"
      jdbc_page_size => "50000"
      statement_filepath => "/opt/elasticsearch/logstash-6.2.2/sql/bank_sync.sql"
      schedule => "*/1 * * * *"
      type => "jdbc"
    }
}

filter {
    json {
        source => "message"
        remove_field => ["message"]
    }
}

output {
    elasticsearch {
        hosts => "127.0.0.1:9200"
        index => "bank"
        document_id => "%{id}"
    }
    stdout {
        codec => json_lines
    }
}

因为当时参考的一篇文章,安装的估计是2.X版本的,而我安装的是最新的6.2.2版本的,遇到的问题还是挺多的
报错:
Unknown setting ‘host’ for elasticsearch
Failed to execute action {:action=>LogStash::PipelineAction::Create/pipeline_id:main, :exception=>”LogStash::ConfigurationError”, :message=>”Something is wrong with your configuration.”

这里写图片描述

这是因为output 配置出错,6.2.2的不再使用host,port方式的配置了,后面参考了官方文档,是使用hosts方式的配置了

报错
A plugin had an unrecoverable error. Will restart this plugin
Error: invalid weekday expression (?)
Exception: ArgumentError
这里写图片描述
很明显,一个正则写错了,不像我们经常用的那种占位6个的从秒开始的,这里的是从分开始的。

报错:
Could not index event to Elasticsearch. {:status=>400, :action=>[“index”,
invalid_index_name_exception
这里写图片描述
很明显是index名称非法,是不能包含大写字母,不能下划线之类的

上传mysql.conf(logstash bin目录中),bank_sync.sql,mysql驱动包到指定文件目录
在logstash的bin目录中启动
./logstash -f mysql.conf
后台启动: nohup ./logstash -f mysql.conf > /dev/null 2>&1 &

这里写图片描述
应用成功
kibana中查询数据

GET /bank/_search

这里写图片描述

参考资料:http://blog.csdn.net/yeyuma/article/details/50240595
http://blog.csdn.net/q15150676766/article/details/75949679

猜你喜欢

转载自blog.csdn.net/u010180738/article/details/79468673