第一个go-mysql-binlog解析

栏目: 数据库 · 发布时间: 6年前

1、这是我第一个二进制日志解析的例子,对官方给出的demo做出相应调整,使其能够运行

package main

import (

"context"

"os"

"time"

"github.com/siddontang/go-mysql/mysql"

"github.com/siddontang/go-mysql/replication"

)

// Create a binlog syncer with a unique server id, the server id must be different from other MySQL's.

// flavor is mysql or mariadb

func main () {

cfg := replication.BinlogSyncerConfig{

ServerID: 100 ,

Flavor: "mysql" ,

Host: "127.0.0.1" ,

Port: 3306 ,

User: "root" ,

Password: "123456" ,

}

syncer := replication. NewBinlogSyncer (cfg)

// Start sync with specified binlog file and position

streamer , _ := syncer. StartSync (mysql.Position{ "mysql.000001" , 405322 })

// or you can start a gtid replication like

// streamer, _ := syncer.StartSyncGTID(gtidSet)

// the mysql GTID set likes this "de278ad0-2106-11e4-9f8e-6edd0ca20947:1-2"

// the mariadb GTID set likes this "0-1-100"

for {

ev , _ := streamer. GetEvent (context. Background ())

// Dump event

ev. Dump (os.Stdout)

}

// or we can use a timeout context

for {

ctx , cancel := context. WithTimeout (context. Background (), 2 *time.Second)

ev , err := streamer. GetEvent (ctx)

cancel ()

if err == context.DeadlineExceeded {

// meet timeout

continue

}

ev. Dump (os.Stdout)

}

}


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

深度探索C++对象模型

深度探索C++对象模型

斯坦利•B.李普曼 (Stanley B. Lippman) / 侯捷 / 电子工业出版社 / 2012-1 / 69.00元

作者Lippman参与设计了全世界第一套C++编译程序cfront,这本书就是一位伟大的C++编译程序设计者向你阐述他如何处理各种explicit(明确出现于C++程序代码中)和implicit(隐藏于程序代码背后)的C++语意。 本书专注于C++面向对象程序设计的底层机制,包括结构式语意、临时性对象的生成、封装、继承,以及虚拟——虚拟函数和虚拟继承。这本书让你知道:一旦你能够了解底层实现模......一起来看看 《深度探索C++对象模型》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码