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

相关推荐
2301_7816686112 小时前
Elasticsearch 02
大数据·elasticsearch·搜索引擎
Kt&Rs12 小时前
MySQL复制技术的发展历程
数据库·mysql
小小菜鸡ing12 小时前
pymysql
java·服务器·数据库
手握风云-12 小时前
MySQL数据库精研之旅第十六期:深度拆解事务核心(上)
数据库·mysql
boonya13 小时前
Redis核心原理与面试问题解析
数据库·redis·面试
沙二原住民13 小时前
提升数据库性能的秘密武器:深入解析慢查询、连接池与Druid监控
java·数据库·oracle
三毛200413 小时前
玳瑁的嵌入式日记D33-0908(SQL数据库)
jvm·数据库·sql
叫我龙翔13 小时前
【MySQL】从零开始了解数据库开发 --- 库的操作
数据库·mysql·数据库开发
没有bug.的程序员13 小时前
Redis Stream:轻量级消息队列深度解析
java·数据库·chrome·redis·消息队列
GottdesKrieges14 小时前
OceanBase容量统计:租户、数据库、表大小
数据库·oceanbase