go-mysqlstack is an MySQL protocol library implementing in Go (golang).

使用示例

  1. 下载
1
2
3
go get -u github.com/xelabs/go-mysqlstack/driver
cd $GOPATH/src/github.com/xelabs/go-mysqlstack/
make test
  1. examples/mysqld.go mocks a MySQL server by running:
1
2
> go run mysqld.go
 2020/08/22 19:01:44.518045 mysqld.go:102:       [INFO]         mysqld.server.start.address[:4407]
  1. examples/client.go mocks a client and query from the mock MySQL server:
1
2
> go run client.go
2020/08/22 19:01:50.530677 client.go:32:        [INFO]         results:[[[10 nice name]]]

Handler接口

NewListener方法传入监控地址和handler接口实现即可模拟MySQL服务器接收MySQL客户端请求

1
2
// NewListener creates a new Listener.
func NewListener(log *xlog.Log, address string, handler Handler) (*Listener, error) {

Handler接口方法,实现这些方法来处理客户端连接和请求,driver/mock.go是一个测试实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Handler interface.
type Handler interface {
    ServerVersion() string
    SetServerVersion()
    NewSession(session *Session)
    SessionInc(session *Session)
    SessionDec(session *Session)
    SessionClosed(session *Session)
    SessionCheck(session *Session) error
    AuthCheck(session *Session) error
    ComInitDB(session *Session, database string) error
    ComQuery(session *Session, query string, bindVariables map[string]*querypb.BindVariable, callback func(*sqltypes.Result) error) error
}

使用限制

  1. 只支持mysql_native_password插件