• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

go-mysql: a powerful mysql toolset with Go

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

go-mysql

开源软件地址:

https://gitee.com/siddontang/go-mysql

开源软件介绍:

go-mysql

A pure go library to handle MySQL network protocol and replication.

Replication

Replication package handles MySQL replication protocol like python-mysql-replication.

You can use it as a MySQL slave to sync binlog from master then do something, like updating cache, etc...

Example

import (    "github.com/siddontang/go-mysql/replication"    "os")// Create a binlog syncer with a unique server id, the server id must be different from other MySQL's. // flavor is mysql or mariadbsyncer := replication.NewBinlogSyncer(100, "mysql")// Register slave, the MySQL master is at 127.0.0.1:3306, with user root and an empty passwordsyncer.RegisterSlave("127.0.0.1", 3306, "root", "")// Start sync with sepcified binlog file and positionstreamer, _ := syncer.StartSync(binlogFile, binlogPos)// 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()    // Dump event    ev.Dump(os.Stdout)}

The output looks:

=== RotateEvent ===Date: 1970-01-01 08:00:00Log position: 0Event size: 43Position: 4Next log name: mysql.000002=== FormatDescriptionEvent ===Date: 2014-12-18 16:36:09Log position: 120Event size: 116Version: 4Server version: 5.6.19-logCreate date: 2014-12-18 16:36:09=== QueryEvent ===Date: 2014-12-18 16:38:24Log position: 259Event size: 139Salve proxy ID: 1Execution time: 0Error code: 0Schema: testQuery: DROP TABLE IF EXISTS `test_replication` /* generated by server */

Canal

Canal is a package that can sync your MySQL into everywhere, like Redis, Elasticsearch.

First, canal will dump your MySQL data then sync changed data using binlog incrementally.

You must use ROW format for binlog, full binlog row image is preferred, because we may meet some errors when primary key changed in update for minimal or noblob row image.

A simple example:

cfg := NewDefaultConfig()cfg.Addr = "127.0.0.1:3306"cfg.User = "root"// We only care table canal_test in test dbcfg.Dump.TableDB = "test"cfg.Dump.Tables = []string{"canal_test"}c, err := NewCanal(cfg)type myRowsEventHandler struct {}func (h *myRowsEventHandler) Do(e *RowsEvent) error {    log.Infof("%s %v\n", e.Action, e.Rows)    return nil}func (h *myRowsEventHandler) String() string {    return "myRowsEventHandler"}// Register a handler to handle RowsEventc.RegRowsEventHandler(&MyRowsEventHandler{})// Start canalc.Start()

You can see go-mysql-elasticsearch for how to sync MySQL data into Elasticsearch.

Client

Client package supports a simple MySQL connection driver which you can use it to communicate with MySQL server.

Example

import (    "github.com/siddontang/go-mysql/client")// Connect MySQL at 127.0.0.1:3306, with user root, an empty passowrd and database testconn, _ := client.Connect("127.0.0.1:3306", "root", "", "test")conn.Ping()// Insertr, _ := conn.Execute(`insert into table (id, name) values (1, "abc")`)// Get last insert idprintln(r.InsertId)// Selectr, _ := conn.Execute(`select id, name from table where id = 1`)// Handle resultsetv, _ := r.GetInt(0, 0)v, _ = r.GetIntByName(0, "id") 

Server

Server package supplies a framework to implement a simple MySQL server which can handle the packets from the MySQL client.You can use it to build your own MySQL proxy.

Example

import (    "github.com/siddontang/go-mysql/server"    "net")l, _ := net.Listen("127.0.0.1:4000")c, _ := l.Accept()// Create a connection with user root and an empty passowrd// We only an empty handler to handle command tooconn, _ := server.NewConn(c, "root", "", server.EmptyHandler{})for {    conn.HandleCommand()}

Another shell

mysql -h127.0.0.1 -P4000 -uroot -p //Becuase empty handler does nothing, so here the MySQL client can only connect the proxy server. :-) 

Failover

Failover supports to promote a new master and let other slaves replicate from it automatically when the old master was down.

Failover supports MySQL >= 5.6.9 with GTID mode, if you use lower version, e.g, MySQL 5.0 - 5.5, please use MHA or orchestrator.

At the same time, Failover supports MariaDB >= 10.0.9 with GTID mode too.

Why only GTID? Supporting failover with no GTID mode is very hard, because slave can not find the proper binlog filename and position with the new master.Although there are many companies use MySQL 5.0 - 5.5, I think upgrade MySQL to 5.6 or higher is easy.

Driver

Driver is the package that you can use go-mysql with go database/sql like other drivers. A simple example:

import (    "database/sql"    - "github.com/siddontang/go-mysql/driver")func main() {    // dsn format: "user:password@addr?dbname"    dsn := "[email protected]:3306?test"    db, _ := sql.Open(dsn)    db.Close()}

We pass all tests in https://github.com/bradfitz/go-sql-test using go-mysql driver. :-)

Feedback

go-mysql is still in development, your feedback is very welcome.

Gmail: [email protected]


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap