Skip to content

Commit 6478e8c

Browse files
committed
server: Improve example
1. Use the example from the `README.md` and turn it into a `go-mysqlserver` binary that users can run. 2. Add more logging to make it easier to understand what it is doing. This is done both in `go-mysqlserver` as well as the `EmptyHandler` 3. Remove the `server/example` as we already have a example now. 4. Support the minimal set of queries that MySQL Shell `mysqlsh` needs to be able to connect. (tested with MySQL Shell 9.1.0) 5. Change the default version from 5.7.0 to 8.0.11 (first 8.0 GA version)
1 parent cadcdcc commit 6478e8c

File tree

6 files changed

+78
-57
lines changed

6 files changed

+78
-57
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ build:
99
${GO} build -o bin/go-mysqldump cmd/go-mysqldump/main.go
1010
${GO} build -o bin/go-canal cmd/go-canal/main.go
1111
${GO} build -o bin/go-binlogparser cmd/go-binlogparser/main.go
12+
${GO} build -o bin/go-mysqlserver cmd/go-mysqlserver/main.go
1213

1314
test:
1415
${GO} test --race -timeout 2m ./...

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ func main() {
305305
}
306306
}
307307
}
308-
309308
```
310309

311310
Another shell

cmd/go-mysqlserver/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net"
6+
7+
"github.com/go-mysql-org/go-mysql/server"
8+
)
9+
10+
func main() {
11+
// Listen for connections on localhost port 4000
12+
l, err := net.Listen("tcp", "127.0.0.1:4000")
13+
if err != nil {
14+
log.Fatal(err)
15+
}
16+
17+
log.Println("Listening on port 4000, connect with 'mysql -h 127.0.0.1 -P 4000 -u root'")
18+
19+
// Accept a new connection once
20+
c, err := l.Accept()
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
log.Println("Accepted connection")
26+
27+
// Create a connection with user root and an empty password.
28+
// You can use your own handler to handle command here.
29+
conn, err := server.NewConn(c, "root", "", server.EmptyHandler{})
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
34+
log.Println("Registered the connection with the server")
35+
36+
// as long as the client keeps sending commands, keep handling them
37+
for {
38+
if err := conn.HandleCommand(); err != nil {
39+
log.Fatal(err)
40+
}
41+
}
42+
}

server/command.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package server
33
import (
44
"bytes"
55
"fmt"
6+
"log"
67

8+
"github.com/go-mysql-org/go-mysql/mysql"
79
. "github.com/go-mysql-org/go-mysql/mysql"
810
"github.com/go-mysql-org/go-mysql/replication"
911
"github.com/siddontang/go/hack"
@@ -186,39 +188,71 @@ type EmptyReplicationHandler struct {
186188
}
187189

188190
func (h EmptyHandler) UseDB(dbName string) error {
191+
log.Printf("Received: UseDB %s", dbName)
189192
return nil
190193
}
191194
func (h EmptyHandler) HandleQuery(query string) (*Result, error) {
195+
log.Printf("Received: Query: %s", query)
196+
197+
// These two queries are implemented for minimal support for MySQL Shell
198+
if query == `SET NAMES 'utf8mb4';` {
199+
return nil, nil
200+
}
201+
if query == `select concat(@@version, ' ', @@version_comment)` {
202+
r, err := mysql.BuildSimpleResultset([]string{"concat(@@version, ' ', @@version_comment)"}, [][]interface{}{
203+
{"8.0.11"},
204+
}, false)
205+
if err != nil {
206+
return nil, err
207+
} else {
208+
return &mysql.Result{
209+
Status: 0,
210+
Warnings: 0,
211+
InsertId: 0,
212+
AffectedRows: 0,
213+
Resultset: r,
214+
}, nil
215+
}
216+
}
217+
192218
return nil, fmt.Errorf("not supported now")
193219
}
194220

195221
func (h EmptyHandler) HandleFieldList(table string, fieldWildcard string) ([]*Field, error) {
222+
log.Println("Received: FieldList")
196223
return nil, fmt.Errorf("not supported now")
197224
}
198225
func (h EmptyHandler) HandleStmtPrepare(query string) (int, int, interface{}, error) {
226+
log.Printf("Received: StmtPrepare: %s", query)
199227
return 0, 0, nil, fmt.Errorf("not supported now")
200228
}
201229
func (h EmptyHandler) HandleStmtExecute(context interface{}, query string, args []interface{}) (*Result, error) {
230+
log.Println("Received: StmtExecute")
202231
return nil, fmt.Errorf("not supported now")
203232
}
204233

205234
func (h EmptyHandler) HandleStmtClose(context interface{}) error {
235+
log.Println("Received: StmtClose")
206236
return nil
207237
}
208238

209239
func (h EmptyReplicationHandler) HandleRegisterSlave(data []byte) error {
240+
log.Println("Received: RegisterSlave")
210241
return fmt.Errorf("not supported now")
211242
}
212243

213244
func (h EmptyReplicationHandler) HandleBinlogDump(pos Position) (*replication.BinlogStreamer, error) {
245+
log.Println("Received: BinlogDump")
214246
return nil, fmt.Errorf("not supported now")
215247
}
216248

217249
func (h EmptyReplicationHandler) HandleBinlogDumpGTID(gtidSet *MysqlGTIDSet) (*replication.BinlogStreamer, error) {
250+
log.Println("Received: BinlogDumpGTID")
218251
return nil, fmt.Errorf("not supported now")
219252
}
220253

221254
func (h EmptyHandler) HandleOtherCommand(cmd byte, data []byte) error {
255+
log.Println("Received: OtherCommand")
222256
return NewError(
223257
ER_UNKNOWN_ERROR,
224258
fmt.Sprintf("command %d is not supported now", cmd),

server/example/server_example.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

server/server_conf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewDefaultServer() *Server {
4848
certPem, keyPem := generateAndSignRSACerts(caPem, caKey)
4949
tlsConf := NewServerTLSConfig(caPem, certPem, keyPem, tls.VerifyClientCertIfGiven)
5050
return &Server{
51-
serverVersion: "5.7.0",
51+
serverVersion: "8.0.11",
5252
protocolVersion: 10,
5353
capability: CLIENT_LONG_PASSWORD | CLIENT_LONG_FLAG | CLIENT_CONNECT_WITH_DB | CLIENT_PROTOCOL_41 |
5454
CLIENT_TRANSACTIONS | CLIENT_SECURE_CONNECTION | CLIENT_PLUGIN_AUTH | CLIENT_SSL | CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA,

0 commit comments

Comments
 (0)