為了解決什么問題?
本文介紹如何將mysql里的表數(shù)據(jù)導(dǎo)入至ElasticSearch(以下簡稱es),并用kibana查詢這些數(shù)據(jù)
準(zhǔn)備
一臺(tái)裝有centos 7能夠正常聯(lián)網(wǎng)的服務(wù)器(內(nèi)存至少8G)
安裝好JDK(版本大于等于8)
下載相同版本的ElasticSearch,kibana,logstash的rpm安裝包(官網(wǎng)都有這里就不多說了)
提供一個(gè)測(cè)試mysql數(shù)據(jù)庫(表里要有數(shù)據(jù)噢)
將下載好的rpm包全部傳到服務(wù)器/tmp/下
mysql驅(qū)動(dòng)jar
安裝ElasticSearch(以下簡稱es)
- 關(guān)閉機(jī)器selinux
- 執(zhí)行安裝命令
rpm -ivh elasticsearch-8.5.0-x86_64.rpm
- 查看elasticsearch.service安裝文件
#我們使用rpm安裝的serivce文件都在/usr/lib/systemd/system/下
vi /usr/lib/systemd/system/elasticsearch.service
- 查看es本體安裝目錄
#我們使用rpm安裝的安裝本體都在/usr/share/下
cd /usr/share/elasticsearch/
[root@node91 elasticsearch]# ls
bin jdk lib LICENSE.txt modules NOTICE.txt plugins README.asciidoc
- 調(diào)整查看配置文件
[root@node91 elasticsearch]# cd /etc/elasticsearch/
[root@node91 elasticsearch]# ls
certs elasticsearch-plugins.example.yml elasticsearch.yml.rpmsave jvm.options.d role_mapping.yml users
elasticsearch.keystore elasticsearch.yml jvm.options log4j2.properties roles.yml users_roles
- vi編輯elasticsearch.yml主配置文件
# 部分需要更改的配置
# es日志地址
path.logs: /var/log/elasticsearch解析
# 網(wǎng)絡(luò)監(jiān)聽地址
network.host: 0.0.0.0
# 訪問端口
http.port: 9200
# 安全配置 xpack開頭的全部改成false
xpack.* = false
- 啟動(dòng)es服務(wù)
# 所有采用rpm安裝的軟件都會(huì)交由systemctl去管理,下面趁機(jī)說下一些systemctl服務(wù)的基本操作
# 系統(tǒng)重載systemctl服務(wù),凡是對(duì)serivice文件做了更改的必須執(zhí)行下面的命令
systemctl daemon-reload
# 查看es服務(wù)狀態(tài)
systemctl status elasticsearch.service
# 運(yùn)行es服務(wù),并查看日志
systemctl start elasticsearch.service && journalctl -f -n 22 -u elasticsearch.service
# 關(guān)閉es服務(wù)
systemctl stop elasticsearch.service
# 將es服務(wù)注冊(cè)為開機(jī)自啟服務(wù)
systemctl enable elasticsearch.service
# 禁用es服務(wù)為開機(jī)自啟
systemctl disabled elasticsearch.service
# 重啟es服務(wù),并查看日志
systemctl restart elasticsearch.service && journalctl -f -n 22 -u elasticsearch.service
- 測(cè)試es服務(wù)是否正常開啟
# 能看到 "You Know, for Search" 即說明正常
curl http://localhost:9200
{
"name" : "elk",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "hqp4Aad0T2Gcd4QyiHASmA",
"version" : {
"number" : "8.5.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "de777fa",
"build_date" : "2022-10-24T18:30:11.767338Z",
"build_snapshot" : false,
"lucene_version" : "9.4.1",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
安裝kibana
- 執(zhí)行安裝命令
rpm -ivh kibana-8.5.0-x86_64.rpm
- 查看kibana.service安裝文件
#我們使用rpm安裝的serivce文件都在/usr/lib/systemd/system/下
vi /usr/lib/systemd/system/kibana.service
- 查看kibana本體安裝目錄
#我們使用rpm安裝的安裝本體都在/usr/share/下
cd /usr/share/kibana/
[root@node91 kibana]# ls
bin LICENSE.txt node node_modules NOTICE.txt package.json plugins README.txt src x-pack
- 調(diào)整查看配置文件
[root@node91 kibana]# cd /etc/kibana/
[root@node91 kibana]# ls
kibana.keystore kibana.yml node.options
- vi編輯kibana.yml主配置文件
# 部分需要更改的配置
# kibana端口
server.port: 5601
# 可訪問,0.0.0.0 表示可遠(yuǎn)程鏈接
server.host: "0.0.0.0"
# 監(jiān)聽es地址
elasticsearch.hosts: ["http://localhost:9200"]
# 訪問端口
http.port: 9200
# web頁面顯示改成中文
i18n.locale: "zh-CN"
- 啟動(dòng)kibana服務(wù)(跟es的服務(wù)一個(gè)地址噢也是同樣的用法)
# 運(yùn)行kibana服務(wù),并查看日志
systemctl start kibana.service && journalctl -f -n 22 -u kibana.service
- 打開瀏覽器,瀏覽kibana web頁面
http://localhost:5601
安裝logstash
- 執(zhí)行安裝命令
rpm -ivh logstash-8.5.0-x86_64.rpm
- 查看logstash.service安裝文件
#我們使用rpm安裝的serivce文件都在/usr/lib/systemd/system/下
vi /usr/lib/systemd/system/logstash.service
- 查看logstash本體安裝目錄
#我們使用rpm安裝的安裝本體都在/usr/share/下
cd /usr/share/logstash/
[root@node91 logstash]# ls
bin CONTRIBUTORS Gemfile jdk lib logstash-core modules tools x-pack
config data Gemfile.lock JDK_VERSION LICENSE.txt logstash-core-plugin-api NOTICE.TXT vendor
- 調(diào)整查看配置文件
[root@node91 logstash]# cd /etc/logstash/
[root@node91 logstash]# ls
conf.d jvm.options log4j2.properties logstash-sample.conf logstash.yml pipelines.yml startup.options
- vi編輯startup.options啟動(dòng)參數(shù)配置文件
# 部分需要更改的配置
# logstash自身帶了jdk,需要把這里編輯下,否則啟動(dòng)可能會(huì)報(bào)錯(cuò)
JAVACMD=/usr/share/logstash/jdk
- 將mysql 驅(qū)動(dòng)jar放至/usr/share/logstash/bin/下
- 我的路徑是/usr/share/logstash/bin/mysql-connector-java-8.0.19.jar
- 增加編輯數(shù)據(jù)傳輸配置
# 在/etc/logstash/文件夾下增加config文件夾,將放置鏈接mysql與es的配置文件
[root@node91 logstash]# cd /etc/logstash/
[root@node91 logstash]# mkdir config
[root@node91 logstash]# cd config
[root@node91 config]# cd config
[root@node91 config]# vi jdbc.conf
# jdbc.conf內(nèi)容,如下
input {
jdbc {
# 數(shù)據(jù)庫連接字符串
jdbc_connection_string => "jdbc:mysql://192.168.3.187:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8"
# 數(shù)據(jù)庫登錄名
jdbc_user => "root"
# 登錄密碼
jdbc_password => "rootdb"
# jdbc驅(qū)動(dòng)程序包路徑
jdbc_driver_library => "/usr/share/logstash/bin/mysql-connector-java-8.0.19.jar"
# 驅(qū)動(dòng)程序類名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
# 是否分頁
jdbc_paging_enabled => "true"
# 每頁幾條記錄
jdbc_page_size => "10000"
# 同步查詢語句演闭,:sql_last_value變量為上次同步時(shí)間,程序會(huì)記錄下來霹崎,如果第一次同步則為1970-1-1
# mapping中日期類型字段我定義了格式,為了避免報(bào)錯(cuò)臭墨,我就在SQL語句中格式化為字符類型
statement => "SELECT * FROM test WHERE createTime>= :sql_last_value"
# 同步頻率双炕,cron表達(dá)式,不會(huì)的可以百度一下障本;這樣是每分鐘執(zhí)行一次
schedule => "* * * * *"
# 時(shí)區(qū)弛槐,一定要配置懊亡,不然 :sql_last_value變量會(huì)少了8小時(shí)
jdbc_default_timezone => "Asia/Shanghai"
type => "jdbc"
}
}
filter {
}
output {
elasticsearch {
# ES索引名稱
index => "mysql-test01"
# ES文檔ID,配置表的主鍵字段
document_id => "%{id}"
hosts => ["localhost:9200"]
}
}
- 啟動(dòng)logstash服務(wù)(跟es的服務(wù)一個(gè)地址噢也是同樣的用法)
# 運(yùn)行l(wèi)ogstash服務(wù),并查看日志
systemctl start logstash.service && journalctl -f -n 22 -u logstash.service
- 將配置文件設(shè)置到啟動(dòng)項(xiàng)
# 編輯 logstash.service
vi /usr/lib/systemd/system/logstash.service
# ExecStart項(xiàng)前 增加啟動(dòng)配置
# 修改前
ExecStart=/usr/share/logstash/bin/logstash "--path.settings" "/etc/logstash"
# 修改后
ExecStart=/usr/share/logstash/bin/logstash -f /etc/logstash/config/jdbc.conf "--path.settings" "/etc/logstash"
- 打開瀏覽器,瀏覽kibana web頁面,操作數(shù)據(jù)
http://localhost:5601
附錄:
- 三套軟件下載地址
https://artifacts.elastic.co/downloads/elasticsearch
https://www.elastic.co/cn/downloads/kibana
https://www.elastic.co/cn/downloads/logstash