第一个go-mysql-binlog解析

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

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)

}

}


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

查看所有标签

猜你喜欢:

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

Vim实用技巧

Vim实用技巧

[英] Drew Neil / 杨源、车文隆 / 人民邮电出版社 / 2014-5-1 / 59.00元

vim是一款功能丰富而强大的文本编辑器,其代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中得到非常广泛的使用。vim能够大大提高程序员的工作效率。对于vim高手来说,vim能以与思考同步的速度编辑文本。同时,学习和熟练使用vim又有一定的难度。 《vim实用技巧》为那些想要提升自己的程序员编写,阅读本书是熟练地掌握高超的vim技巧的必由之路。全书共21章,包括121个技巧。每一章......一起来看看 《Vim实用技巧》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

在线进制转换器
在线进制转换器

各进制数互转换器

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

Base64 编码/解码