安裝 ElasticSearch 並連線mysql資料庫
阿新 • • 發佈:2018-12-27
一、安裝ElasticSearch
1.官網下載ElasticSearch,解壓即可。(下載地址:https://www.elastic.co/downloads/elasticsearch)
2.進入安裝目錄下bin目錄中,使用命令 ./elasticsearch 啟動,啟動輸出資訊結尾出現started 關鍵字,表示啟動成功,使用./elasticsearch -d 命令使服務後臺啟動,程式預設埠為9200。
3. 使用 http://localhost:9200 訪問,結果如下
4.當前服務只允許本機訪問,想要外網訪問,想要修改安裝目錄下config目錄中的elasticsearch.yml檔案,
在yml檔案中加入 network.host: 0.0.0.0 ,表示所有IP都可以訪問。
二、安裝logstash
logstash 可以直接和資料庫關聯,並且自動根據資料庫中的資料更新索引。
安裝版本需要和elasticsearch的版本一致。
1.下載安裝包,上傳到自定義的安裝目錄即可。(下載地址:https://www.elastic.co/downloads/logstash)
2.進入安裝目錄的bin目錄中,執行如下命令:
./logstash -e 'input { stdin { } } output { stdout {} }'
3.連線mysql
停掉logstash。
下載mysql JDBC驅動jar包,(下載地址:https://www.mysql.com/products/connector/)將jar包放在logstash目錄下,
在 bin目錄中新建檔案 logstash-mysql.conf 。
logstash-mysql.conf 檔案內容
input { stdin { } jdbc { # 資料庫 資料庫名稱為elk,表名為book_table jdbc_connection_string => "jdbc:mysql://localhost:3306/elk" # 使用者名稱密碼 jdbc_user => "admin" jdbc_password => "password" # jar包的位置 jdbc_driver_library => "/usr/elastic/logstash-6.5.3/mysql-connector-java-8.0.13.jar" # mysql的Driver jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_paging_enabled => "true" jdbc_page_size => "50000" #statement_filepath => "config-mysql/book.sql" statement => "select * from book_table" schedule => "* * * * *" #索引的型別 type => "book" } } filter { json { source => "message" remove_field => ["message"] } } output { elasticsearch { hosts => "127.0.0.1:9200" # index名 index => "elk" # 需要關聯的資料庫中有有一個id欄位,對應索引的id號 document_id => "%{id}" } stdout { codec => json_lines } }
再次啟動logstash,進入bin目錄,執行命令:
./logstash -f logstash-mysql.conf
資料庫中資料就被讀取出來了。