go-mysql-transfer 同步数据到es

同步数据需要注意的事项 前提条件

1 要同步的mysql 表必须包含主键

2 mysql binlog 必须是row 模式

3 不支持程序运行过程中修改表结构

4 要赋予连接mysql 账号的权限 reload, replication super 权限 如果是root 权限则不需要

安装 go-mysql-transfer

复制代码
​
git clone  https://github.com/wj596/go-mysql-transfer.git

cd go-mysql-transfer
​go get
go build

1 设置mysql binlog模式为row

同步数据位置 查看mysql binlog 日志位置

确保binlog 日志为 on

数据库结构 表 test

app.yml 配置

复制代码
# mysql
addr: 192.168.217.134:3306
user: root
pass: Devops%8
charset : utf8
slave_id: 1001 #slave ID
flavor: mysql #mysql or mariadb,榛???mysql

target: elasticsearch #

#elasticsearch
es_addrs: 127.0.0.1:9200
es_version: 6 # 
#es_password:  #
#es_version:  #

rule:
  - schema: game
    table: lottery 
    order_by_column: id
    column_underscore_to_camel: false 
    value_encoder: json
    
    es_index: lottery 
    es_mappings: 
      - column: id
        field: id
        type: long
      - column: lose
        field: lose #es
        type: keyword
      - column: win
        field: win
        type: keyword
      - column: stake
        field: stake
        type: double
      - column: odds
        field: odds
        type: double

全局数据初始化

复制代码
./go-mysql-transfer -stock

增量数据同步

修改mysql 的个别数据 同步binlog到最新位置 。。。执行同步操作

同步数据到最新位置,修改部分数据。。。

./go-mysql-transfer -position binlog.000015 2457

复制代码
[root@localhost data]# ./go-mysql-transfer -position binlog.000015 2457 
2024-04-27 10:14:36.564695 I | process id: 46615
2024-04-27 10:14:36.564905 I | GOMAXPROCS :2
2024-04-27 10:14:36.564909 I | source  mysql(192.168.217.134:3306)
2024-04-27 10:14:36.564912 I | destination elasticsearch(http://127.0.0.1:9200)
The current dump position is : binlog.000015 2457

执行增量同步

复制代码
./go-mysql-transfer -config app.yml

数据类型映射表

|-----------|---------|---|
| mysql数据类型 | es数据类型 | |
| int | long | |
| varchar | keyword | |
| decimal | double | |

报错

同步数据报错

在 go-mysql-transfer 中执行

go get github.com/json-iterator/go@v1.1.12

报错: document missing

复制代码
2024-05-01 17:15:16.081082 I | dumper stopped
2024-05-01 17:15:16.083295 I | transfer run from position(binlog.000017 6375)
2024-05-01 17:15:16.289692 I | [_doc][6]: document missing
2024-05-01 17:15:17.086846 I | transfer stop
2024-05-01 17:15:17.088804 I | dumper stopped
2024-05-01 17:15:17.091393 I | transfer run from position(binlog.000017 6375)
2024-05-01 17:15:17.294877 I | [_doc][6]: document missing
2024-05-01 17:15:18.095177 I | transfer stop
2024-05-01 17:15:18.097077 I | dumper stopped
2024-05-01 17:15:18.099320 I | transfer run from position(binlog.000017 6375)
2024-05-01 17:15:18.302411 I | [_doc][6]: document missing
^C2024-05-01 17:15:18.704188 I | application stoped锛𸱳ignal: interrupt 
2024-05-01 17:15:19.102070 I | transfer stop
2024-05-01 17:15:19.103859 I | dumper stopped

_doc 默认的索引类型 一个索引index 可以包含多个type

类型 type 包含多个文档(document)

原因: es 中没有数据,更新导致找不到对应的数据 因此需要先初始化数据到es 中

./go-mysql-transfer -stock

相关推荐
倔强的石头_9 小时前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
Elasticsearch2 天前
如何使用 Agent Builder 排查 Kubernetes Pod 重启和 OOMKilled 事件
elasticsearch
jiayou642 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
于眠牧北2 天前
MySQL的锁类型,表锁,行锁,MVCC中所使用的临键锁
mysql
李广坤2 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
Elasticsearch3 天前
通用表达式语言 ( CEL ): CEL 输入如何改进 Elastic Agent 集成中的数据收集
elasticsearch
Turnip12023 天前
深度解析:为什么简单的数据库"写操作"会在 MySQL 中卡住?
后端·mysql
爱可生开源社区3 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1774 天前
《从零搭建NestJS项目》
数据库·typescript
加号34 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql