mysql

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package mysql implements MySQL protocol compatibility for XxSql.

Package mysql implements MySQL protocol compatibility for XxSql.

Index

Constants

View Source
const (
	ProtocolVersion = 10
	MaxPacketSize   = 1<<24 - 1 // 16MB

	// Client capabilities
	CLIENT_LONG_PASSWORD                  = 0x00000001
	CLIENT_FOUND_ROWS                     = 0x00000002
	CLIENT_LONG_FLAG                      = 0x00000004
	CLIENT_CONNECT_WITH_DB                = 0x00000008
	CLIENT_NO_SCHEMA                      = 0x00000010
	CLIENT_COMPRESS                       = 0x00000020
	CLIENT_ODBC                           = 0x00000040
	CLIENT_LOCAL_FILES                    = 0x00000080
	CLIENT_IGNORE_SPACE                   = 0x00000100
	CLIENT_PROTOCOL_41                    = 0x00000200
	CLIENT_INTERACTIVE                    = 0x00000400
	CLIENT_SSL                            = 0x00000800
	CLIENT_IGNORE_SIGPIPE                 = 0x00001000
	CLIENT_TRANSACTIONS                   = 0x00002000
	CLIENT_SECURE_CONN                    = 0x00008000
	CLIENT_MULTI_STATEMENTS               = 0x00010000
	CLIENT_MULTI_RESULTS                  = 0x00020000
	CLIENT_PS_MULTI_RESULTS               = 0x00040000
	CLIENT_PLUGIN_AUTH                    = 0x00080000
	CLIENT_CONNECT_ATTRS                  = 0x00100000
	CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 0x00200000
	CLIENT_DEPRECATE_EOF                  = 0x01000000

	// Server capabilities (what we support)
	DefaultServerCapabilities = CLIENT_LONG_PASSWORD |
		CLIENT_FOUND_ROWS |
		CLIENT_LONG_FLAG |
		CLIENT_CONNECT_WITH_DB |
		CLIENT_PROTOCOL_41 |
		CLIENT_TRANSACTIONS |
		CLIENT_SECURE_CONN |
		CLIENT_MULTI_STATEMENTS |
		CLIENT_MULTI_RESULTS |
		CLIENT_PLUGIN_AUTH |
		CLIENT_DEPRECATE_EOF

	// Character sets
	CharacterSetUTF8MB4 = 45

	// Command types
	COM_SLEEP            = 0x00
	COM_QUIT             = 0x01
	COM_INIT_DB          = 0x02
	COM_QUERY            = 0x03
	COM_FIELD_LIST       = 0x04
	COM_CREATE_DB        = 0x05
	COM_DROP_DB          = 0x06
	COM_REFRESH          = 0x07
	COM_SHUTDOWN         = 0x08
	COM_STATISTICS       = 0x09
	COM_PROCESS_INFO     = 0x0A
	COM_CONNECT          = 0x0B
	COM_PROCESS_KILL     = 0x0C
	COM_DEBUG            = 0x0D
	COM_PING             = 0x0E
	COM_CHANGE_USER      = 0x11
	COM_RESET_CONNECTION = 0x1F

	// Response types
	OK_PACKET  = 0x00
	ERR_PACKET = 0xFF
	EOF_PACKET = 0xFE

	// Auth plugin names
	AuthPluginNativePassword = "mysql_native_password"
	AuthPluginCachingSHA2    = "caching_sha2_password"
)

MySQL protocol constants

Variables

This section is empty.

Functions

func MySQLAuthPassword

func MySQLAuthPassword(password, salt, authResponse []byte) bool

MySQLAuthPassword verifies MySQL native password authentication.

Types

type ClientHandshakePacket

type ClientHandshakePacket struct {
	CapabilityFlags uint32
	MaxPacketSize   uint32
	CharacterSet    uint8
	Username        string
	AuthResponse    []byte
	Database        string
	AuthPluginName  string
}

ClientHandshakePacket is the response from the client.

type ColumnDefinition

type ColumnDefinition struct {
	Catalog  string
	Schema   string
	Table    string
	OrgTable string
	Name     string
	OrgName  string
	Charset  uint16
	Length   uint32
	Type     uint8
	Flags    uint16
	Decimals uint8
}

ColumnDefinition represents a column definition packet.

type ERRPacket

type ERRPacket struct {
	ErrorCode    uint16
	SQLState     string
	ErrorMessage string
}

ERRPacket represents an error response packet.

type MySQLHandler

type MySQLHandler struct {
	// contains filtered or unexported fields
}

MySQLHandler handles a MySQL protocol connection.

func NewMySQLHandler

func NewMySQLHandler(conn net.Conn, connectionID uint32, opts ...MySQLHandlerOption) *MySQLHandler

NewMySQLHandler creates a new MySQL handler.

func (*MySQLHandler) AuthPluginData

func (h *MySQLHandler) AuthPluginData() []byte

AuthPluginData returns the auth plugin data (salt) used for authentication.

func (*MySQLHandler) Close

func (h *MySQLHandler) Close() error

Close closes the connection.

func (*MySQLHandler) ConnectionID

func (h *MySQLHandler) ConnectionID() uint32

ConnectionID returns the connection ID.

func (*MySQLHandler) Database

func (h *MySQLHandler) Database() string

Database returns the current database.

func (*MySQLHandler) Handle

func (h *MySQLHandler) Handle() error

Handle handles the MySQL connection.

func (*MySQLHandler) IsClosed

func (h *MySQLHandler) IsClosed() bool

IsClosed returns whether the connection is closed.

func (*MySQLHandler) SessionID

func (h *MySQLHandler) SessionID() string

SessionID returns the session ID.

func (*MySQLHandler) SetSessionID

func (h *MySQLHandler) SetSessionID(id string)

SetSessionID sets the session ID.

func (*MySQLHandler) Username

func (h *MySQLHandler) Username() string

Username returns the authenticated username.

type MySQLHandlerOption

type MySQLHandlerOption func(*MySQLHandler)

MySQLHandlerOption is a functional option for MySQLHandler.

func WithMySQLAuthHandler

func WithMySQLAuthHandler(fn func(h *MySQLHandler, username, database string, authResponse []byte) (bool, error)) MySQLHandlerOption

WithMySQLAuthHandler sets the auth handler.

func WithMySQLCloseHandler

func WithMySQLCloseHandler(fn func(h *MySQLHandler)) MySQLHandlerOption

WithMySQLCloseHandler sets the close handler.

func WithMySQLFieldListHandler

func WithMySQLFieldListHandler(fn func(h *MySQLHandler, table string) ([]*ColumnDefinition, error)) MySQLHandlerOption

WithMySQLFieldListHandler sets the field list handler.

func WithMySQLQueryHandler

func WithMySQLQueryHandler(fn func(h *MySQLHandler, sql string) ([]*ColumnDefinition, [][]interface{}, error)) MySQLHandlerOption

WithMySQLQueryHandler sets the query handler.

type OKPacket

type OKPacket struct {
	AffectedRows uint64
	LastInsertID uint64
	StatusFlags  uint16
	Warnings     uint16
	Info         string
}

OKPacket represents an OK response packet.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a MySQL protocol server.

func NewServer

func NewServer(config *ServerConfig) *Server

NewServer creates a new MySQL server.

func (*Server) Addr

func (s *Server) Addr() net.Addr

Addr returns the server address.

func (*Server) ConnectionCount

func (s *Server) ConnectionCount() int

ConnectionCount returns the current connection count.

func (*Server) Connections

func (s *Server) Connections() []*MySQLHandler

Connections returns a slice of active connections.

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns whether the server is running.

func (*Server) SetAuthHandler

func (s *Server) SetAuthHandler(fn func(h *MySQLHandler, username, database string, authResponse []byte) (bool, error))

SetAuthHandler sets the authentication handler.

func (*Server) SetCloseHandler

func (s *Server) SetCloseHandler(fn func(h *MySQLHandler))

SetCloseHandler sets the close handler.

func (*Server) SetFieldListHandler

func (s *Server) SetFieldListHandler(fn func(h *MySQLHandler, table string) ([]*ColumnDefinition, error))

SetFieldListHandler sets the field list handler.

func (*Server) SetQueryHandler

func (s *Server) SetQueryHandler(fn func(h *MySQLHandler, sql string) ([]*ColumnDefinition, [][]interface{}, error))

SetQueryHandler sets the query handler.

func (*Server) Start

func (s *Server) Start() error

Start starts the MySQL server.

func (*Server) Stats

func (s *Server) Stats() ServerStats

Stats returns server statistics.

func (*Server) Stop

func (s *Server) Stop() error

Stop stops the MySQL server.

type ServerConfig

type ServerConfig struct {
	Bind           string
	Port           int
	MaxConnections int
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
	AcceptBacklog  int
}

ServerConfig holds MySQL server configuration.

func DefaultServerConfig

func DefaultServerConfig() *ServerConfig

DefaultServerConfig returns default MySQL server configuration.

type ServerHandshakePacket

type ServerHandshakePacket struct {
	ProtocolVersion     uint8
	ServerVersion       string
	ConnectionID        uint32
	AuthPluginDataPart1 []byte // 8 bytes
	CapabilityFlags1    uint16
	CharacterSet        uint8
	StatusFlags         uint16
	CapabilityFlags2    uint16
	AuthPluginDataLen   uint8
	AuthPluginDataPart2 []byte // 12 bytes
	AuthPluginName      string
}

ServerHandshakePacket is the initial handshake packet sent by the server.

type ServerStats

type ServerStats struct {
	Running         bool   `json:"running"`
	ConnectionCount int    `json:"connection_count"`
	Addr            string `json:"addr"`
}

ServerStats holds MySQL server statistics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL