|
1 | 1 | # go-mysql |
2 | 2 |
|
3 | | -A pure go library to handle MySQL network protocol and replication. |
| 3 | +A pure go library to handle MySQL network protocol and replication as used by MySQL and MariaDB. |
4 | 4 |
|
5 | 5 |  |
6 | 6 |  |
7 | 7 |  |
8 | | - |
9 | | -## How to migrate to this repo |
10 | | -To change the used package in your repo it's enough to add this `replace` directive to your `go.mod`: |
11 | | -``` |
12 | | -replace github.com/siddontang/go-mysql => github.com/go-mysql-org/go-mysql v1.10.0 |
13 | | -``` |
14 | | - |
15 | | -v1.10.0 - is the last tag in repo, feel free to choose what you want. |
| 8 | +[](https://pkg.go.dev/github.com/go-mysql-org/go-mysql) |
16 | 9 |
|
17 | 10 | ## Changelog |
18 | 11 | This repo uses [Changelog](CHANGELOG.md). |
19 | 12 |
|
20 | 13 | --- |
21 | 14 | # Content |
22 | | -* [Replication](#replication) |
23 | | -* [Incremental dumping](#canal) |
24 | | -* [Client](#client) |
25 | | -* [Fake server](#server) |
26 | | -* [database/sql like driver](#driver) |
27 | | -* [Logging](#logging) |
| 15 | +* [Replication](#replication) - Process events from a binlog stream. |
| 16 | +* [Incremental dumping](#canal) - Sync from MySQL to Redis, Elasticsearch, etc. |
| 17 | +* [Client](#client) - Simple MySQL client. |
| 18 | +* [Fake server](#server) - server side of the MySQL protocol, as library. |
| 19 | +* [database/sql like driver](#driver) - An alternative `database/sql` driver for MySQL. |
| 20 | +* [Logging](#logging) - Custom logging options. |
| 21 | +* [Migration](#how-to-migrate-to-this-repo) - Information for how to migrate if you used the old location of this project. |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | +The `cmd` directory contains example applications that can be build by running `make build` in the root of the project. The resulting binaries will be places in `bin/`. |
| 26 | + |
| 27 | +- `go-binlogparser`: parses a binlog file at a given offset |
| 28 | +- `go-canal`: streams binlog events from a server to canal |
| 29 | +- `go-mysqlbinlog`: streams binlog events |
| 30 | +- `go-mysqldump`: like `mysqldump`, but in Go |
| 31 | +- `go-mysqlserver`: fake MySQL server |
28 | 32 |
|
29 | 33 | ## Replication |
30 | 34 |
|
@@ -55,9 +59,10 @@ syncer := replication.NewBinlogSyncer(cfg) |
55 | 59 | streamer, _ := syncer.StartSync(mysql.Position{binlogFile, binlogPos}) |
56 | 60 |
|
57 | 61 | // or you can start a gtid replication like |
| 62 | +// gtidSet, _ := mysql.ParseGTIDSet(mysql.MySQLFlavor, "de278ad0-2106-11e4-9f8e-6edd0ca20947:1-2") |
58 | 63 | // streamer, _ := syncer.StartSyncGTID(gtidSet) |
59 | | -// the mysql GTID set likes this "de278ad0-2106-11e4-9f8e-6edd0ca20947:1-2" |
60 | | -// the mariadb GTID set likes this "0-1-100" |
| 64 | +// the mysql GTID set is like this "de278ad0-2106-11e4-9f8e-6edd0ca20947:1-2" and uses mysql.MySQLFlavor |
| 65 | +// the mariadb GTID set is like this "0-1-100" and uses mysql.MariaDBFlavor |
61 | 66 |
|
62 | 67 | for { |
63 | 68 | ev, _ := streamer.GetEvent(context.Background()) |
@@ -111,7 +116,7 @@ Query: DROP TABLE IF EXISTS `test_replication` /* generated by server */ |
111 | 116 |
|
112 | 117 | ## Canal |
113 | 118 |
|
114 | | -Canal is a package that can sync your MySQL into everywhere, like Redis, Elasticsearch. |
| 119 | +Canal is a package that can sync your MySQL into everywhere, like Redis, Elasticsearch. |
115 | 120 |
|
116 | 121 | First, canal will dump your MySQL data then sync changed data using binlog incrementally. |
117 | 122 |
|
@@ -161,7 +166,7 @@ func main() { |
161 | 166 | } |
162 | 167 | ``` |
163 | 168 |
|
164 | | -You can see [go-mysql-elasticsearch](https://github.com/siddontang/go-mysql-elasticsearch) for how to sync MySQL data into Elasticsearch. |
| 169 | +You can see [go-mysql-elasticsearch](https://github.com/go-mysql-org/go-mysql-elasticsearch) for how to sync MySQL data into Elasticsearch. |
165 | 170 |
|
166 | 171 | ## Client |
167 | 172 |
|
@@ -342,7 +347,7 @@ import ( |
342 | 347 | func main() { |
343 | 348 | // dsn format: "user:password@addr?dbname" |
344 | 349 | dsn := "[email protected]:3306?test" |
345 | | - db, _ := sql.Open(dsn) |
| 350 | + db, _ := sql.Open("mysql", dsn) |
346 | 351 | db.Close() |
347 | 352 | } |
348 | 353 | ``` |
@@ -455,6 +460,25 @@ func main() { |
455 | 460 | } |
456 | 461 | ``` |
457 | 462 |
|
| 463 | +### Custom Driver Name |
| 464 | + |
| 465 | +A custom driver name can be set via build options: `-ldflags '-X "github.com/go-mysql-org/go-mysql/driver.driverName=gomysql"'`. |
| 466 | + |
| 467 | +This can be useful when using [GORM](https://gorm.io/docs/connecting_to_the_database.html#Customize-Driver): |
| 468 | + |
| 469 | +```go |
| 470 | +import ( |
| 471 | + _ "github.com/go-mysql-org/go-mysql/driver" |
| 472 | + "gorm.io/driver/mysql" |
| 473 | + "gorm.io/gorm" |
| 474 | +) |
| 475 | + |
| 476 | +db, err := gorm.Open(mysql.New(mysql.Config{ |
| 477 | + DriverName: "gomysql", |
| 478 | + DSN: "gorm:[email protected]:3306/test", |
| 479 | +})) |
| 480 | +``` |
| 481 | + |
458 | 482 | ### Custom NamedValueChecker |
459 | 483 |
|
460 | 484 | Golang allows for custom handling of query arguments before they are passed to the driver |
@@ -516,17 +540,19 @@ import "github.com/siddontang/go-log/log" |
516 | 540 |
|
517 | 541 | Or you can implement your own [`log.Handler`](https://pkg.go.dev/github.com/siddontang/go-log/log#Handler). |
518 | 542 |
|
519 | | -## Donate |
520 | | - |
521 | | -If you like the project and want to buy me a cola, you can through: |
522 | | - |
523 | | -|PayPal|微信| |
524 | | -|------|---| |
525 | | -|[](https://paypal.me/siddontang)|[| |
| 543 | +## How to migrate to this repo |
| 544 | +To change the used package in your repo it's enough to add this `replace` directive to your `go.mod`: |
| 545 | +``` |
| 546 | +replace github.com/siddontang/go-mysql => github.com/go-mysql-org/go-mysql v1.10.0 |
| 547 | +``` |
526 | 548 |
|
527 | | -## Feedback |
| 549 | +This can be done by running this command: |
| 550 | +``` |
| 551 | +go mod edit -replace=github.com/siddontang/go-mysql=github.com/go-mysql-org/[email protected] |
| 552 | +``` |
528 | 553 |
|
529 | | -go-mysql is still in development, your feedback is very welcome. |
| 554 | +v1.10.0 - is the last tag in repo, feel free to choose what you want. |
530 | 555 |
|
| 556 | +## Credits |
531 | 557 |
|
532 | | -Gmail: siddontang@gmail.com |
| 558 | +go-mysql was started by @siddontang and has many [contributors](https://github.com/go-mysql-org/go-mysql/graphs/contributors) |
0 commit comments