ElasticSearch实时同步MySQL数据

avatar 2019年02月05日19:36:22 6 9489 views
博主分享免费Java教学视频,B站账号:Java刘哥
ElasticSearch是目前最风靡的一款的开源框架,可用于站内搜索和日志分析。 目前,我们准备把所有文章搜索或分页都基于 ElasticSearch 实现,其“搜索速度快,有分词,高亮等功能”非常吸引人。 通常为了缓解 MySQL 的压力,我们可以尝试让 MySQL 用于写,读都从 ElasticSearch 或 Redis 来进行。然后,我们需要解决的就是数据如何从 MySQL实时或近实时同步到 ElasticSearch 中去,可以用 MySQL 的 binlog 出发,它记录了 MySQL 执行的 sql 语句(除了查询语句),可以用于恢复数据。 目前通过 binlog 实现 mysql 数据同步到 es 中的开源框架有好几个,本文介绍其中一种:go-mysql-elasticsearch,在 mac 上实践。 官方地址:https://github.com/siddontang/go-mysql-elasticsearch  

一、安装 go 环境

1.下载 go语言中文网:https://studygolang.com/dl 下载 > 1.9 版本的对应的包,mac 版可以直接下载 pkg 后缀的执行文件,双击安装 也可以直接下载压缩包,然后解压   2.配置环境变量
  1. export PATH=$PATH:/usr/local/go/bin
  2. export GOPATH=/Users/liuyanzhao/go
 

二、下载 go-mysql-elasticsearch

分别执行以下三行命令(也可以直接克隆上面的 github 资源地址到本地) go get github.com/siddontang/go-mysql-elasticsearch cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch make  

三、配置

1.MySQL 配置 修改 MySQL 的 my.cnf 文件,mac 上是在 /etc/my.cnf
  1. binlog_format=ROW
  2. log-bin=mysql-bin
  3. server-id=1
下面分别说明一下这三行配置 go-mysql-search 限定了 binlog 必须使用 ROW 模式,server-id 和 下面的配置文件中的一致。   2.go-mysql-elasticsearch 配置

修改 go-mysql-elasticsearch/etc/river.toml

该文件是唯一配置文件

  1. # MySQL address, user and password
  2. # user must have replication privilege in MySQL.
  3. my_addr = "127.0.0.1:3306"
  4. my_user = "root"
  5. my_pass = "123456"
  6. my_charset = "utf8"
  7. # Set true when elasticsearch use https
  8. #es_https = false
  9. # Elasticsearch address
  10. es_addr = "127.0.0.1:9200"
  11. # Elasticsearch user and password, maybe set by shield, nginx, or x-pack
  12. es_user = ""
  13. es_pass = ""
  14. # Path to store data, like master.info, if not set or empty,
  15. # we must use this to support breakpoint resume syncing.
  16. # TODO: support other storage, like etcd.
  17. data_dir = "./var"
  18. # Inner Http status address
  19. stat_addr = "127.0.0.1:12800"
  20. # pseudo server id like a slave
  21. server_id = 1
  22. # mysql or mariadb
  23. flavor = "mysql"
  24. # mysqldump execution path
  25. if not set or empty, ignore mysqldump.
  26. mysqldump = "mysqldump"
  27. if we have no privilege to use mysqldump with --master-data,
  28. # we must skip it.
  29. #skip_master_data = true
  30. # minimal items to be inserted in one bulk
  31. bulk_size = 128
  32. # force flush the pending requests if we don't have enough items >= bulk_size
  33. flush_bulk_time = "200ms"
  34. # Ignore table without primary key
  35. skip_no_pk_table = false
  36. # MySQL data source
  37. [[source]]
  38. schema = "sens_blog"
  39. [[rule]]
  40. schema = "sens_blog"
  41. table = "sens_post"
  42. index = "blog"
  43. type = "post"
  44. filter = ["post_id""user_id""post_title""post_summary""post_thumbnail""post_type""post_status""post_views",  "post_likes""comment_size""post_date"]
  45. id = ["post_id"]
  46. [rule.field]
  47. post_id = "postId"
  48. user_id = "userId"
  49. post_title = "postTitle"
  50. post_summary = "postSummary"
  51. post_thumbnail = "postThumbnail"
  52. post_type = "postType"
  53. post_status = "postStatus"
  54. post_views = "postViews"
  55. post_likes = "postLikes"
  56. comment_size = "commentSize"
  57. post_date = "postDate"

上面的 schema 指的是 MySQL 数据库名,table 数据库中要同步的表名,index 是 ES 索引,type 是 ES 类型。

上面这段配置指的是 同步 MySQL sens_blog 数据库中的 sens_post 表到 ES 中,索引为 blog,类型为 post。如果索引没有创建,会自动创建。如果索引创建了,不会自动创建。

建议还是自己创建 索引

PUT 127.0.0.1:9200/blog

  1. {
  2.     "settings": {
  3.         "number_of_shards"5,
  4.         "number_of_replicas"1
  5.     },
  6.     "mappings": {
  7.         "post": {
  8.             "properties": {
  9.                 "postId": {
  10.                     "type""long"
  11.                 },
  12.                 "userId": {
  13.                     "type""long"
  14.                 },
  15.                 "postTitle": {
  16.                     "type""text"
  17.                 },
  18.                 "postSummary": {
  19.                     "type""text"
  20.                 },
  21.                 "postThumbnail": {
  22.                     "type""keyword"
  23.                 },
  24.                 "postType": {
  25.                     "type""keyword"
  26.                 },
  27.                 "postStatus": {
  28.                     "type""integer"
  29.                 },
  30.                 "postViews": {
  31.                     "type""long"
  32.                 },
  33.                 "postLikes": {
  34.                     "type""long"
  35.                 },
  36.                 "commentSize": {
  37.                     "type""long"
  38.                 },
  39.                 "postDate": {
  40.                     "type""date"
  41.                 }
  42.             }
  43.         }
  44.     }
  45. }

四、启动,开始自动同步

上面配置文件修改后,保存。

执行命令:

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

然后就能开始同步。

打开 es 的 head 插件:http://127.0.0.1:9100 查看数据

如果你不小心删除了索引,想要重新同步,但是却发现无法同步之前的数据。

你可以删除 go-mysql-elasticsearch/var/master.info 即可解决问题

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0