{"id":15048243,"url":"https://github.com/github/go-mysql","last_synced_at":"2025-10-04T07:31:46.481Z","repository":{"id":65974847,"uuid":"71760309","full_name":"github/go-mysql","owner":"github","description":"a powerful mysql toolset with Go","archived":true,"fork":true,"pushed_at":"2016-11-01T11:24:06.000Z","size":714,"stargazers_count":8,"open_issues_count":0,"forks_count":9,"subscribers_count":29,"default_branch":"master","last_synced_at":"2024-09-30T00:20:33.873Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"go-mysql-org/go-mysql","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/github.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-24T06:53:09.000Z","updated_at":"2024-07-31T03:20:18.000Z","dependencies_parsed_at":"2023-02-19T18:01:01.032Z","dependency_job_id":null,"html_url":"https://github.com/github/go-mysql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgo-mysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgo-mysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgo-mysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgo-mysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/github","download_url":"https://codeload.github.com/github/go-mysql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235227462,"owners_count":18956137,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T21:09:42.805Z","updated_at":"2025-10-04T07:31:41.103Z","avatar_url":"https://github.com/github.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-mysql\n\nA pure go library to handle MySQL network protocol and replication.\n\n## Replication\n\nReplication package handles MySQL replication protocol like [python-mysql-replication](https://github.com/noplay/python-mysql-replication).\n\nYou can use it as a MySQL slave to sync binlog from master then do something, like updating cache, etc...\n\n### Example\n\n```go\nimport (\n    \"github.com/siddontang/go-mysql/replication\"\n    \"os\"\n)\n// Create a binlog syncer with a unique server id, the server id must be different from other MySQL's. \n// flavor is mysql or mariadb\ncfg := replication.BinlogSyncerConfig {\n    ServerID: 100,\n    Flavor:   \"mysql\",\n    Host:     \"127.0.0.1\",\n    Port:     3306,\n    User:     \"root\",\n    Password: \"\",\n}\nsyncer := replication.NewBinlogSyncer(\u0026cfg)\n\n// Start sync with sepcified binlog file and position\nstreamer, _ := syncer.StartSync(mysql.Position{binlogFile, binlogPos})\n\n// or you can start a gtid replication like\n// streamer, _ := syncer.StartSyncGTID(gtidSet)\n// the mysql GTID set likes this \"de278ad0-2106-11e4-9f8e-6edd0ca20947:1-2\"\n// the mariadb GTID set likes this \"0-1-100\"\n\nfor {\n    ev, _ := streamer.GetEvent(context.Background())\n    // Dump event\n    ev.Dump(os.Stdout)\n}\n\n// or we can use a timeout context\nfor {\n    ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)\n    e, _ := s.GetEvent(ctx)\n    cancel()\n\n    if err == context.DeadlineExceeded {\n        // meet timeout\n        continue\n    }\n\n    ev.Dump(os.Stdout)\n}\n```\n\nThe output looks:\n\n```\n=== RotateEvent ===\nDate: 1970-01-01 08:00:00\nLog position: 0\nEvent size: 43\nPosition: 4\nNext log name: mysql.000002\n\n=== FormatDescriptionEvent ===\nDate: 2014-12-18 16:36:09\nLog position: 120\nEvent size: 116\nVersion: 4\nServer version: 5.6.19-log\nCreate date: 2014-12-18 16:36:09\n\n=== QueryEvent ===\nDate: 2014-12-18 16:38:24\nLog position: 259\nEvent size: 139\nSalve proxy ID: 1\nExecution time: 0\nError code: 0\nSchema: test\nQuery: DROP TABLE IF EXISTS `test_replication` /* generated by server */\n```\n\n## Canal \n\nCanal is a package that can sync your MySQL into everywhere, like Redis, Elasticsearch. \n\nFirst, canal will dump your MySQL data then sync changed data using binlog incrementally. \n\nYou 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. \n\nA simple example:\n\n```\ncfg := NewDefaultConfig()\ncfg.Addr = \"127.0.0.1:3306\"\ncfg.User = \"root\"\n// We only care table canal_test in test db\ncfg.Dump.TableDB = \"test\"\ncfg.Dump.Tables = []string{\"canal_test\"}\n\nc, err := NewCanal(cfg)\n\ntype myRowsEventHandler struct {\n}\n\nfunc (h *myRowsEventHandler) Do(e *RowsEvent) error {\n    log.Infof(\"%s %v\\n\", e.Action, e.Rows)\n    return nil\n}\n\nfunc (h *myRowsEventHandler) String() string {\n    return \"myRowsEventHandler\"\n}\n\n// Register a handler to handle RowsEvent\nc.RegRowsEventHandler(\u0026MyRowsEventHandler{})\n\n// Start canal\nc.Start()\n```\n\nYou can see [go-mysql-elasticsearch](https://github.com/siddontang/go-mysql-elasticsearch) for how to sync MySQL data into Elasticsearch. \n\n## Client\n\nClient package supports a simple MySQL connection driver which you can use it to communicate with MySQL server. \n\n### Example\n\n```go\nimport (\n    \"github.com/siddontang/go-mysql/client\"\n)\n\n// Connect MySQL at 127.0.0.1:3306, with user root, an empty passowrd and database test\nconn, _ := client.Connect(\"127.0.0.1:3306\", \"root\", \"\", \"test\")\n\nconn.Ping()\n\n// Insert\nr, _ := conn.Execute(`insert into table (id, name) values (1, \"abc\")`)\n\n// Get last insert id\nprintln(r.InsertId)\n\n// Select\nr, _ := conn.Execute(`select id, name from table where id = 1`)\n\n// Handle resultset\nv, _ := r.GetInt(0, 0)\nv, _ = r.GetIntByName(0, \"id\") \n```\n\n## Server\n\nServer package supplies a framework to implement a simple MySQL server which can handle the packets from the MySQL client. \nYou can use it to build your own MySQL proxy. \n\n### Example\n\n```go\nimport (\n    \"github.com/siddontang/go-mysql/server\"\n    \"net\"\n)\n\nl, _ := net.Listen(\"tcp\", \"127.0.0.1:4000\")\n\nc, _ := l.Accept()\n\n// Create a connection with user root and an empty passowrd\n// We only an empty handler to handle command too\nconn, _ := server.NewConn(c, \"root\", \"\", server.EmptyHandler{})\n\nfor {\n    conn.HandleCommand()\n}\n```\n\nAnother shell\n\n```\nmysql -h127.0.0.1 -P4000 -uroot -p \n//Becuase empty handler does nothing, so here the MySQL client can only connect the proxy server. :-) \n```\n\n## Failover\n\nFailover supports to promote a new master and let other slaves replicate from it automatically when the old master was down.\n\nFailover supports MySQL \u003e= 5.6.9 with GTID mode, if you use lower version, e.g, MySQL 5.0 - 5.5, please use [MHA](http://code.google.com/p/mysql-master-ha/) or [orchestrator](https://github.com/outbrain/orchestrator).\n\nAt the same time, Failover supports MariaDB \u003e= 10.0.9 with GTID mode too. \n\nWhy 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. \nAlthough there are many companies use MySQL 5.0 - 5.5, I think upgrade MySQL to 5.6 or higher is easy. \n\n## Driver\n\nDriver is the package that you can use go-mysql with go database/sql like other drivers. A simple example:\n\n```\nimport (\n    \"database/sql\"\n\n    - \"github.com/siddontang/go-mysql/driver\"\n)\n\nfunc main() {\n    // dsn format: \"user:password@addr?dbname\"\n    dsn := \"root@127.0.0.1:3306?test\"\n    db, _ := sql.Open(dsn)\n    db.Close()\n}\n```\n\nWe pass all tests in https://github.com/bradfitz/go-sql-test using go-mysql driver. :-)\n\n## Feedback\n\ngo-mysql is still in development, your feedback is very welcome. \n\n\nGmail: siddontang@gmail.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub%2Fgo-mysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgithub%2Fgo-mysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub%2Fgo-mysql/lists"}