使用logstash同步mysql數據到elasticsearch實現
mysql建表test;
安裝logstash(跟es版本一致)
# 下載 wget https://repo.huaweicloud.com/logstash/7.14.2/logstash-7.14.2-linux-x86_64.tar.gz # 解壓 tar -zxvf logstash-7.14.2-linux-x86_64.tar.gz # 需要mysql-connector-java-5.1.40.jar,隨便放到比如目錄 # /var/lib/hadoop-hdfs/logstash-7.14.2/lib/mysql-connector-java-5.1.40.jar
新建es索引test
curl -u elastic:changeme -X PUT http://192.168.20.130:9200/test -H 'Content-Type: application/json' -d' { "settings" : { "number_of_shards" : 1, "number_of_replicas" : 1 }, "mappings" : { "properties": { "id": { "type" : "long" }, "type": { "type": "keyword" }, "keyword_1": { "type": "text", "analyzer" : "ik_smart" }, "keyword_2": { "type": "text", "analyzer" : "ik_smart" }, "keyword_3": { "type": "text", "analyzer" : "ik_smart" }, "data": { "type": "keyword" }, "created_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd" }, "updated_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd" } } } }'
編輯配置文件
vim ~/script/logstash/logstash_mysql2es.conf
input { stdin{ } jdbc{ # 連接的數據庫地址和數據庫,指定編碼格式,禁用ssl協議,設定自動重連 # 此處10.112.103.2為MySQL所在IP地址,也是elastic search所在IP地址 jdbc_connection_string => "jdbc:mysql://192.168.13.28:3306/test?characterEncoding=UTF-8&useSSL=FALSE&autoReconnect=true" #數據庫用戶名 jdbc_user => "root" # 數據庫用戶名對應的密碼 jdbc_password => "root" # jar包存放位置 jdbc_driver_library => "/var/lib/hadoop-hdfs/logstash-7.14.2/lib/mysql-connector-java-5.1.40.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_default_timezone => "Asia/Shanghai" jdbc_paging_enabled => "true" jdbc_page_size => "320000" lowercase_column_names => false statement => "select id, type, tags, title from test" } } filter { # 移除無關的字段 mutate { remove_field => ["@version", "@timestamp"] } } output { elasticsearch { hosts => ["http://192.168.20.130:9200"] user => "elastic" password => "changeme" index => "test" document_type => "_doc" # 將字段type和id作為文檔id document_id => "%{type}_%{id}" } stdout { codec => json_lines } }
重要配置參數說明:
remove_field => ["@version", "@timestamp"]
: 默認logstash
會添加這兩個字段,這裡去掉;document_id => "%{type}_%{id}"
: 將兩個字段拼接作為es的文檔id;
啟動任務
./logstash-7.14.2/bin/logstash -f script/logstash/logstash_mysql2es.conf
參考資料
- Jdbc input plugin
- Elasticsearch output plugin
- 通過logstash將mysql數據同步到elastic search
以上就是使用logstash同步mysql數據到elasticsearch實現的詳細內容,更多關於logstash mysql數據同步elasticsearch的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- logstash將mysql數據同步到elasticsearch方法詳解
- 基於Docker搭建ELK 日志系統的方法
- MySql 8.0及對應驅動包匹配的註意點說明
- 升級到mysql-connector-java8.0.27的註意事項
- 一文秒懂logstash收集springboot日志的方法