安裝 logstashjava
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.5.0.zip
解壓:mysql
unzip logstash-5.5.0.zip
以後將文件移動到本身存放的目錄便可。sql
啓動服務測試一下是否安裝成功:數據庫
cd bin ./logstash -e 'input { stdin { } } output { stdout {} }'
若是出現下面的東西就表示成功:json
輸入隨便什麼內容:vim
HelloWorld
安裝logstash-input-jdbc插件ruby
在我安裝完這東西以後,看到好像能夠不用安裝這個就能夠直接和數據庫同步的,可是因爲從新安裝太麻煩了,並且沒有其餘的服務器給我試,就沒有去試試看行不行。服務器
我主要參考的是http://blog.csdn.net/yeyuma/article/details/50240595這位老鐵的方法。app
安裝logstash是一件比較蛋疼的事,由於這東西適用ruby開發的,我對ruby這東西是一點也不懂,因此比較很差弄。elasticsearch
若是沒有gem命令的話,須要先安裝一會兒(root用戶才能夠)
替換ruby鏡像庫爲國內的庫,由於國外的庫,國內是訪問不到的,而後國內有兩個庫,兩個庫都是能夠用的:
一、替換成ruby-china的庫
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
查看是否成功
gem sources -l
二、進入logstash中的bin 修改Gemfile文件裏面的數據源
三、進入到logstash的bin下
cd bin ./plugin install logstash-input-jdbc
四、在bin下新建一個目錄config-mysql建立mysql.conf文件:
mkdir config-mysql cd config-mysql vim mysql.conf
在文件中輸入下面的內容
input { stdin { } jdbc { # 數據庫 jdbc_connection_string => "jdbc:mysql://localhost:3306/test01" # 用戶名密碼 jdbc_user => "root" jdbc_password => "123456" # jar包的位置 jdbc_driver_library => "/usr/local/logstash-5.5.0/mysql-connector-java-6.0.6.jar" # mysql的Driver jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_paging_enabled => "true" jdbc_page_size => "50000" #statement_filepath => "config-mysql/test02.sql" statement => "select * from test02" schedule => "* * * * *" #索引的類型 type => "test02" } } filter { json { source => "message" remove_field => ["message"] } } output { elasticsearch { hosts => "127.0.0.1:9200" # index名 index => "test01" # 須要關聯的數據庫中有有一個id字段,對應索引的id號 document_id => "%{id}" } stdout { codec => json_lines } }
注意關聯的數據庫表中須要有id字段用來映射造成的索引的id號。
終於把工做都作完了。
進入bin目錄下:
./logstash -f config-mysql/mysql.conf --path.data=/data/ELK/logstash/data2 (設置數據存貯路徑)