• go-mysql-elasticsearch 使用


    文档

    github 链接:GitHub - go-mysql-org/go-mysql-elasticsearch: Sync MySQL data into elasticsearch

    参考博客

    注意事项

    1. go-mysql-elasticsearch启动以后不能进行修改表结构
    2. 使用go-mysql-elasticsearch的必需要添加就是先开启binlog模式
    3. 记得先创建索引
    4. 提前先同步数据,不然binlog日志之前的数据无法同步。
    5. 可以支持es7 和mysql8版本 不知道为啥官方文档没写可以用

    优点

    • 无需三方工具直接监听 mysql binlog 即可同步数据到 es

    • 所占内存小 cpu 水位不会飙升(相对使用logstash)

    监控

    服务器配置 2 核 4g 代码,mysql和es在同一台服务器上

    开启时间 11:00 后

    测试数据1万

    安装

     
    

    1. Git clone https://github.com/go-mysql-org/go-mysql-elasticsearch.git
    2. [root@hecs-202871 go-mysql-elasticsearch]# pwd
    3. /home/golang/gopath/go-mysql-elasticsearch
    4. #安装
    5. go install
    6. #编译
    7. make
    8. [root@hecs-202871 go-mysql-elasticsearch]# ls
    9. bin cmd elastic go.mod LICENSE README.md var
    10. clear_vendor.sh Dockerfile etc go.sum Makefile river
    11. #进行配置,配置比较重要,在下边将详细去将
    12. vim /home/golang/gopath/go-mysql-elasticsearch/etc/river.toml
    13. #启动
    14. ./bin/go-mysql-elasticsearch -config=./etc/river.toml

    配置river

    1. # MySQL 的相关配置
    2. # 指定用户必须具备复制权限
    3. my_addr = "127.0.0.1:3306"
    4. my_user = "canal"
    5. my_pass = "123456"
    6. my_charset = "utf8"
    7. # ES 相关配置
    8. es_addr = "127.0.0.1:9200"
    9. es_user = ""
    10. es_pass = ""
    11. # Inner Http status address 不加会报错
    12. stat_addr = "127.0.0.1:12800"
    13. stat_path = "/metrics"
    14. # 数据源配置
    15. # 以 Slave 模式工作
    16. server_id = 10001
    17. # mysql/mariadb
    18. flavor = "mysql"
    19. # mysqldump 路径,如果为空或者未设置,会跳过这一环节。
    20. mysqldump = "mysqldump"
    21. bulk_size = 128
    22. flush_bulk_time = "200ms"
    23. skip_no_pk_table = false
    24. [[source]]
    25. # 数据库名称
    26. schema = "canal"
    27. # 数据表同步范围,支持通配符
    28. tables = ["uuid"]
    29. # 规则定义
    30. [[rule]]
    31. # 数据库名称
    32. schema = "canal"
    33. # 规则对应的数据表,支持通配符
    34. table = "uuid"
    35. # 目标 ES 索引
    36. index = "uuid"
    37. # 该规则在 ES 中生成的文档类型
    38. type = "_doc"

    其他参数

    # 规则对应的数据表,支持通配符 table = "uuid*"

    多表配置

    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 = ""
    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. stat_path = "/metrics"
    21. # pseudo server id like a slave
    22. server_id = 1001
    23. # mysql or mariadb
    24. flavor = "mysql"
    25. # mysqldump execution path
    26. # if not set or empty, ignore mysqldump.
    27. mysqldump = "mysqldump"
    28. # if we have no privilege to use mysqldump with --master-data,
    29. # we must skip it.
    30. #skip_master_data = false
    31. # minimal items to be inserted in one bulk
    32. bulk_size = 128
    33. # force flush the pending requests if we don't have enough items >= bulk_size
    34. flush_bulk_time = "200ms"
    35. # Ignore table without primary key
    36. skip_no_pk_table = false
    37. # MySQL data source
    38. [[source]]
    39. schema = "test"
    40. # Only below tables will be synced into Elasticsearch.
    41. # "t_[0-9]{4}" is a wildcard table format, you can use it if you have many sub tables, like table_0000 - table_1023
    42. # I don't think it is necessary to sync all tables in a database.
    43. tables = ["t", "t_[0-9]{4}", "tfield", "tfilter"]
    44. # Below is for special rule mapping
    45. # Very simple example
    46. #
    47. # desc t;
    48. # +-------+--------------+------+-----+---------+-------+
    49. # | Field | Type | Null | Key | Default | Extra |
    50. # +-------+--------------+------+-----+---------+-------+
    51. # | id | int(11) | NO | PRI | NULL | |
    52. # | name | varchar(256) | YES | | NULL | |
    53. # +-------+--------------+------+-----+---------+-------+
    54. #
    55. # The table `t` will be synced to ES index `test` and type `t`.
    56. [[rule]]
    57. schema = "test"
    58. table = "t"
    59. index = "test"
    60. type = "t"
    61. # Wildcard table rule, the wildcard table must be in source tables
    62. # All tables which match the wildcard format will be synced to ES index `test` and type `t`.
    63. # In this example, all tables must have same schema with above table `t`;
    64. [[rule]]
    65. schema = "test"
    66. table = "t_[0-9]{4}"
    67. index = "test"
    68. type = "t"
    69. # Simple field rule
    70. #
    71. # desc tfield;
    72. # +----------+--------------+------+-----+---------+-------+
    73. # | Field | Type | Null | Key | Default | Extra |
    74. # +----------+--------------+------+-----+---------+-------+
    75. # | id | int(11) | NO | PRI | NULL | |
    76. # | tags | varchar(256) | YES | | NULL | |
    77. # | keywords | varchar(256) | YES | | NULL | |
    78. # +----------+--------------+------+-----+---------+-------+
    79. #
    80. [[rule]]
    81. schema = "test"
    82. table = "tfield"
    83. index = "test"
    84. type = "tfield"
    85. [rule.field]
    86. # Map column `id` to ES field `es_id`
    87. id="es_id"
    88. # Map column `tags` to ES field `es_tags` with array type
    89. tags="es_tags,list"
    90. # Map column `keywords` to ES with array type
    91. keywords=",list"
    92. # Filter rule
    93. #
    94. # desc tfilter;
    95. # +-------+--------------+------+-----+---------+-------+
    96. # | Field | Type | Null | Key | Default | Extra |
    97. # +-------+--------------+------+-----+---------+-------+
    98. # | id | int(11) | NO | PRI | NULL | |
    99. # | c1 | int(11) | YES | | 0 | |
    100. # | c2 | int(11) | YES | | 0 | |
    101. # | name | varchar(256) | YES | | NULL | |
    102. # +-------+--------------+------+-----+---------+-------+
    103. #
    104. [[rule]]
    105. schema = "test"
    106. table = "tfilter"
    107. index = "test"
    108. type = "tfilter"
    109. # Only sync following columns
    110. filter = ["id", "name"]
    111. # id rule
    112. #
    113. # desc tid_[0-9]{4};
    114. # +----------+--------------+------+-----+---------+-------+
    115. # | Field | Type | Null | Key | Default | Extra |
    116. # +----------+--------------+------+-----+---------+-------+
    117. # | id | int(11) | NO | PRI | NULL | |
    118. # | tag | varchar(256) | YES | | NULL | |
    119. # | desc | varchar(256) | YES | | NULL | |
    120. # +----------+--------------+------+-----+---------+-------+
    121. #
    122. [[rule]]
    123. schema = "test"
    124. table = "tid_[0-9]{4}"
    125. index = "test"
    126. type = "t"
    127. # The es doc's id will be `id`:`tag`
    128. # It is useful for merge muliple table into one type while theses tables have same PK
    129. id = ["id", "tag"]

    Docker 部署

    待续

  • 相关阅读:
    第九期|不是吧,我在社交媒体的照片也会被网络爬虫?
    Xilinx 7系列FPGA的配置流程
    Mongodb 副本集名称重命名
    spyglass cdc检查约束
    Java实现单链表的反转
    小松鼠踩一踩游戏
    驱动——gpio子系统(LED灯的操控实验)
    2023秋招--梦加网络--游戏客户端--二面面经
    spring+SpringMVC+MyBatis之配置多数据源
    mysql主从节点搭建
  • 原文地址:https://blog.csdn.net/qq_27229113/article/details/127726991