Logstash安装

logstash是做数据采集的,类似于flume。

官网logstash介绍

下载地址

解压后执行一下命令,查看效果:

1
bin/logstash -e 'input { stdin { } } output { stdout {} }'

安装logstash-input-jdbc

参考

logstash-input-jdbc插件是logstash 的一个个插件。

使用ruby语言开发. 安装gem, 替换淘宝镜像

1
2
3
4
5
6
安装gem
sudo apt install gem
sudo apt install ruby
gem -v
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
gem sources -l

RubyGems镜像站

  • 查看logstash可用插件
1
bin/logstash-plugin list --verbose

以上找到对应的版本. 查看相应的文档https://www.elastic.co/guide/en/logstash-versioned-plugins/current/v4.3.9-plugins-inputs-jdbc.html

  • 安装命令
1
bin/logstash-plugin install logstash-input-jdbc

使用

实现mysql数据同步到Elasticsearch

需要一个mysql驱动包,sql文件,以及conf配置文件

  • sql文件

bank_sync.sql

1
2
3
4
5
6
7
SELECT
t.id,
t.`code`,
t.`name`,
t.per_day_limit
FROM
tb_bank_type t
  • mysql.conf文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
}
}

上面配置文件中的sql文件和mysql驱动注意路径正确

  • 启动logstash
1
2
3
bin/logstash -f mysql.conf 
后台启动: nohup ./logstash -f mysql.conf > /dev/null 2>&1 &
nohup bin/logstash -f sync-data/mysql.conf &

注意: es需要外网访问,同kibana,需要配置如下:

1
network.host: 0.0.0.0

配置以上后出现问题:

  • 系统最大文件描述符限制,最大虚拟内存限制

解决:

1
2
3
4
5
6
7
8
vi /etc/security/limits.conf 
将65535 改为65536
root用户执行以下:
vi /etc/sysctl.conf
添加一下配置
vm.max_map_count=655360
使其生效
sysctl -p

应用

【技术实验】mysql准实时同步数据到Elasticsearch

全文搜索引擎 Elasticsearch (三)logstash-input-jdbc同步数据 到elasticsearch