1. 程式人生 > >elasticsearch5.2.1使用logstash同步mysql

elasticsearch5.2.1使用logstash同步mysql

oot con href move stdout java fresh clipboard tail

centos 本人親測可以 技術分享圖片技術分享圖片 首先安裝好mysql,elasticsearch 不懂的請參考另一篇文章

安裝logstash
官方:https://www.elastic.co/guide/en/logstash/current/installing-logstash.html
1.下載公共密鑰
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
2.添加yum源
vim /etc/yum.repos.d/logstash.repo
文件中寫入
[logstash-5.x]
name=Elastic repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
保存退出
3.使用yum安裝
yum install logstash
4.驗證是否安裝成功
進入 logstash 安裝目錄
cd /usr/share/logstash
運行
bin/logstash -e ‘input { stdin { } } output { stdout {} }‘
等待幾秒鐘 出現
The stdin plugin is now waiting for input:
然後輸入
hello world
得到類似的結果
2016-11-24T08:01:55.949Z bogon hello world

安裝logstash-input-jdbc插件

1.安裝 ruby 和 rubygems(註意:需要 ruby 的版本在 1.8.7 以上)
# yum install -y ruby rubygems
檢查 ruby 版本:
# ruby -v
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
替換國內的鏡像
gem sources --remove http://rubygems.org/

gem sources -a http://gems.ruby-china.org/
驗證是否成功
gem sources -l
修改Gemfile的數據源地址
whereis logstash # 查看logstash安裝的位置, 我的在 /usr/share/logstash目錄

cd /usr/share/logstash
vim Gemfile
修改 source 的值 為: "https://gems.ruby-china.org/"

vim Gemfile.jruby-1.9.lock

# 找到 remote 修改它的值為:https://gems.ruby-china.org/

開始安裝

./bin/logstash-plugin install --no-verify logstash-input-jdbc

2.寫配置文件開始同步 vi xxx.conf

[html] view plain copy
  1. input {
  2. jdbc {
  3. type => "user"
  4. jdbc_connection_string => "jdbc:mysql://192.168.33.101:3306/test"
  5. jdbc_user => "root"
  6. jdbc_password => "123456"
  7. jdbc_driver_library => "/usr/share/logstash/mysql-connector-java-5.1.41.jar"
  8. jdbc_driver_class => "com.mysql.jdbc.Driver"
  9. jdbc_paging_enabled => "true"
  10. jdbc_page_size => "1000"
  11. statement => "select * from user"
  12. schedule => "* * * * *"
  13. }
  14. jdbc {
  15. type => "task"
  16. jdbc_connection_string => "jdbc:mysql://192.168.33.101:3306/test"
  17. jdbc_user => "root"
  18. jdbc_password => "123456"
  19. jdbc_driver_library => "/usr/share/logstash/mysql-connector-java-5.1.41.jar"
  20. jdbc_driver_class => "com.mysql.jdbc.Driver"
  21. jdbc_paging_enabled => "true"
  22. jdbc_page_size => "1000"
  23. statement => "select * from task"
  24. schedule => "* * * * *"
  25. }
  26. }
  27. filter{
  28. mutate{
  29. remove_field => [ "@timestamp", "@version"]
  30. }
  31. }
  32. output {
  33. if [type] == "user" {
  34. elasticsearch {
  35. hosts => ["192.168.33.111:9200","192.168.33.112:9200","192.168.33.113:9200"]
  36. index => "testindex"
  37. document_type => "user"
  38. document_id => "%{id}"
  39. user => "elastic"
  40. password => "changeme"
  41. }
  42. }
  43. if [type] == "task" {
  44. elasticsearch {
  45. hosts => ["192.168.33.111:9200","192.168.33.112:9200","192.168.33.113:9200"]
  46. index => "testindex"
  47. document_type => "task"
  48. document_id => "%{id}"
  49. user => "elastic"
  50. password => "changeme"
  51. }
  52. }
  53. }


執行:bin/logstash -f xxx.conf ok 大功告成!!!

elasticsearch5.2.1使用logstash同步mysql