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/[email protected]

报错: 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

相关推荐
Zfox_2 小时前
Redis:Hash数据类型
服务器·数据库·redis·缓存·微服务·哈希算法
陈丹阳(滁州学院)4 小时前
若依添加添加监听容器配置(删除键,键过期)
数据库·oracle
远方16094 小时前
14-Oracle 23ai Vector Search 向量索引和混合索引-实操
数据库·ai·oracle
GUIQU.6 小时前
【Oracle】数据仓库
数据库·oracle
恰薯条的屑海鸥6 小时前
零基础在实践中学习网络安全-皮卡丘靶场(第十六期-SSRF模块)
数据库·学习·安全·web安全·渗透测试·网络安全学习
咖啡啡不加糖6 小时前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存
曼汐 .6 小时前
数据库管理与高可用-MySQL高可用
数据库·mysql
MickeyCV6 小时前
使用Docker部署MySQL&Redis容器与常见命令
redis·mysql·docker·容器·wsl·镜像
2301_793102496 小时前
Linux——MySql数据库
linux·数据库
喵叔哟6 小时前
第4章:Cypher查询语言基础
数据库