Implement a custom MySQL server with the Server package
masterThe server package provides a framework for building MySQL-compatible servers or proxies. It is compatible with MySQL 5.5, 5.6, 5.7, and 8.0 clients.
server.NewConn() provides a default configuration that:
- Automatically generates default TLS/SSL certificates.
- Supports
mysql_native_password,caching_sha2_password, andsha256_password(defaults tomysql_native_password). - Uses an in-memory user credential provider.
To use custom configurations, use server.NewServer() and create connections via server.NewCustomizedConn().
package main
import (
"log"
"net"
"github.com/go-mysql-org/go-mysql/server"
)
func main() {
// Listen for connections on localhost port 4000
l, err := net.Listen("tcp", "127.0.0.1:4000")
if err != nil {
log.Fatal(err)
}
// Accept a new connection once
c, err := l.Accept()
if err != nil {
log.Fatal(err)
}
// Create a connection with user root and an empty password.
conn, err := server.NewConn(c, "root", "", server.EmptyHandler{})
if err != nil {
log.Fatal(err)
}
// Handle commands as long as the client keeps sending them
for {
if err := conn.HandleCommand(); err != nil {
log.Fatal(err)
}
}
}