mysql數(shù)據(jù)同步到elasticsearch


環(huán)境

  • centos7.2
  • php5.5.7
  • mysql5.7

安裝elasticsearch

下載地址:https://www.elastic.co/downloads/elasticsearch

image

安裝2.4.6

wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.4.6/elasticsearch-2.4.6.rpm

yum -y install elasticsearch-2.4.6.rpm


環(huán)境:

  • ubuntu 18.0.4
  • php7.2.13
  • mysql5.7
  • java1.8+
  • go1.9+

安裝elasticsearch

下載地址:https://www.elastic.co/downloads/elasticsearch

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.0.deb

gdebi elasticsearch-6.5.0.deb

安裝完成后茫打,配置elasticsearch

vim /etc/elasticsearch/elasticsearch.yml

//配置內容
.
.
.
cluster.name: zpdx-search    //集群名稱
.
.
.
node.name: zpdx-1      //節(jié)點名稱
.
.
.
path.data: /var/lib/elasticsearch    //數(shù)據(jù)路徑
.
.
.
path.log: /var/log/elasticsearch      //日志路徑
.
.
.
network.host:  127.0.0.1      //主機地址撕捍,默認
.
.
.
http:port:  9200      //端口钧舌,默認

開啟elasticsearch

systemctl start elasticsearch.service

elasticsearch設置開機自啟動

sudo /bin/systemctl daemon-reload

sudo /bin/systemctl enable elasticsearch.service

安裝mvn

sudo apt update

sudo apt install maven 

安裝 ik

github下載地址: elasticsearch-analysis-ik

git clone https://github.com/medcl/elasticsearch-analysis-ik.git

cd lasticsearch-analysis-ik

git checkout tags/1.10.6

mvn clean

mvn compile

mvn package

./elasticsearch-analysis-ik/target/releases目錄會下載了一個對應版本的插件包elasticsearch-analysis-ik-1.10.6.zip

將插件包復制到elaticsearch的插件目錄下

cp elasticsearch-analysis-ik-1.10.6.zip /usr/share/elasticsearch/plugins/

cd /usr/share/elasticsearch/plugins/

unzip elasticsearch-analysis-ik-1.10.6.zip

mkdir ik

mv ./* ik

重啟elasticsearch

systemctl restart elasticsearch.service

測試肝陪,使用curl 'http://127.0.0.1:9200'歉糜,或者用瀏覽器訪問http://127.0.0.1:9200```,顯示

{
  "name" : "zpdx-1",
  "cluster_name" : "zpdxshop-search",
  "cluster_uuid" : "g1wO4CR8TGOwfrDHY-vcGw",
  "version" : {
    "number" : "6.5.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "816e6f6",
    "build_date" : "2018-11-09T18:58:36.352602Z",
    "build_snapshot" : false,
    "lucene_version" : "7.5.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

elasticsearch安裝完成并成功啟動

安裝go

sudo apt update

sudo apt install golang

查看go 版本

go version

查看go 環(huán)境

go env

設置go環(huán)境苫纤,vim /etc/profile

//將go環(huán)境路徑設置到/usr/local/go方便管理
//在最后添加
.
.
.
#go環(huán)境
export GOPATH=/usr/local/go

保存并退出,使/etc/profile文件生效

source /etc/profile

安裝go-mysql-elasticsearch

go-mysql-elasticsearch的基本原理是:如果是第一次啟動該程序流妻,首先使用mysqldump工具對源mysql數(shù)據(jù)庫進行一次全量同步,通過elasticsearch client執(zhí)行操作寫入數(shù)據(jù)到ES笆制;然后實現(xiàn)了一個mysql client,作為slave連接到源mysql,源mysql作為master會將所有數(shù)據(jù)的更新操作通過binlog event同步給slave绅这, 通過解析binlog event就可以獲取到數(shù)據(jù)的更新內容,之后寫入到ES.

使用限制

1. mysql binlog必須是ROW模式

2. 要同步的mysql數(shù)據(jù)表必須包含主鍵项贺,否則直接忽略君躺,這是因為如果數(shù)據(jù)表沒有主鍵,UPDATE和DELETE操作就會因為在ES中找不到對應的document而無法進行同步

3. 不支持程序運行過程中修改表結構

4. 要賦予用于連接mysql的賬戶RELOAD權限以及REPLICATION權限, SUPER權限:
       GRANT REPLICATION SLAVE ON *.* TO 'admin'@'127.0.0.1';
       GRANT RELOAD ON *.* TO 'admin'@'27.0.0.1';
       UPDATE mysql.user SET Super_Priv='Y' WHERE user='admin' AND host='127.0.0.1';

查看mysqlbinlog开缎、server_id棕叫,需要設置

//進入mysql

mysql> show global variables like '%binlog_format%'; 
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.02 sec)

mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 100   |
| server_id_bits | 32    |
+----------------+-------+
2 rows in set (0.01 sec)

mysql> 

設置mysql,編輯/etc/mysql/mysql.conf.d/mysqld.cnf

vim /etc/mysql/mysql.conf.d/mysqld.cnf

.
.
.
[mysql]
log-bin=mysql-bin
binlog_format="ROW"
server-id=100

重啟mysql

systemctl restart mysql

安裝go-mysql-elasticsearch

github下載地址:go-mysql-elasticsearch

go get github.com/siddontang/go-mysql-elasticsearch

cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch

make

go-elasticsearch不能自動創(chuàng)建索引index奕删,需手動創(chuàng)建

vim create_index.json

//添加內容
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "content": {
      "properties": {
        "title": {
          "type": "text",
          "analyzer": "ik_smart",     
          "search_analyzer": "ik_smart"
        },
        "content": {
          "type": "text",
          "analyzer": "ik_smart",
          "search_analyzer": "ik_smart"
        }
      }
    }
  }
}

curl執(zhí)行

chase@chase-MACH-WX9:~$ curl -XPUT 'localhost:9200/test?pretty' -H 'Content-Type:application/json' -d'@create_index.json'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test"
}
chase@chase-MACH-WX9:~$ 

創(chuàng)建成功俺泣,查看索引

chase@chase-MACH-WX9:~$ curl localhost:9200/test?pretty
{
  "test" : {
    "aliases" : { },
    "mappings" : {
      "content" : {
        "properties" : {
          "content" : {
            "type" : "text",
            "analyzer" : "ik_smart"
          },
          "title" : {
            "type" : "text",
            "analyzer" : "ik_smart"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1548855659726",
        "number_of_shards" : "1",
        "number_of_replicas" : "0",
        "uuid" : "F3ze_A2gQo6mE88aEQzKNA",
        "version" : {
          "created" : "6050099"
        },
        "provided_name" : "test"
      }
    }
  }
}
chase@chase-MACH-WX9:~$ 

配置go-mysql-elasticsearch,編輯$GOPATH/src/github.com/siddontang/go-mysql-elasticsearch/etc/river.toml

# MySQL address, user and password
# user must have replication privilege in MySQL.
my_addr = "127.0.0.1:3306"
my_user = "admin"
my_pass = "123456"
my_charset = "utf8"

# Set true when elasticsearch use https
#es_https = false
# Elasticsearch address
es_addr = "127.0.0.1:9200"
# Elasticsearch user and password, maybe set by shield, nginx, or x-pack
es_user = ""
es_pass = ""

# Path to store data, like master.info, if not set or empty,
# we must use this to support breakpoint resume syncing. 
# TODO: support other storage, like etcd. 
data_dir = "./var"

# Inner Http status address
stat_addr = "127.0.0.1:12800"

# pseudo server id like a slave 
server_id = 100

# mysql or mariadb
flavor = "mysql"

# mysqldump execution path
# if not set or empty, ignore mysqldump.
mysqldump = "mysqldump"

# if we have no privilege to use mysqldump with --master-data,
# we must skip it.
#skip_master_data = false

# minimal items to be inserted in one bulk
bulk_size = 128

# force flush the pending requests if we don't have enough items >= bulk_size
flush_bulk_time = "200ms"

# Ignore table without primary key
skip_no_pk_table = false

# MySQL data source
[[source]]
schema = "test"

# Only below tables will be synced into Elasticsearch.
# "t_[0-9]{4}" is a wildcard table format, you can use it if you have many sub tables, like table_0000 - table_1023
# I don't think it is necessary to sync all tables in a database.
tables = ["node"]

# Below is for special rule mapping

# Very simple example
# 
# desc t;
# +-------+--------------+------+-----+---------+-------+
# | Field | Type         | Null | Key | Default | Extra |
# +-------+--------------+------+-----+---------+-------+
# | id    | int(11)      | NO   | PRI | NULL    |       |
# | name  | varchar(256) | YES  |     | NULL    |       |
# +-------+--------------+------+-----+---------+-------+
# 
# The table `t` will be synced to ES index `test` and type `t`.
[[rule]]
schema = "test"
table = "node"
index = "test"
type = "content"

啟動go-mysql-elasticsearch

cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch

./bin/go-mysql-elasticsearch -config=./etc/river.toml

同步數(shù)據(jù)信息

[2019/01/30 21:48:31] [info] binlogsyncer.go:111 create BinlogSyncer with config {100 mysql 127.0.0.1 3306 admin   utf8 false false <nil> false false 0 0s 0s 0}
[2019/01/30 21:48:31] [info] dump.go:164 skip dump, use last binlog replication pos (mysql-bin.000001, 790) or GTID set %!s(<nil>)
[2019/01/30 21:48:31] [info] status.go:53 run status http server 127.0.0.1:12800
[2019/01/30 21:48:31] [info] binlogsyncer.go:323 begin to sync binlog from position (mysql-bin.000001, 790)
[2019/01/30 21:48:31] [info] binlogsyncer.go:172 register slave for master server 127.0.0.1:3306
[2019/01/30 21:48:31] [info] sync.go:31 start sync binlog at binlog file (mysql-bin.000001, 790)
[2019/01/30 21:48:31] [info] binlogsyncer.go:692 rotate to (mysql-bin.000001, 790)
[2019/01/30 21:48:31] [info] binlogsyncer.go:692 rotate to (mysql-bin.000002, 4)
[2019/01/30 21:48:31] [info] sync.go:73 rotate binlog to (mysql-bin.000001, 790)
[2019/01/30 21:48:31] [info] master.go:54 save position (mysql-bin.000001, 790)
[2019/01/30 21:48:31] [info] sync.go:73 rotate binlog to (mysql-bin.000002, 4)
[2019/01/30 21:48:31] [info] master.go:54 save position (mysql-bin.000002, 4)

自此完残,數(shù)據(jù)全量導入到elasticsearch

設置go-mysql-elasticsearch開機自啟動

ubuntu安裝sysv-rc-conf

sudo apt update

sudo apt install sysv-rc-conf

如果報錯:

sudo apt-get install sysv-rc-conf
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關系樹       
正在讀取狀態(tài)信息... 完成       
E: 無法定位軟件包 sysv-rc-conf

解決方法:添加鏡像源

vim /etc/apt/sources.list

//添加如下內容
deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse

sudo apt update

sudo apt install sysv-rc-con

創(chuàng)建go-mysql-elasticsearch腳本

vim /etc/init.d/go-mysql-elasticsearch

//添加內容如下:
#!/bin/bash
#go-mysql-elasticsearch start
$GOPATH/src/github.com/siddontang/go-mysql-elasticsearch/bin/go-mysql-elasticsearch -config=$GOPATH/src/github.com/siddontang/go-mysql-elasticsearch/etc/river.toml

執(zhí)行

sysv-rc-conf go-mysql-elasticsearch on

service go-mysql-elasticsearch start

centos安裝chkconfig

yum -y install chkconfig

安裝elasticsearch-php

composer require elasticsearch/elasticsearch

elasticsearch官方文檔

elasticsearch-php官方文檔

Mysql與Elasticsearch數(shù)據(jù)實時同步

elasticsearch之實例篇

阮一峰 -- 全文搜索引擎 Elasticsearch 入門教程

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末伏钠,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子谨设,更是在濱河造成了極大的恐慌熟掂,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件扎拣,死亡現(xiàn)場離奇詭異赴肚,居然都是意外死亡,警方通過查閱死者的電腦和手機二蓝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門誉券,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人刊愚,你說我怎么就攤上這事踊跟。” “怎么了鸥诽?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵商玫,是天一觀的道長。 經(jīng)常有香客問我衙传,道長决帖,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任蓖捶,我火速辦了婚禮,結果婚禮上扁远,老公的妹妹穿的比我還像新娘俊鱼。我一直安慰自己刻像,他們只是感情好,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布并闲。 她就那樣靜靜地躺著细睡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪帝火。 梳的紋絲不亂的頭發(fā)上溜徙,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機與錄音犀填,去河邊找鬼蠢壹。 笑死,一個胖子當著我的面吹牛九巡,可吹牛的內容都是我干的图贸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼冕广,長吁一口氣:“原來是場噩夢啊……” “哼疏日!你這毒婦竟也來了?” 一聲冷哼從身側響起撒汉,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤沟优,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后睬辐,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體挠阁,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年溉委,在試婚紗的時候發(fā)現(xiàn)自己被綠了鹃唯。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡瓣喊,死狀恐怖坡慌,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情藻三,我是刑警寧澤洪橘,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站棵帽,受9級特大地震影響熄求,放射性物質發(fā)生泄漏。R本人自食惡果不足惜逗概,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一弟晚、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦卿城、人聲如沸枚钓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽搀捷。三九已至,卻和暖如春多望,著一層夾襖步出監(jiān)牢的瞬間嫩舟,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工怀偷, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留家厌,地道東北人。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓枢纠,卻偏偏與公主長得像像街,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子晋渺,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內容