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

相关推荐
大布布将军5 小时前
⚡️ 深入数据之海:SQL 基础与 ORM 的应用
前端·数据库·经验分享·sql·程序人生·面试·改行学it
JIngJaneIL6 小时前
基于java+ vue农产投入线上管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
源代码•宸6 小时前
goframe框架签到系统项目(BITFIELD 命令详解、Redis Key 设计、goframe 框架教程、安装MySQL)
开发语言·数据库·经验分享·redis·后端·mysql·golang
川贝枇杷膏cbppg6 小时前
Redis 的 AOF
java·数据库·redis
TG:@yunlaoda360 云老大6 小时前
如何在华为云国际站代理商控制台进行SFS Turbo的性能与容量核查?
服务器·网络·数据库·华为云
ytttr8737 小时前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab
云老大TG:@yunlaoda3607 小时前
如何进行华为云国际站代理商跨Region适配?
大数据·数据库·华为云·负载均衡
思成不止于此8 小时前
【MySQL 零基础入门】事务精讲(二):ACID 特性与并发问题
数据库·笔记·学习·mysql
Boilermaker19928 小时前
[MySQL] 初识 MySQL 与 SQL 基础
数据库·mysql
今晚务必早点睡8 小时前
Redis——快速入门第二课:Redis 常用命令 + 能解决实际问题
数据库·redis·bootstrap