mysql

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 51 Imported by: 90

Documentation

Overview

Package mysql is a library to support MySQL binary protocol, both client and server sides. It also supports binlog event parsing.

Index

Constants

View Source
const (
	BinlogDumpNonBlock    = 0x01
	BinlogThroughPosition = 0x02
	BinlogThroughGTID     = 0x04
)
View Source
const (
	// Default length of the fixed header for v4 events.
	BinlogFixedHeaderLen = 19
	// The offset from 0 where the type is stored as 1 byte.
	BinlogEventTypeOffset = 4
	// Byte length of the checksum suffix when the CRC32 algorithm is used.
	BinlogCRC32ChecksumLen = 4
)
View Source
const (
	// Compression algorithms that are supported (only zstd today
	// in MySQL 8.0):
	// https://dev.mysql.com/doc/refman/8.0/en/binary-log-transaction-compression.html
	TransactionPayloadCompressionZstd = 0
	TransactionPayloadCompressionNone = 255
)
View Source
const (
	// MaxPacketSize is the maximum payload length of a packet
	// the server supports.
	MaxPacketSize = (1 << 24) - 1

	// https://dev.mysql.com/doc/refman/en/identifier-length.html
	MaxIdentifierLength = 64
)
View Source
const (
	// MysqlNativePassword uses a salt and transmits a hash on the wire.
	MysqlNativePassword = AuthMethodDescription("mysql_native_password")

	// MysqlClearPassword transmits the password in the clear.
	MysqlClearPassword = AuthMethodDescription("mysql_clear_password")

	// CachingSha2Password uses a salt and transmits a SHA256 hash on the wire.
	CachingSha2Password = AuthMethodDescription("caching_sha2_password")

	// MysqlDialog uses the dialog plugin on the client side.
	// It transmits data in the clear.
	MysqlDialog = AuthMethodDescription("dialog")
)

Supported auth forms.

View Source
const (
	// CapabilityClientLongPassword is CLIENT_LONG_PASSWORD.
	// New more secure passwords. Assumed to be set since 4.1.1.
	// We do not check this anywhere.
	CapabilityClientLongPassword = 1

	// CapabilityClientFoundRows is CLIENT_FOUND_ROWS.
	CapabilityClientFoundRows = 1 << 1

	// CapabilityClientLongFlag is CLIENT_LONG_FLAG.
	// Longer flags in Protocol::ColumnDefinition320.
	// Set it everywhere, not used, as we use Protocol::ColumnDefinition41.
	CapabilityClientLongFlag = 1 << 2

	// CapabilityClientConnectWithDB is CLIENT_CONNECT_WITH_DB.
	// One can specify db on connect.
	CapabilityClientConnectWithDB = 1 << 3

	// CapabilityClientProtocol41 is CLIENT_PROTOCOL_41.
	// New 4.1 protocol. Enforced everywhere.
	CapabilityClientProtocol41 = 1 << 9

	// CapabilityClientSSL is CLIENT_SSL.
	// Switch to SSL after handshake.
	CapabilityClientSSL = 1 << 11

	// CapabilityClientTransactions is CLIENT_TRANSACTIONS.
	// Can send status flags in EOF_Packet.
	// This flag is optional in 3.23, but always set by the server since 4.0.
	// We just do it all the time.
	CapabilityClientTransactions = 1 << 13

	// CapabilityClientSecureConnection is CLIENT_SECURE_CONNECTION.
	// New 4.1 authentication. Always set, expected, never checked.
	CapabilityClientSecureConnection = 1 << 15

	// CapabilityClientMultiStatements is CLIENT_MULTI_STATEMENTS
	// Can handle multiple statements per COM_QUERY and COM_STMT_PREPARE.
	CapabilityClientMultiStatements = 1 << 16

	// CapabilityClientMultiResults is CLIENT_MULTI_RESULTS
	// Can send multiple resultsets for COM_QUERY.
	CapabilityClientMultiResults = 1 << 17

	// CapabilityClientPluginAuth is CLIENT_PLUGIN_AUTH.
	// Client supports plugin authentication.
	CapabilityClientPluginAuth = 1 << 19

	// CapabilityClientConnAttr is CLIENT_CONNECT_ATTRS
	// Permits connection attributes in Protocol::HandshakeResponse41.
	CapabilityClientConnAttr = 1 << 20

	// CapabilityClientPluginAuthLenencClientData is CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
	CapabilityClientPluginAuthLenencClientData = 1 << 21

	// CLIENT_SESSION_TRACK 1 << 23
	// Can set ServerSessionStateChanged in the Status Flags
	// and send session-state change data after a OK packet.
	// Not yet supported.
	CapabilityClientSessionTrack = 1 << 23

	// CapabilityClientDeprecateEOF is CLIENT_DEPRECATE_EOF
	// Expects an OK (instead of EOF) after the resultset rows of a Text Resultset.
	CapabilityClientDeprecateEOF = 1 << 24
)

Capability flags. Originally found in include/mysql/mysql_com.h

View Source
const (
	// a transaction is active
	ServerStatusInTrans   uint16 = 0x0001
	NoServerStatusInTrans uint16 = 0xFFFE

	// auto-commit is enabled
	ServerStatusAutocommit   uint16 = 0x0002
	NoServerStatusAutocommit uint16 = 0xFFFD

	ServerMoreResultsExists     uint16 = 0x0008
	ServerStatusNoGoodIndexUsed uint16 = 0x0010
	ServerStatusNoIndexUsed     uint16 = 0x0020
	// Used by Binary Protocol Resultset to signal that COM_STMT_FETCH must be used to fetch the row-data.
	ServerStatusCursorExists       uint16 = 0x0040
	ServerStatusLastRowSent        uint16 = 0x0080
	ServerStatusDbDropped          uint16 = 0x0100
	ServerStatusNoBackslashEscapes uint16 = 0x0200
	ServerStatusMetadataChanged    uint16 = 0x0400
	ServerQueryWasSlow             uint16 = 0x0800
	ServerPsOutParams              uint16 = 0x1000
	// in a read-only transaction
	ServerStatusInTransReadonly uint16 = 0x2000
	// connection state information has changed
	ServerSessionStateChanged uint16 = 0x4000
)

Status flags. They are returned by the server in a few cases. Originally found in include/mysql/mysql_com.h See http://dev.mysql.com/doc/internals/en/status-flags.html

View Source
const (
	// one or more system variables changed.
	SessionTrackSystemVariables uint8 = 0x00
	// schema changed.
	SessionTrackSchema uint8 = 0x01
	// "track state change" changed.
	SessionTrackStateChange uint8 = 0x02
	// "track GTIDs" changed.
	SessionTrackGtids uint8 = 0x03
)

State Change Information

View Source
const (
	// ComQuit is COM_QUIT.
	ComQuit = 0x01

	// ComInitDB is COM_INIT_DB.
	ComInitDB = 0x02

	// ComQuery is COM_QUERY.
	ComQuery = 0x03

	// ComFieldList is COM_Field_List.
	ComFieldList = 0x04

	// ComPing is COM_PING.
	ComPing = 0x0e

	// ComBinlogDump is COM_BINLOG_DUMP.
	ComBinlogDump = 0x12

	// ComSemiSyncAck is SEMI_SYNC_ACK.
	ComSemiSyncAck = 0xef

	// ComPrepare is COM_PREPARE.
	ComPrepare = 0x16

	// ComStmtExecute is COM_STMT_EXECUTE.
	ComStmtExecute = 0x17

	// ComStmtSendLongData is COM_STMT_SEND_LONG_DATA
	ComStmtSendLongData = 0x18

	// ComStmtClose is COM_STMT_CLOSE.
	ComStmtClose = 0x19

	// ComStmtReset is COM_STMT_RESET
	ComStmtReset = 0x1a

	//ComStmtFetch is COM_STMT_FETCH
	ComStmtFetch = 0x1c

	// ComSetOption is COM_SET_OPTION
	ComSetOption = 0x1b

	// ComResetConnection is COM_RESET_CONNECTION
	ComResetConnection = 0x1f

	// ComBinlogDumpGTID is COM_BINLOG_DUMP_GTID.
	ComBinlogDumpGTID = 0x1e

	// ComRegisterReplica is COM_REGISTER_SLAVE
	// https://dev.mysql.com/doc/internals/en/com-register-slave.html
	ComRegisterReplica = 0x15

	// OKPacket is the header of the OK packet.
	OKPacket = 0x00

	// EOFPacket is the header of the EOF packet.
	EOFPacket = 0xfe

	// ErrPacket is the header of the error packet.
	ErrPacket = 0xff

	// NullValue is the encoded value of NULL.
	NullValue = 0xfb
)

Packet types. Originally found in include/mysql/mysql_com.h

View Source
const (
	// AuthMoreDataPacket is sent when server requires more data to authenticate
	AuthMoreDataPacket = 0x01

	// CachingSha2FastAuth is sent before OKPacket when server authenticates using cache
	CachingSha2FastAuth = 0x03

	// CachingSha2FullAuth is sent when server requests un-scrambled password to authenticate
	CachingSha2FullAuth = 0x04

	// AuthSwitchRequestPacket is used to switch auth method.
	AuthSwitchRequestPacket = 0xfe
)

Auth packet types

View Source
const (
	// IntVarInvalidInt is INVALID_INT_EVENT
	IntVarInvalidInt = 0

	// IntVarLastInsertID is LAST_INSERT_ID_EVENT
	IntVarLastInsertID = 1

	// IntVarInsertID is INSERT_ID_EVENT
	IntVarInsertID = 2
)

Constants for the type of an INTVAR_EVENT.

View Source
const (
	// BinlogChecksumAlgOff indicates that checksums are supported but off.
	BinlogChecksumAlgOff = 0

	// BinlogChecksumAlgCRC32 indicates that CRC32 checksums are used.
	BinlogChecksumAlgCRC32 = 1

	// BinlogChecksumAlgUndef indicates that checksums are not supported.
	BinlogChecksumAlgUndef = 255
)

Constants about the type of checksum in a packet. These constants are common between MariaDB 10.0 and MySQL 5.6.

View Source
const (
	// QFlags2Code is Q_FLAGS2_CODE
	QFlags2Code = 0

	// QSQLModeCode is Q_SQL_MODE_CODE
	QSQLModeCode = 1

	// QCatalog is Q_CATALOG
	QCatalog = 2

	// QAutoIncrement is Q_AUTO_INCREMENT
	QAutoIncrement = 3

	// QCharsetCode is Q_CHARSET_CODE
	QCharsetCode = 4

	// QTimeZoneCode is Q_TIME_ZONE_CODE
	QTimeZoneCode = 5

	// QCatalogNZCode is Q_CATALOG_NZ_CODE
	QCatalogNZCode = 6
)

These constants describe the type of status variables in q Query packet.

View Source
const (
	// BaseShowPrimary is the base query for fetching primary key info.
	BaseShowPrimary = `` /* 203-byte string literal not displayed */

	// ShowRowsRead is the query used to find the number of rows read.
	ShowRowsRead = "show status like 'Innodb_rows_read'"

	// GetColumnNamesQueryPatternForTable is used for mocking queries in unit tests
	GetColumnNamesQueryPatternForTable = `SELECT COLUMN_NAME.*TABLE_NAME.*%s.*`
)
View Source
const BaseShowTables = `` /* 159-byte string literal not displayed */

CapabilityFlags are client capability flag sent to mysql on connect

View Source
const CapabilityFlagsSsl = CapabilityFlags |
	CapabilityClientSSL

CapabilityFlagsSsl signals that we can handle SSL as well

View Source
const (
	DefaultFlushDelay = 100 * time.Millisecond
)
View Source
const (
	// The directory used for redo logs within innodb_log_group_home_dir
	// in MySQL 8.0.30 and later.
	// You would check to see if this is relevant using the Flavor's
	// capability interface to check for DynamicRedoLogCapacityFlavorCapability.
	DynamicRedoLogSubdir = "#innodb_redo"
)
View Source
const (
	// Use as `maxrows` in `ExecuteFetch` and related functions, to indicate no rows should be fetched.
	// This is different than specifying `0`, because `0` means "expect zero results", while this means
	// "do not attempt to read any results into memory".
	FETCH_NO_ROWS = math.MinInt
)
View Source
const (
	FlagLogEventArtificial = 0x20
)
View Source
const GRFlavorID = "MysqlGR"

GRFlavorID is the string identifier for the MysqlGR flavor.

View Source
const TablesWithSize56 = `` /* 293-byte string literal not displayed */

TablesWithSize56 is a query to select table along with size for mysql 5.6

View Source
const TablesWithSize57 = `` /* 679-byte string literal not displayed */

TablesWithSize57 is a query to select table along with size for mysql 5.7.

It's a little weird, because the JOIN predicate only works if the table and databases do not contain weird characters. If the join does not return any data, we fall back to the same fields as used in the mysql 5.6 query.

We join with a subquery that materializes the data from `information_schema.innodb_sys_tablespaces` early for performance reasons. This effectively causes only a single read of `information_schema.innodb_sys_tablespaces` per query.

View Source
const TablesWithSize80 = `` /* 1146-byte string literal not displayed */

TablesWithSize80 is a query to select table along with size for mysql 8.0 Note the following:

  • `TABLES`.`TABLE_NAME` has `utf8mb4_0900_ai_ci` collation. `INNODB_TABLESPACES`.`NAME` has `utf8mb3_general_ci`. We normalize the collation to get better query performance (we force the casting at the time of our choosing)
  • InnoDB has different table names than MySQL does, in particular for partitioned tables. As far as InnoDB is concerned, each partition is its own table.
  • We use a `UNION ALL` approach to handle two distinct scenarios: tables that are partitioned and those that are not. Since we `LEFT JOIN` from `TABLES` to `INNODB_TABLESPACES`, we know we already do full table scan on `TABLES`. We therefore don't mind spending some extra computation time (as in `CONCAT(t.table_schema, '/', t.table_name, '#p#%') COLLATE utf8mb3_general_ci`) to make things easier for the JOIN.
  • We utilize `INFORMATION_SCHEMA`.`TABLES`.`CREATE_OPTIONS` column to tell if the table is partitioned or not. The column may be `NULL` or may have multiple attributes, one of which is "partitioned", which we are looking for.
  • In a partitioned table, InnoDB will return multiple rows for the same table name, one for each partition, which we successively SUM. We also `SUM` the sizes in the non-partitioned case. This is not because we need to, but because it makes the query symmetric and less prone to future edit errors.

Variables

View Source
var (
	TransactionPayloadCompressionTypes = map[uint64]string{
		TransactionPayloadCompressionZstd: "ZSTD",
		TransactionPayloadCompressionNone: "NONE",
	}

	// At what size should we switch from the in-memory buffer
	// decoding to streaming mode which is much slower, but does
	// not require everything be done in memory all at once.
	ZstdInMemoryDecompressorMaxSize = uint64(128 << (10 * 2)) // 128MiB

)
View Source
var (
	// ErrNotReplica means there is no replication status.
	// Returned by ShowReplicationStatus().
	ErrNotReplica = sqlerror.NewSQLError(sqlerror.ERNotReplica, sqlerror.SSUnknownSQLState, "no replication status")

	// ErrNoPrimaryStatus means no status was returned by ShowPrimaryStatus().
	ErrNoPrimaryStatus = errors.New("no master status")
)
View Source
var BaseShowTablesFields = []*querypb.Field{{
	Name:         "t.table_name",
	Type:         querypb.Type_VARCHAR,
	Table:        "tables",
	OrgTable:     "TABLES",
	Database:     "information_schema",
	OrgName:      "TABLE_NAME",
	ColumnLength: 192,
	Charset:      uint32(collations.SystemCollation.Collation),
	Flags:        uint32(querypb.MySqlFlag_NOT_NULL_FLAG),
}, {
	Name:         "t.table_type",
	Type:         querypb.Type_VARCHAR,
	Table:        "tables",
	OrgTable:     "TABLES",
	Database:     "information_schema",
	OrgName:      "TABLE_TYPE",
	ColumnLength: 192,
	Charset:      uint32(collations.SystemCollation.Collation),
	Flags:        uint32(querypb.MySqlFlag_NOT_NULL_FLAG),
}, {
	Name:         "unix_timestamp(t.create_time)",
	Type:         querypb.Type_INT64,
	ColumnLength: 11,
	Charset:      collations.CollationBinaryID,
	Flags:        uint32(querypb.MySqlFlag_BINARY_FLAG | querypb.MySqlFlag_NUM_FLAG),
}, {
	Name:         "t.table_comment",
	Type:         querypb.Type_VARCHAR,
	Table:        "tables",
	OrgTable:     "TABLES",
	Database:     "information_schema",
	OrgName:      "TABLE_COMMENT",
	ColumnLength: 6144,
	Charset:      uint32(collations.SystemCollation.Collation),
	Flags:        uint32(querypb.MySqlFlag_NOT_NULL_FLAG),
}}

BaseShowTablesFields contains the fields returned by a BaseShowTables or a BaseShowTablesForTable command. They are validated by the testBaseShowTables test.

View Source
var BaseShowTablesWithSizesFields = append(BaseShowTablesFields, &querypb.Field{
	Name:         "i.file_size",
	Type:         querypb.Type_INT64,
	ColumnLength: 11,
	Charset:      collations.CollationBinaryID,
	Flags:        uint32(querypb.MySqlFlag_BINARY_FLAG | querypb.MySqlFlag_NUM_FLAG),
}, &querypb.Field{
	Name:         "i.allocated_size",
	Type:         querypb.Type_INT64,
	ColumnLength: 11,
	Charset:      collations.CollationBinaryID,
	Flags:        uint32(querypb.MySqlFlag_BINARY_FLAG | querypb.MySqlFlag_NUM_FLAG),
})
View Source
var (
	// BinglogMagicNumber is 4-byte number at the beginning of every binary log
	BinglogMagicNumber = []byte{0xfe, 0x62, 0x69, 0x6e}
)
View Source
var (
	ErrExecuteFetchMultipleResults = vterrors.Errorf(vtrpc.Code_INTERNAL, "unexpected multiple results. Use ExecuteFetchMulti instead.")
)
View Source
var ErrNoGroupStatus = errors.New("no group status")

ErrNoGroupStatus means no status for group replication.

View Source
var (
	// IntVarNames maps a InVar type to the variable name it represents.
	IntVarNames = map[byte]string{
		IntVarLastInsertID: "LAST_INSERT_ID",
		IntVarInsertID:     "INSERT_ID",
	}
)

Name of the variable represented by an IntVar.

View Source
var ShowPrimaryFields = []*querypb.Field{{
	Name: "table_name",
	Type: sqltypes.VarChar,
}, {
	Name: "column_name",
	Type: sqltypes.VarChar,
}}

ShowPrimaryFields contains the fields for a BaseShowPrimary.

Functions

func BaseShowTablesRow

func BaseShowTablesRow(tableName string, isView bool, comment string) []sqltypes.Value

BaseShowTablesRow returns the fields from a BaseShowTables or BaseShowTablesForTable command.

func BaseShowTablesWithSizesRow added in v0.18.6

func BaseShowTablesWithSizesRow(tableName string, isView bool, comment string) []sqltypes.Value

func DecodeMysqlNativePasswordHex added in v0.12.0

func DecodeMysqlNativePasswordHex(hexEncodedPassword string) ([]byte, error)

DecodeMysqlNativePasswordHex decodes the standard format used by MySQL for 4.1 style password hashes. It drops the optionally leading * before decoding the rest as a hex encoded string.

func EncryptPasswordWithPublicKey added in v0.10.0

func EncryptPasswordWithPublicKey(salt []byte, password []byte, pub *rsa.PublicKey) ([]byte, error)

EncryptPasswordWithPublicKey obfuscates the password and encrypts it with server's public key as required by caching_sha2_password plugin for "full" authentication

func ExecuteFetchMap

func ExecuteFetchMap(conn *Conn, query string) (map[string]string, error)

ExecuteFetchMap returns a map from column names to cell data for a query that should return exactly 1 row.

func FlagsForColumn added in v0.17.2

func FlagsForColumn(t sqltypes.Type, col collations.ID) uint32

func GetCharset

func GetCharset(conn *Conn) (*binlogdatapb.Charset, error)

GetCharset returns the current numerical values of the per-session character set variables.

func GetFlavor added in v0.14.0

func GetFlavor(serverVersion string, flavorFunc func(serverVersion string) flavor) (f flavor, capableOf capabilities.CapableOf, canonicalVersion string)

GetFlavor fills in c.Flavor. If the params specify the flavor, that is used. Otherwise, we auto-detect.

This is the same logic as the ConnectorJ java client. We try to recognize MariaDB as much as we can, but default to MySQL.

MariaDB note: the server version returned here might look like: 5.5.5-10.0.21-MariaDB-... If that is the case, we are removing the 5.5.5- prefix. Note on such servers, 'select version()' would return 10.0.21-MariaDB-... as well (not matching what c.ServerVersion is, but matching after we remove the prefix).

func InitAuthServerClientCert

func InitAuthServerClientCert(clientcertAuthMethod string)

InitAuthServerClientCert is public so it can be called from plugin_auth_clientcert.go (go/cmd/vtgate)

func InitAuthServerStatic

func InitAuthServerStatic(mysqlAuthServerStaticFile, mysqlAuthServerStaticString string, mysqlAuthServerStaticReloadInterval time.Duration)

InitAuthServerStatic Handles initializing the AuthServerStatic if necessary.

func IsNum

func IsNum(typ uint8) bool

IsNum returns true if a MySQL type is a numeric value. It is the same as IS_NUM defined in mysql.h.

func MatchSourceHost added in v0.12.0

func MatchSourceHost(remoteAddr net.Addr, targetSourceHost string) bool

MatchSourceHost validates host entry in auth configuration

func ParseConfig added in v0.12.0

func ParseConfig(jsonBytes []byte, config *map[string][]*AuthServerStaticEntry) error

ParseConfig takes a JSON MySQL static config and converts to a validated map

func ParseErrorPacket

func ParseErrorPacket(data []byte) error

ParseErrorPacket parses the error packet and returns a SQLError.

func RegisterAuthServer added in v0.12.0

func RegisterAuthServer(name string, authServer AuthServer)

RegisterAuthServer registers an implementations of AuthServer.

func RegisterAuthServerStaticFromParams

func RegisterAuthServerStaticFromParams(file, jsonConfig string, reloadInterval time.Duration)

RegisterAuthServerStaticFromParams creates and registers a new AuthServerStatic, loaded for a JSON file or string. If file is set, it uses file. Otherwise, load the string. It log.Exits out in case of error.

func ScrambleCachingSha2Password added in v0.10.0

func ScrambleCachingSha2Password(salt []byte, password []byte) []byte

ScrambleCachingSha2Password computes the hash of the password using SHA256 as required by caching_sha2_password plugin for "fast" authentication

func ScrambleMysqlNativePassword added in v0.10.0

func ScrambleMysqlNativePassword(salt, password []byte) []byte

ScrambleMysqlNativePassword computes the hash of the password using 4.1+ method.

This can be used for example inside a `mysql_native_password` plugin implementation if the backend storage implements storage of plain text passwords.

func ServerVersionCapableOf added in v0.19.0

func ServerVersionCapableOf(serverVersion string) (capableOf capabilities.CapableOf)

ServerVersionCapableOf is a convenience function that returns a CapableOf function given a server version. It is a shortcut for GetFlavor(serverVersion, nil).

func SetCharset

func SetCharset(conn *Conn, cs *binlogdatapb.Charset) error

SetCharset changes the per-session character set variables.

func ShowPrimaryRow

func ShowPrimaryRow(tableName, colName string) []sqltypes.Value

ShowPrimaryRow returns a row for a primary key column.

func VerifyHashedCachingSha2Password added in v0.12.0

func VerifyHashedCachingSha2Password(reply, salt, hashedCachingSha2Password []byte) bool

VerifyHashedCachingSha2Password verifies a client reply against a stored hash.

This can be used for example inside a `caching_sha2_password` plugin implementation if the cache storage uses password keys with SHA256(SHA256(password)).

All values here are non encoded byte slices, so if you store for example the double SHA256 of the password as hex encoded characters, you need to decode that first.

func VerifyHashedMysqlNativePassword added in v0.12.0

func VerifyHashedMysqlNativePassword(reply, salt, hashedNativePassword []byte) bool

VerifyHashedMysqlNativePassword verifies a client reply against a stored hash.

This can be used for example inside a `mysql_native_password` plugin implementation if the backend storage where the stored password is a SHA1(SHA1(password)).

All values here are non encoded byte slices, so if you store for example the double SHA1 of the password as hex encoded characters, you need to decode that first. See DecodeMysqlNativePasswordHex for a decoding helper for the standard encoding format of this hash used by MySQL.

Types

type AuthMethod added in v0.12.0

type AuthMethod interface {
	// Name returns the auth method description for this implementation.
	// This is the name that is sent as the auth plugin name during the
	// Mysql authentication protocol handshake.
	Name() AuthMethodDescription

	// HandleUser verifies if the current auth method can authenticate
	// the given user with the current auth method. This can be useful
	// for example if you only have a plain text of hashed password
	// for specific users and not all users and auth method support
	// depends on what you have.
	HandleUser(conn *Conn, user string) bool

	// AllowClearTextWithoutTLS identifies if an auth method is allowed
	// on a plain text connection. This check is only enforced
	// if the listener has AllowClearTextWithoutTLS() disabled.
	AllowClearTextWithoutTLS() bool

	// AuthPluginData generates the information for the auth plugin.
	// This is included in for example the auth switch request. This
	// is auth plugin specific and opaque to the Mysql handshake
	// protocol.
	AuthPluginData() ([]byte, error)

	// HandleAuthPluginData handles the returned auth plugin data from
	// the client. The original data the server sent is also included
	// which can include things like the salt for `mysql_native_password`.
	//
	// The remote address is provided for plugins that also want to
	// do additional checks like IP based restrictions.
	HandleAuthPluginData(conn *Conn, user string, serverAuthPluginData []byte, clientAuthPluginData []byte, remoteAddr net.Addr) (Getter, error)
}

AuthMethod interface for concrete auth method implementations. When building an auth server, you usually don't implement these yourself but the helper methods to build AuthMethod instances should be used.

func NewMysqlClearAuthMethod added in v0.12.0

func NewMysqlClearAuthMethod(layer PlainTextStorage, validator UserValidator) AuthMethod

NewMysqlClearAuthMethod will create a new AuthMethod that implements the `mysql_clear_password` handshake. The caller will need to provide a storage object for plain text passwords and validator that will be called during the handshake phase.

func NewMysqlDialogAuthMethod added in v0.12.0

func NewMysqlDialogAuthMethod(layer PlainTextStorage, validator UserValidator, msg string) AuthMethod

NewMysqlDialogAuthMethod will create a new AuthMethod that implements the `dialog` handshake. The caller will need to provide a storage object for plain text passwords and validator that will be called during the handshake phase. The message given will be sent as part of the dialog. If the empty string is provided, the default message of "Enter password: " will be used.

func NewMysqlNativeAuthMethod added in v0.12.0

func NewMysqlNativeAuthMethod(layer HashStorage, validator UserValidator) AuthMethod

NewMysqlNativeAuthMethod will create a new AuthMethod that implements the `mysql_native_password` handshake. The caller will need to provide a storage object and validator that will be called during the handshake phase.

func NewSha2CachingAuthMethod added in v0.12.0

func NewSha2CachingAuthMethod(layer1 CachingStorage, layer2 PlainTextStorage, validator UserValidator) AuthMethod

NewSha2CachingAuthMethod will create a new AuthMethod that implements the `caching_sha2_password` handshake. The caller will need to provide a cache object for the fast auth path and a plain text storage object that will be called if the return of the first layer indicates the full auth dance is needed.

Right now we only support caching_sha2_password over TLS or a Unix socket.

If TLS is not enabled, the client needs to encrypt it with the public key of the server. In that case, Vitess is already configured with a certificate anyway, so we recommend to use TLS if you want to use caching_sha2_password in that case instead of allowing the plain text fallback path here.

This might change in the future if there's a good argument and implementation for allowing the plain text path here as well.

type AuthMethodDescription added in v0.12.0

type AuthMethodDescription string

AuthMethodDescription is the type for different supported and implemented authentication methods.

type AuthServer

type AuthServer interface {
	// AuthMethods returns a list of auth methods that are part of this
	// interface. Building an authentication server usually means
	// creating AuthMethod instances with the known helpers for the
	// currently supported AuthMethod implementations.
	//
	// When a client connects, the server checks the list of auth methods
	// available. If an auth method for the requested auth mechanism by the
	// client is available, it will be used immediately.
	//
	// If there is no overlap between the provided auth methods and
	// the one the client requests, the server will send back an
	// auth switch request using the first provided AuthMethod in this list.
	AuthMethods() []AuthMethod

	// DefaultAuthMethodDescription returns the auth method that the auth server
	// sends during the initial server handshake. This needs to be either
	// `mysql_native_password` or `caching_sha2_password` as those are the only
	// supported auth methods during the initial handshake.
	//
	// It's not needed to also support those methods in the AuthMethods(),
	// in fact, if you want to only support for example clear text passwords,
	// you must still return `mysql_native_password` or `caching_sha2_password`
	// here and the auth switch protocol will be used to switch to clear text.
	DefaultAuthMethodDescription() AuthMethodDescription
}

AuthServer is the interface that servers must implement to validate users and passwords. It needs to be able to return a list of AuthMethod interfaces which implement the supported auth methods for the server.

func GetAuthServer

func GetAuthServer(name string) AuthServer

GetAuthServer returns an AuthServer by name, or log.Exitf.

type AuthServerClientCert

type AuthServerClientCert struct {
	Method AuthMethodDescription
	// contains filtered or unexported fields
}

AuthServerClientCert implements AuthServer which enforces client side certificates

func (*AuthServerClientCert) AuthMethods added in v0.12.0

func (asl *AuthServerClientCert) AuthMethods() []AuthMethod

AuthMethods returns the implement auth methods for the client certificate authentication setup.

func (*AuthServerClientCert) DefaultAuthMethodDescription added in v0.12.0

func (asl *AuthServerClientCert) DefaultAuthMethodDescription() AuthMethodDescription

DefaultAuthMethodDescription returns always MysqlNativePassword for the client certificate authentication setup.

func (*AuthServerClientCert) HandleUser added in v0.12.0

func (asl *AuthServerClientCert) HandleUser(user string) bool

HandleUser is part of the UserValidator interface. We handle any user here since we don't check up front.

func (*AuthServerClientCert) UserEntryWithPassword added in v0.12.0

func (asl *AuthServerClientCert) UserEntryWithPassword(conn *Conn, user string, password string, remoteAddr net.Addr) (Getter, error)

UserEntryWithPassword is part of the PlaintextStorage interface

type AuthServerNone

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

AuthServerNone takes all comers. It's meant to be used for testing and prototyping. With this config, you can connect to a local vtgate using the following command line: 'mysql -P port -h ::'. It only uses MysqlNativePassword method.

func NewAuthServerNone added in v0.12.0

func NewAuthServerNone() *AuthServerNone

NewAuthServerNone returns an empty auth server. Always accepts all clients.

func (*AuthServerNone) AuthMethods added in v0.12.0

func (a *AuthServerNone) AuthMethods() []AuthMethod

AuthMethods returns the list of registered auth methods implemented by this auth server.

func (*AuthServerNone) DefaultAuthMethodDescription added in v0.12.0

func (a *AuthServerNone) DefaultAuthMethodDescription() AuthMethodDescription

DefaultAuthMethodDescription returns MysqlNativePassword as the default authentication method for the auth server implementation.

func (*AuthServerNone) HandleUser added in v0.12.0

func (a *AuthServerNone) HandleUser(user string) bool

HandleUser validates if this user can use this auth method

func (*AuthServerNone) UserEntryWithHash added in v0.12.0

func (a *AuthServerNone) UserEntryWithHash(conn *Conn, salt []byte, user string, authResponse []byte, remoteAddr net.Addr) (Getter, error)

UserEntryWithHash validates the user if it exists and returns the information. Always accepts any user.

type AuthServerStatic

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

AuthServerStatic implements AuthServer using a static configuration.

func NewAuthServerStatic

func NewAuthServerStatic(file, jsonConfig string, reloadInterval time.Duration) *AuthServerStatic

NewAuthServerStatic returns a new empty AuthServerStatic.

func NewAuthServerStaticWithAuthMethodDescription added in v0.12.0

func NewAuthServerStaticWithAuthMethodDescription(file, jsonConfig string, reloadInterval time.Duration, authMethodDescription AuthMethodDescription) *AuthServerStatic

NewAuthServerStaticWithAuthMethodDescription returns a new empty AuthServerStatic but with support for a different auth method. Mostly used for testing purposes.

func (*AuthServerStatic) AuthMethods added in v0.12.0

func (a *AuthServerStatic) AuthMethods() []AuthMethod

AuthMethods returns the AuthMethod instances this auth server can handle.

func (*AuthServerStatic) DefaultAuthMethodDescription added in v0.12.0

func (a *AuthServerStatic) DefaultAuthMethodDescription() AuthMethodDescription

DefaultAuthMethodDescription returns the default auth method in the handshake which is MysqlNativePassword for this auth server.

func (*AuthServerStatic) HandleUser added in v0.12.0

func (a *AuthServerStatic) HandleUser(user string) bool

HandleUser is part of the Validator interface. We handle any user here since we don't check up front.

func (*AuthServerStatic) UserEntryWithCacheHash added in v0.12.0

func (a *AuthServerStatic) UserEntryWithCacheHash(conn *Conn, salt []byte, user string, authResponse []byte, remoteAddr net.Addr) (Getter, CacheState, error)

UserEntryWithCacheHash implements password lookup based on a caching_sha2_password hash that is negotiated with the client.

func (*AuthServerStatic) UserEntryWithHash added in v0.12.0

func (a *AuthServerStatic) UserEntryWithHash(conn *Conn, salt []byte, user string, authResponse []byte, remoteAddr net.Addr) (Getter, error)

UserEntryWithHash implements password lookup based on a mysql_native_password hash that is negotiated with the client.

func (*AuthServerStatic) UserEntryWithPassword added in v0.12.0

func (a *AuthServerStatic) UserEntryWithPassword(conn *Conn, user string, password string, remoteAddr net.Addr) (Getter, error)

UserEntryWithPassword implements password lookup based on a plain text password that is negotiated with the client.

type AuthServerStaticEntry

type AuthServerStaticEntry struct {
	// MysqlNativePassword is generated by password hashing methods in MySQL.
	// These changes are illustrated by changes in the result from the PASSWORD() function
	// that computes password hash values and in the structure of the user table where passwords are stored.
	// mysql> SELECT PASSWORD('mypass');
	// +-------------------------------------------+
	// | PASSWORD('mypass')                        |
	// +-------------------------------------------+
	// | *6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4 |
	// +-------------------------------------------+
	// MysqlNativePassword's format looks like "*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4", it store a hashing value.
	// Use MysqlNativePassword in auth config, maybe more secure. After all, it is cryptographic storage.
	MysqlNativePassword string
	Password            string
	UserData            string
	SourceHost          string
	Groups              []string
}

AuthServerStaticEntry stores the values for a given user.

type BinlogEvent

type BinlogEvent interface {
	// IsValid returns true if the underlying data buffer contains a valid
	// event. This should be called first on any BinlogEvent, and other
	// methods should only be called if this one returns true. This ensures
	// you won't get panics due to bounds checking on the byte array.
	IsValid() bool

	// General protocol events.
	NextPosition() uint32

	// IsFormatDescription returns true if this is a
	// FORMAT_DESCRIPTION_EVENT. Do not call StripChecksum before
	// calling Format (Format returns the BinlogFormat anyway,
	// required for calling StripChecksum).
	IsFormatDescription() bool
	// IsQuery returns true if this is a QUERY_EVENT, which encompasses
	// all SQL statements.
	IsQuery() bool
	// IsXID returns true if this is an XID_EVENT, which is an alternate
	// form of COMMIT.
	IsXID() bool
	// IsStop returns true if this is a STOP_EVENT.
	IsStop() bool
	// IsGTID returns true if this is a GTID_EVENT.
	IsGTID() bool
	// IsRotate returns true if this is a ROTATE_EVENT.
	IsRotate() bool
	// IsIntVar returns true if this is an INTVAR_EVENT.
	IsIntVar() bool
	// IsRand returns true if this is a RAND_EVENT.
	IsRand() bool
	// IsPreviousGTIDs returns true if this event is a PREVIOUS_GTIDS_EVENT.
	IsPreviousGTIDs() bool
	// IsHeartbeat returns true if this event is a HEARTBEAT_EVENT.
	IsHeartbeat() bool
	// IsSemiSyncAckRequested returns true if the source requests a semi-sync ack for this event
	IsSemiSyncAckRequested() bool

	// IsTableMap returns true if this is a TABLE_MAP_EVENT.
	IsTableMap() bool
	// IsWriteRows returns true if this is a WRITE_ROWS_EVENT.
	IsWriteRows() bool
	// IsUpdateRows returns true if this is a UPDATE_ROWS_EVENT.
	IsUpdateRows() bool
	// IsDeleteRows returns true if this is a DELETE_ROWS_EVENT.
	IsDeleteRows() bool

	// Timestamp returns the timestamp from the event header.
	Timestamp() uint32
	// ServerID returns the server ID from the event header.
	ServerID() uint32

	// Format returns a BinlogFormat struct based on the event data.
	// This is only valid if IsFormatDescription() returns true.
	Format() (BinlogFormat, error)
	// GTID returns the GTID from the event, and if this event
	// also serves as a BEGIN statement.
	// This is only valid if IsGTID() returns true.
	GTID(BinlogFormat) (replication.GTID, bool, error)
	// Query returns a Query struct representing data from a QUERY_EVENT.
	// This is only valid if IsQuery() returns true.
	Query(BinlogFormat) (Query, error)
	// IntVar returns the type and value of the variable for an INTVAR_EVENT.
	// This is only valid if IsIntVar() returns true.
	IntVar(BinlogFormat) (byte, uint64, error)
	// Rand returns the two seed values for a RAND_EVENT.
	// This is only valid if IsRand() returns true.
	Rand(BinlogFormat) (uint64, uint64, error)
	// PreviousGTIDs returns the Position from the event.
	// This is only valid if IsPreviousGTIDs() returns true.
	PreviousGTIDs(BinlogFormat) (replication.Position, error)

	// TableID returns the table ID for a TableMap, UpdateRows,
	// WriteRows or DeleteRows event.
	TableID(BinlogFormat) uint64
	// TableMap returns a TableMap struct representing data from a
	// TABLE_MAP_EVENT.  This is only valid if IsTableMapEvent() returns
	// true.
	TableMap(BinlogFormat) (*TableMap, error)
	// Rows returns a Rows struct representing data from a
	// {WRITE,UPDATE,DELETE}_ROWS_EVENT.  This is only valid if
	// IsWriteRows(), IsUpdateRows(), or IsDeleteRows() returns
	// true.
	Rows(BinlogFormat, *TableMap) (Rows, error)
	// TransactionPayload returns a TransactionPayload type which provides
	// a GetNextEvent() method to iterate over the events contained within
	// the uncompressed payload. You must call Close() when you are done
	// with the TransactionPayload to ensure that the underlying resources
	// used are cleaned up.
	TransactionPayload(BinlogFormat) (*TransactionPayload, error)
	// NextLogFile returns the name of the next binary log file & pos.
	// This is only valid if IsRotate() returns true
	NextLogFile(BinlogFormat) (string, uint64, error)

	// StripChecksum returns the checksum and a modified event with the
	// checksum stripped off, if any. If there is no checksum, it returns
	// the same event and a nil checksum.
	StripChecksum(BinlogFormat) (ev BinlogEvent, checksum []byte, err error)

	// IsPseudo is for custom implementations of GTID.
	IsPseudo() bool

	// IsTransactionPayload returns true if a compressed transaction
	// payload event is found (binlog_transaction_compression=ON).
	IsTransactionPayload() bool

	// Bytes returns the binary representation of the event
	Bytes() []byte
}

BinlogEvent represents a single event from a raw MySQL binlog dump stream. The implementation is provided by each supported flavor in go/vt/mysqlctl.

binlog.Streamer receives these events through a mysqlctl.BinlogConnection and processes them, grouping statements into BinlogTransactions as appropriate.

Methods that only access header fields can't fail as long as IsValid() returns true, so they have a single return value. Methods that might fail even when IsValid() is true return an error value.

Methods that require information from the initial FORMAT_DESCRIPTION_EVENT will have a BinlogFormat parameter.

A BinlogEvent should never be sent over the wire. UpdateStream service will send BinlogTransactions from these events.

func NewDeleteRowsEvent

func NewDeleteRowsEvent(f BinlogFormat, s *FakeBinlogStream, tableID uint64, rows Rows) BinlogEvent

NewDeleteRowsEvent returns an DeleteRows event. Uses v2.

func NewFakeRotateEvent added in v0.16.0

func NewFakeRotateEvent(f BinlogFormat, s *FakeBinlogStream, filename string) BinlogEvent

func NewFormatDescriptionEvent

func NewFormatDescriptionEvent(f BinlogFormat, s *FakeBinlogStream) BinlogEvent

NewFormatDescriptionEvent creates a new FormatDescriptionEvent based on the provided BinlogFormat. It uses a mysql56BinlogEvent but could use a MariaDB one.

func NewHeartbeatEvent added in v0.16.0

func NewHeartbeatEvent(f BinlogFormat, s *FakeBinlogStream) BinlogEvent

NewHeartbeatEvent returns a HeartbeatEvent. see https://dev.mysql.com/doc/internals/en/heartbeat-event.html

func NewHeartbeatEventWithLogFile added in v0.16.0

func NewHeartbeatEventWithLogFile(f BinlogFormat, s *FakeBinlogStream, filename string) BinlogEvent

NewHeartbeatEvent returns a HeartbeatEvent. see https://dev.mysql.com/doc/internals/en/heartbeat-event.html

func NewIntVarEvent

func NewIntVarEvent(f BinlogFormat, s *FakeBinlogStream, typ byte, value uint64) BinlogEvent

NewIntVarEvent returns an IntVar event.

func NewInvalidEvent

func NewInvalidEvent() BinlogEvent

NewInvalidEvent returns an invalid event (its size is <19).

func NewInvalidFormatDescriptionEvent

func NewInvalidFormatDescriptionEvent(f BinlogFormat, s *FakeBinlogStream) BinlogEvent

NewInvalidFormatDescriptionEvent returns an invalid FormatDescriptionEvent. The binlog version is set to 3. It IsValid() though.

func NewInvalidQueryEvent

func NewInvalidQueryEvent(f BinlogFormat, s *FakeBinlogStream) BinlogEvent

NewInvalidQueryEvent returns an invalid QueryEvent. IsValid is however true. sqlPos is out of bounds.

func NewMariaDBGTIDEvent

func NewMariaDBGTIDEvent(f BinlogFormat, s *FakeBinlogStream, gtid replication.MariadbGTID, hasBegin bool) BinlogEvent

NewMariaDBGTIDEvent returns a MariaDB specific GTID event. It ignores the Server in the gtid, instead uses the FakeBinlogStream.ServerID.

func NewMariadbBinlogEvent

func NewMariadbBinlogEvent(buf []byte) BinlogEvent

NewMariadbBinlogEvent creates a BinlogEvent instance from given byte array

func NewMariadbBinlogEventWithSemiSyncInfo added in v0.14.0

func NewMariadbBinlogEventWithSemiSyncInfo(buf []byte, semiSyncAckRequested bool) BinlogEvent

NewMariadbBinlogEventWithSemiSyncInfo creates a BinlogEvent instance from given byte array

func NewMysql56BinlogEvent

func NewMysql56BinlogEvent(buf []byte) BinlogEvent

NewMysql56BinlogEvent creates a BinlogEvent from given byte array

func NewMysql56BinlogEventWithSemiSyncInfo added in v0.14.0

func NewMysql56BinlogEventWithSemiSyncInfo(buf []byte, semiSyncAckRequested bool) BinlogEvent

NewMysql56BinlogEventWithSemiSyncInfo creates a BinlogEvent from given byte array

func NewQueryEvent

func NewQueryEvent(f BinlogFormat, s *FakeBinlogStream, q Query) BinlogEvent

NewQueryEvent makes up a QueryEvent based on the Query structure.

func NewRotateEvent

func NewRotateEvent(f BinlogFormat, s *FakeBinlogStream, position uint64, filename string) BinlogEvent

NewRotateEvent returns a RotateEvent. The timestamp of such an event should be zero, so we patch it in.

func NewTableMapEvent

func NewTableMapEvent(f BinlogFormat, s *FakeBinlogStream, tableID uint64, tm *TableMap) BinlogEvent

NewTableMapEvent returns a TableMap event. Only works with post_header_length=8.

func NewUpdateRowsEvent

func NewUpdateRowsEvent(f BinlogFormat, s *FakeBinlogStream, tableID uint64, rows Rows) BinlogEvent

NewUpdateRowsEvent returns an UpdateRows event. Uses v2.

func NewWriteRowsEvent

func NewWriteRowsEvent(f BinlogFormat, s *FakeBinlogStream, tableID uint64, rows Rows) BinlogEvent

NewWriteRowsEvent returns a WriteRows event. Uses v2.

func NewXIDEvent

func NewXIDEvent(f BinlogFormat, s *FakeBinlogStream) BinlogEvent

NewXIDEvent returns a XID event. We do not use the data, so keep it 0.

type BinlogFormat

type BinlogFormat struct {
	// HeaderSizes is an array of sizes of the headers for each message.
	HeaderSizes []byte

	// ServerVersion is the name of the MySQL server version.
	// It starts with something like 8.0.34-xxxx.
	ServerVersion string

	// FormatVersion is the version number of the binlog file format.
	// We only support version 4.
	FormatVersion uint16

	// HeaderLength is the size in bytes of event headers other
	// than FORMAT_DESCRIPTION_EVENT. Almost always 19.
	HeaderLength byte

	// ChecksumAlgorithm is the ID number of the binlog checksum algorithm.
	// See three possible values below.
	ChecksumAlgorithm byte
}

BinlogFormat contains relevant data from the FORMAT_DESCRIPTION_EVENT. This structure is passed to subsequent event types to let them know how to parse themselves.

func NewMariaDBBinlogFormat

func NewMariaDBBinlogFormat() BinlogFormat

NewMariaDBBinlogFormat returns a typical BinlogFormat for MariaDB 10.0.

func NewMySQL56BinlogFormat

func NewMySQL56BinlogFormat() BinlogFormat

NewMySQL56BinlogFormat returns a typical BinlogFormat for MySQL 5.6.

func (BinlogFormat) HeaderSize

func (f BinlogFormat) HeaderSize(typ byte) byte

HeaderSize returns the header size of any event type.

func (BinlogFormat) IsZero

func (f BinlogFormat) IsZero() bool

IsZero returns true if the BinlogFormat has not been initialized.

type Bitmap

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

Bitmap is used by the previous structures.

func NewServerBitmap

func NewServerBitmap(count int) Bitmap

NewServerBitmap returns a bitmap that can hold 'count' bits.

func (*Bitmap) Bit

func (b *Bitmap) Bit(index int) bool

Bit returned the value of a given bit in the Bitmap.

func (*Bitmap) BitCount

func (b *Bitmap) BitCount() int

BitCount returns how many bits are set in the bitmap. Note values that are not used may be set to 0 or 1, hence the non-efficient logic.

func (*Bitmap) Bits added in v0.17.0

func (b *Bitmap) Bits() []byte

Bits returns the underlying bitmap.

func (*Bitmap) Count

func (b *Bitmap) Count() int

Count returns the number of bits in this Bitmap.

func (*Bitmap) Set

func (b *Bitmap) Set(index int, value bool)

Set sets the given boolean value.

type CacheState added in v0.12.0

type CacheState int

CacheState is a state that is returned by the UserEntryWithCacheHash method from the CachingStorage interface. This state is needed to indicate whether the authentication is accepted, rejected by the cache itself or if the cache can't fullfill the request. In that case it indicates that with AuthNeedMoreData.

const (
	// AuthRejected is used when the cache knows the request can be rejected.
	AuthRejected CacheState = iota
	// AuthAccepted is used when the cache knows the request can be accepted.
	AuthAccepted
	// AuthNeedMoreData is used when the cache doesn't know the answer and more data is needed.
	AuthNeedMoreData
)

type CachingStorage added in v0.12.0

type CachingStorage interface {
	UserEntryWithCacheHash(conn *Conn, salt []byte, user string, authResponse []byte, remoteAddr net.Addr) (Getter, CacheState, error)
}

CachingStorage describes an object that is suitable to retrieve user information based on a hashed value of the password. This applies to the `caching_sha2_password` authentication method.

The cache would hash the password internally as `SHA256(SHA256(password))`.

The VerifyHashedCachingSha2Password helper method can be used to verify such a hash based on the salt and auth response provided here after retrieving the hashed password from the cache.

type Conn

type Conn struct {

	// ClientData is a place where an application can store any
	// connection-related data. Mostly used on the server side, to
	// avoid maps indexed by ConnectionID for instance.
	ClientData any

	// ServerVersion is set during Connect with the server
	// version.  It is not changed afterwards. It is unused for
	// server-side connections.
	ServerVersion string

	// User is the name used by the client to connect.
	// It is set during the initial handshake.
	User string // For server-side connections, listener points to the server object.

	// UserData is custom data returned by the AuthServer module.
	// It is set during the initial handshake.
	UserData Getter

	// PrepareData is the map to use a prepared statement.
	PrepareData map[uint32]*PrepareData

	// Capabilities is the current set of features this connection
	// is using.  It is the features that are both supported by
	// the client and the server, and currently in use.
	// It is set during the initial handshake.
	//
	// It is only used for CapabilityClientDeprecateEOF
	// and CapabilityClientFoundRows.
	Capabilities uint32

	// ConnectionID is set:
	// - at Connect() time for clients, with the value returned by
	// the server.
	// - at accept time for the server.
	ConnectionID uint32

	// StatementID is the prepared statement ID.
	StatementID uint32

	// StatusFlags are the status flags we will base our returned flags on.
	// This is a bit field, with values documented in constants.go.
	// An interesting value here would be ServerStatusAutocommit.
	// It is only used by the server. These flags can be changed
	// by Handler methods.
	StatusFlags uint16

	// CharacterSet is the charset for this connection, as negotiated
	// in our handshake with the server. Note that although the MySQL protocol lists this
	// as a "character set", the returned byte value is actually a Collation ID,
	// and hence it's casted as such here.
	// If the user has specified a custom Collation in the ConnParams for this
	// connection, once the CharacterSet has been negotiated, we will override
	// it via SQL and update this field accordingly.
	CharacterSet collations.ID

	// ExpectSemiSyncIndicator is applicable when the connection is used for replication (ComBinlogDump).
	// When 'true', events are assumed to be padded with 2-byte semi-sync information
	// See https://dev.mysql.com/doc/internals/en/semi-sync-binlog-event.html
	ExpectSemiSyncIndicator bool
	// contains filtered or unexported fields
}

Conn is a connection between a client and a server, using the MySQL binary protocol. It is built on top of an existing net.Conn, that has already been established.

Use Connect on the client side to create a connection. Use NewListener to create a server side and listen for connections.

func Connect

func Connect(ctx context.Context, params *ConnParams) (*Conn, error)

Connect creates a connection to a server. It then handles the initial handshake.

If context is canceled before the end of the process, this function will return nil, ctx.Err().

FIXME(alainjobart) once we have more of a server side, add test cases to cover all failure scenarios.

func GetTestConn added in v0.18.0

func GetTestConn() *Conn

GetTestConn returns a conn for testing purpose only.

func GetTestServerConn added in v0.19.0

func GetTestServerConn(listener *Listener) *Conn

GetTestServerConn is only meant to be used for testing. It creates a server connection using a testConn and the provided listener.

func (*Conn) AnalyzeSemiSyncAckRequest added in v0.14.0

func (c *Conn) AnalyzeSemiSyncAckRequest(buf []byte) (strippedBuf []byte, ackRequested bool, err error)

AnalyzeSemiSyncAckRequest is given a packet and checks if the packet has a semi-sync Ack request. This is only applicable to binlog dump event packets. If semi sync information exists, then the function returns a stopped buf that should be then processed as the event data

func (*Conn) BaseShowTables added in v0.10.0

func (c *Conn) BaseShowTables() string

BaseShowTables returns a query that shows tables

func (*Conn) BaseShowTablesWithSizes added in v0.16.0

func (c *Conn) BaseShowTablesWithSizes() string

BaseShowTablesWithSizes returns a query that shows tables and their sizes

func (*Conn) BinlogInformation added in v0.20.0

func (c *Conn) BinlogInformation() (string, bool, bool, string, error)

func (*Conn) CancelCtx added in v0.18.0

func (c *Conn) CancelCtx()

CancelCtx aborts an existing running query

func (*Conn) CatchupToGTIDCommands added in v0.20.0

func (c *Conn) CatchupToGTIDCommands(params *ConnParams, pos replication.Position) []string

func (*Conn) Close

func (c *Conn) Close()

Close closes the connection. It can be called from a different go routine to interrupt the current connection.

func (*Conn) CloseResult

func (c *Conn) CloseResult()

CloseResult can be used to terminate a streaming query early. It just drains the remaining values.

func (*Conn) ConnCheck added in v0.19.0

func (c *Conn) ConnCheck() error

ConnCheck ensures that this connection to the MySQL server hasn't been broken. This is a fast, non-blocking check. For details on its implementation, please read "Three Bugs in the Go MySQL Driver" (Vicent Marti, GitHub, 2020) https://github.blog/2020-05-20-three-bugs-in-the-go-mysql-driver/

func (*Conn) ExecuteFetch

func (c *Conn) ExecuteFetch(query string, maxrows int, wantfields bool) (result *sqltypes.Result, err error)

ExecuteFetch executes a query and returns the result. Returns a SQLError. Depending on the transport used, the error returned might be different for the same condition:

1. if the server closes the connection when no command is in flight:

	1.1 unix: WriteComQuery will fail with a 'broken pipe', and we'll
	    return CRServerGone(2006).

	1.2 tcp: WriteComQuery will most likely work, but readComQueryResponse
	    will fail, and we'll return CRServerLost(2013).

	    This is because closing a TCP socket on the server side sends
	    a FIN to the client (telling the client the server is done
	    writing), but on most platforms doesn't send a RST.  So the
	    client has no idea it can't write. So it succeeds writing data, which
	    *then* triggers the server to send a RST back, received a bit
	    later. By then, the client has already started waiting for
	    the response, and will just return a CRServerLost(2013).
	    So CRServerGone(2006) will almost never be seen with TCP.

 2. if the server closes the connection when a command is in flight,
    readComQueryResponse will fail, and we'll return CRServerLost(2013).

func (*Conn) ExecuteFetchMulti

func (c *Conn) ExecuteFetchMulti(query string, maxrows int, wantfields bool) (result *sqltypes.Result, more bool, err error)

ExecuteFetchMulti is for fetching multiple results from a multi-statement result. It returns an additional 'more' flag. If it is set, you must fetch the additional results using ReadQueryResult.

func (*Conn) ExecuteFetchMultiDrain added in v0.20.0

func (c *Conn) ExecuteFetchMultiDrain(query string) (err error)

ExecuteFetchMultiDrain is for executing multiple statements in one call, but without caring for any results. The function returns an error if any of the statements fail. The function drains the query results of all statements, even if there's an error.

func (*Conn) ExecuteFetchWithWarningCount

func (c *Conn) ExecuteFetchWithWarningCount(query string, maxrows int, wantfields bool) (result *sqltypes.Result, warnings uint16, err error)

ExecuteFetchWithWarningCount is for fetching results and a warning count Note: In a future iteration this should be abolished and merged into the ExecuteFetch API.

func (*Conn) ExecuteStreamFetch

func (c *Conn) ExecuteStreamFetch(query string) (err error)

ExecuteStreamFetch starts a streaming query. Fields(), FetchNext() and CloseResult() can be called once this is successful. Returns a SQLError.

func (*Conn) FetchNext

func (c *Conn) FetchNext(in []sqltypes.Value) ([]sqltypes.Value, error)

FetchNext returns the next result for an ongoing streaming query. It returns (nil, nil) if there is nothing more to read.

func (*Conn) Fields

func (c *Conn) Fields() ([]*querypb.Field, error)

Fields returns the fields for an ongoing streaming query.

func (*Conn) GetGTIDMode added in v0.14.0

func (c *Conn) GetGTIDMode() (string, error)

GetGTIDMode returns the tablet's GTID mode. Only available in MySQL flavour

func (*Conn) GetGTIDPurged added in v0.14.0

func (c *Conn) GetGTIDPurged() (replication.Position, error)

GetGTIDPurged returns the tablet's GTIDs which are purged.

func (*Conn) GetRawConn added in v0.11.0

func (c *Conn) GetRawConn() net.Conn

GetRawConn returns the raw net.Conn for nefarious purposes.

func (*Conn) GetServerUUID added in v0.14.0

func (c *Conn) GetServerUUID() (string, error)

GetServerUUID returns the server's UUID.

func (*Conn) GetTLSClientCerts

func (c *Conn) GetTLSClientCerts() []*x509.Certificate

GetTLSClientCerts gets TLS certificates.

func (*Conn) ID

func (c *Conn) ID() int64

ID returns the MySQL connection ID for this connection.

func (*Conn) IsClientUnixSocket added in v0.21.0

func (c *Conn) IsClientUnixSocket() bool

IsClientUnixSocket returns true if the client connection is over a Unix socket with the server.

func (*Conn) IsClosed

func (c *Conn) IsClosed() bool

IsClosed returns true if this connection was ever closed by the Close() method. Note if the other side closes the connection, but Close() wasn't called, this will return false.

func (*Conn) IsMariaDB

func (c *Conn) IsMariaDB() bool

IsMariaDB returns true iff the other side of the client connection is identified as MariaDB. Most applications should not care, but this is useful in tests.

func (*Conn) IsMarkedForClose added in v0.18.0

func (c *Conn) IsMarkedForClose() bool

IsMarkedForClose return true if the connection should be closed.

func (*Conn) IsShuttingDown added in v0.19.0

func (c *Conn) IsShuttingDown() bool

func (*Conn) IsUnixSocket added in v0.12.0

func (c *Conn) IsUnixSocket() bool

IsUnixSocket returns true if the server connection is over a Unix socket.

func (*Conn) MarkForClose added in v0.18.0

func (c *Conn) MarkForClose()

MarkForClose marks the connection for close.

func (*Conn) Ping

func (c *Conn) Ping() error

Ping implements mysql ping command.

func (*Conn) PrimaryFilePosition added in v0.11.0

func (c *Conn) PrimaryFilePosition() (replication.Position, error)

PrimaryFilePosition returns the current primary's file based replication position.

func (*Conn) PrimaryPosition added in v0.11.0

func (c *Conn) PrimaryPosition() (replication.Position, error)

PrimaryPosition returns the current primary's replication position.

func (*Conn) ReadBinlogEvent

func (c *Conn) ReadBinlogEvent() (BinlogEvent, error)

ReadBinlogEvent reads the next BinlogEvent. This must be used in conjunction with SendBinlogDumpCommand.

func (*Conn) ReadPacket

func (c *Conn) ReadPacket() ([]byte, error)

ReadPacket reads a packet from the underlying connection. it is the public API version, that returns a SQLError. The memory for the packet is always allocated, and it is owned by the caller after this function returns.

func (*Conn) ReadQueryResult

func (c *Conn) ReadQueryResult(maxrows int, wantfields bool) (*sqltypes.Result, bool, uint16, error)

ReadQueryResult gets the result from the last written query.

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr returns the underlying socket RemoteAddr().

func (*Conn) ReplicationConfiguration added in v0.20.0

func (c *Conn) ReplicationConfiguration() (*replicationdata.Configuration, error)

ReplicationConfiguration reads the right global variables and performance schema information.

func (*Conn) ResetBinaryLogsCommand added in v0.20.0

func (c *Conn) ResetBinaryLogsCommand() string

ResetBinaryLogsCommand returns the command used to reset the binary logs on the server.

func (*Conn) ResetReplicationCommand added in v0.20.0

func (c *Conn) ResetReplicationCommand() string

func (*Conn) ResetReplicationCommands

func (c *Conn) ResetReplicationCommands() []string

ResetReplicationCommands returns the commands to completely reset replication on the host.

func (*Conn) ResetReplicationParametersCommands added in v0.14.0

func (c *Conn) ResetReplicationParametersCommands() []string

ResetReplicationParametersCommands returns the commands to reset replication parameters on the host.

func (*Conn) RestartReplicationCommands

func (c *Conn) RestartReplicationCommands() []string

RestartReplicationCommands returns the commands to stop, reset and start replication.

func (*Conn) SemiSyncExtensionLoaded

func (c *Conn) SemiSyncExtensionLoaded() (SemiSyncType, error)

SemiSyncExtensionLoaded checks if the semisync extension has been loaded. It should work for both MariaDB and MySQL.

func (*Conn) SendBinlogDumpCommand

func (c *Conn) SendBinlogDumpCommand(serverID uint32, binlogFilename string, startPos replication.Position) error

SendBinlogDumpCommand sends the flavor-specific version of the COM_BINLOG_DUMP command to start dumping raw binlog events over a server connection, starting at a given GTID.

func (*Conn) SendSemiSyncAck added in v0.14.0

func (c *Conn) SendSemiSyncAck(binlogFilename string, binlogPos uint64) error

SendSemiSyncAck sends an ACK to the source, in response to binlog events the source has tagged with a SEMI_SYNC_ACK_REQ see https://dev.mysql.com/doc/internals/en/semi-sync-ack-packet.html

func (*Conn) ServerVersionAtLeast added in v0.14.0

func (c *Conn) ServerVersionAtLeast(parts ...int) (bool, error)

ServerVersionAtLeast returns 'true' if server version is equal or greater than given parts. e.g. "8.0.14-log" is at least [8, 0, 13] and [8, 0, 14], but not [8, 0, 15]

func (*Conn) SetReplicationPositionCommands

func (c *Conn) SetReplicationPositionCommands(pos replication.Position) []string

SetReplicationPositionCommands returns the commands to set the replication position at which the replica will resume when it is later reparented with SetReplicationSourceCommand.

func (*Conn) SetReplicationSourceCommand added in v0.11.0

func (c *Conn) SetReplicationSourceCommand(params *ConnParams, host string, port int32, heartbeatInterval float64, connectRetry int) string

SetReplicationSourceCommand returns the command to use the provided host/port as the new replication source (without changing any GTID position). It is guaranteed to be called with replication stopped. It should not start or stop replication.

func (*Conn) ShowPrimaryStatus added in v0.11.0

func (c *Conn) ShowPrimaryStatus() (replication.PrimaryStatus, error)

ShowPrimaryStatus executes the right SHOW BINARY LOG STATUS command, and returns a parsed executed Position, as well as file based Position.

func (*Conn) ShowReplicationStatus

func (c *Conn) ShowReplicationStatus() (replication.ReplicationStatus, error)

ShowReplicationStatus executes the right command to fetch replication status, and returns a parsed Position with other fields.

func (*Conn) StartReplicationCommand

func (c *Conn) StartReplicationCommand() string

StartReplicationCommand returns the command to start replication.

func (*Conn) StartReplicationUntilAfterCommand

func (c *Conn) StartReplicationUntilAfterCommand(pos replication.Position) string

StartReplicationUntilAfterCommand returns the command to start replication.

func (*Conn) StartSQLThreadCommand added in v0.13.0

func (c *Conn) StartSQLThreadCommand() string

StartSQLThreadCommand returns the command to start the replica's SQL thread.

func (*Conn) StartSQLThreadUntilAfterCommand added in v0.13.2

func (c *Conn) StartSQLThreadUntilAfterCommand(pos replication.Position) string

StartSQLThreadUntilAfterCommand returns the command to start the replica's SQL thread(s) and have it run until it has reached the given position, at which point it will stop.

func (*Conn) StopIOThreadCommand

func (c *Conn) StopIOThreadCommand() string

StopIOThreadCommand returns the command to stop the replica's io thread.

func (*Conn) StopReplicationCommand

func (c *Conn) StopReplicationCommand() string

StopReplicationCommand returns the command to stop the replication.

func (*Conn) StopSQLThreadCommand added in v0.13.2

func (c *Conn) StopSQLThreadCommand() string

StopSQLThreadCommand returns the command to stop the replica's SQL thread(s).

func (*Conn) String

func (c *Conn) String() string

Ident returns a useful identification string for error logging

func (*Conn) SupportsCapability added in v0.14.0

func (c *Conn) SupportsCapability(capability capabilities.FlavorCapability) (bool, error)

SupportsCapability checks if the database server supports the given capability

func (*Conn) TLSEnabled added in v0.12.0

func (c *Conn) TLSEnabled() bool

TLSEnabled returns true if this connection is using TLS.

func (*Conn) UpdateCancelCtx added in v0.18.0

func (c *Conn) UpdateCancelCtx(cancel context.CancelFunc)

UpdateCancelCtx updates the cancel function on the connection.

func (*Conn) WaitUntilFilePosition added in v0.19.0

func (c *Conn) WaitUntilFilePosition(ctx context.Context, pos replication.Position) error

WaitUntilFilePosition waits until the given position is reached or until the context expires for the file position flavor. It returns an error if we did not succeed.

func (*Conn) WaitUntilPosition added in v0.19.0

func (c *Conn) WaitUntilPosition(ctx context.Context, pos replication.Position) error

WaitUntilPosition waits until the given position is reached or until the context expires. It returns an error if we did not succeed.

func (*Conn) WriteBinlogEvent added in v0.16.0

func (c *Conn) WriteBinlogEvent(ev BinlogEvent, semiSyncEnabled bool) error

WriteBinlogEvent writes a binlog event as part of a replication stream https://dev.mysql.com/doc/internals/en/binlog-network-stream.html https://dev.mysql.com/doc/internals/en/binlog-event.html

func (*Conn) WriteComBinlogDump

func (c *Conn) WriteComBinlogDump(serverID uint32, binlogFilename string, binlogPos uint32, flags uint16) error

WriteComBinlogDump writes a ComBinlogDump command. See http://dev.mysql.com/doc/internals/en/com-binlog-dump.html for syntax. Returns a SQLError.

func (*Conn) WriteComBinlogDumpGTID

func (c *Conn) WriteComBinlogDumpGTID(serverID uint32, binlogFilename string, binlogPos uint64, flags uint16, gtidSet []byte) error

WriteComBinlogDumpGTID writes a ComBinlogDumpGTID command. Only works with MySQL 5.6+ (and not MariaDB). See http://dev.mysql.com/doc/internals/en/com-binlog-dump-gtid.html for syntax.

func (*Conn) WriteComQuery

func (c *Conn) WriteComQuery(query string) error

WriteComQuery writes a query for the server to execute. Client -> Server. Returns SQLError(CRServerGone) if it can't.

func (*Conn) WriteErrorAndLog added in v0.16.0

func (c *Conn) WriteErrorAndLog(format string, args ...interface{}) bool

type ConnParams

type ConnParams struct {
	Host       string
	Port       int
	Uname      string
	Pass       string
	DbName     string
	UnixSocket string
	Charset    collations.ID
	Flags      uint64
	Flavor     string

	// The following SSL flags control the SSL behavior.
	//
	// Not setting this value implies preferred mode unless
	// the CapabilityClientSSL bit is set in db_flags. In the
	// flag is set, it ends up equivalent to verify_identity mode.
	SslMode          vttls.SslMode
	SslCa            string
	SslCaPath        string
	SslCert          string
	SslCrl           string
	SslKey           string
	TLSMinVersion    string
	ServerName       string
	ConnectTimeoutMs uint64

	// The following is only set to force the client to connect without
	// using CapabilityClientDeprecateEOF
	DisableClientDeprecateEOF bool

	// EnableQueryInfo sets whether the results from queries performed by this
	// connection should include the 'info' field that MySQL usually returns. This 'info'
	// field usually contains a human-readable text description of the executed query
	// for informative purposes. It has no programmatic value. Returning this field is
	// disabled by default.
	EnableQueryInfo bool

	// FlushDelay is the delay after which buffered response will be flushed to the client.
	FlushDelay time.Duration

	TruncateErrLen int
}

ConnParams contains all the parameters to use to connect to mysql.

func (*ConnParams) EffectiveSslMode added in v0.12.0

func (cp *ConnParams) EffectiveSslMode() vttls.SslMode

EffectiveSslMode computes the effective SslMode. If SslMode is explicitly set, it uses that to determine this, if it's not set it falls back to the legacy db_flags behavior.

func (*ConnParams) EnableClientFoundRows

func (cp *ConnParams) EnableClientFoundRows()

EnableClientFoundRows sets the flag for CLIENT_FOUND_ROWS.

func (*ConnParams) EnableSSL

func (cp *ConnParams) EnableSSL()

EnableSSL will set the right flag on the parameters.

func (*ConnParams) SslEnabled

func (cp *ConnParams) SslEnabled() bool

SslEnabled returns if SSL is enabled. If the effective ssl mode is preferred, it checks the unix socket and hostname to see if we're not connecting to local MySQL.

func (*ConnParams) SslRequired added in v0.12.0

func (cp *ConnParams) SslRequired() bool

SslRequired returns whether the connection parameters define that SSL is a requirement. If SslMode is set, it uses that to determine this, if it's not set it falls back to the legacy db_flags behavior.

type FakeBinlogStream

type FakeBinlogStream struct {
	// ServerID is the server ID of the originating mysql-server.
	ServerID uint32

	// LogPosition is an incrementing log position.
	LogPosition uint32

	// Timestamp is a uint32 of when the events occur. It is not changed.
	Timestamp uint32
}

FakeBinlogStream is used to generate consistent BinlogEvent packets for a stream. It makes sure the ServerID and log positions are reasonable.

func NewFakeBinlogStream

func NewFakeBinlogStream() *FakeBinlogStream

NewFakeBinlogStream returns a simple FakeBinlogStream.

func (*FakeBinlogStream) Packetize

func (s *FakeBinlogStream) Packetize(f BinlogFormat, typ byte, flags uint16, data []byte) []byte

Packetize adds the binlog event header to a packet, and optionally the checksum.

type Getter

type Getter interface {
	Get() *querypb.VTGateCallerID
}

A Getter has a Get()

type Handler

type Handler interface {
	// NewConnection is called when a connection is created.
	// It is not established yet. The handler can decide to
	// set StatusFlags that will be returned by the handshake methods.
	// In particular, ServerStatusAutocommit might be set.
	NewConnection(c *Conn)

	// ConnectionReady is called after the connection handshake, but
	// before we begin to process commands.
	ConnectionReady(c *Conn)

	// ConnectionClosed is called when a connection is closed.
	ConnectionClosed(c *Conn)

	// ComQuery is called when a connection receives a query.
	// Note the contents of the query slice may change after
	// the first call to callback. So the Handler should not
	// hang on to the byte slice.
	ComQuery(c *Conn, query string, callback func(*sqltypes.Result) error) error

	// ComPrepare is called when a connection receives a prepared
	// statement query.
	ComPrepare(c *Conn, query string, bindVars map[string]*querypb.BindVariable) ([]*querypb.Field, error)

	// ComStmtExecute is called when a connection receives a statement
	// execute query.
	ComStmtExecute(c *Conn, prepare *PrepareData, callback func(*sqltypes.Result) error) error

	// ComRegisterReplica is called when a connection receives a ComRegisterReplica request
	ComRegisterReplica(c *Conn, replicaHost string, replicaPort uint16, replicaUser string, replicaPassword string) error

	// ComBinlogDump is called when a connection receives a ComBinlogDump request
	ComBinlogDump(c *Conn, logFile string, binlogPos uint32) error

	// ComBinlogDumpGTID is called when a connection receives a ComBinlogDumpGTID request
	ComBinlogDumpGTID(c *Conn, logFile string, logPos uint64, gtidSet replication.GTIDSet) error

	// WarningCount is called at the end of each query to obtain
	// the value to be returned to the client in the EOF packet.
	// Note that this will be called either in the context of the
	// ComQuery callback if the result does not contain any fields,
	// or after the last ComQuery call completes.
	WarningCount(c *Conn) uint16

	ComResetConnection(c *Conn)

	Env() *vtenv.Environment
}

A Handler is an interface used by Listener to send queries. The implementation of this interface may store data in the ClientData field of the Connection for its own purposes.

For a given Connection, all these methods are serialized. It means only one of these methods will be called concurrently for a given Connection. So access to the Connection ClientData does not need to be protected by a mutex.

However, each connection is using one go routine, so multiple Connection objects can call these concurrently, for different Connections.

type HashStorage added in v0.12.0

type HashStorage interface {
	UserEntryWithHash(conn *Conn, salt []byte, user string, authResponse []byte, remoteAddr net.Addr) (Getter, error)
}

HashStorage describes an object that is suitable to retrieve user information based on the hashed authentication response for mysql_native_password.

In general, an implementation of this would use an internally stored password that is hashed twice with SHA1.

The VerifyHashedMysqlNativePassword helper method can be used to verify such a hash based on the salt and auth response provided here after retrieving the hashed password from the storage.

type Listener

type Listener struct {

	// ServerVersion is the version we will advertise.
	ServerVersion string

	// TLSConfig is the server TLS config. If set, we will advertise
	// that we support SSL.
	// atomic value stores *tls.Config
	TLSConfig atomic.Value

	// AllowClearTextWithoutTLS needs to be set for the
	// mysql_clear_password authentication method to be accepted
	// by the server when TLS is not in use.
	AllowClearTextWithoutTLS atomic.Bool

	// SlowConnectWarnThreshold if non-zero specifies an amount of time
	// beyond which a warning is logged to identify the slow connection
	SlowConnectWarnThreshold atomic.Int64

	// RequireSecureTransport configures the server to reject connections from insecure clients
	RequireSecureTransport bool

	// PreHandleFunc is called for each incoming connection, immediately after
	// accepting a new connection. By default it's no-op. Useful for custom
	// connection inspection or TLS termination. The returned connection is
	// handled further by the MySQL handler. An non-nil error will stop
	// processing the connection by the MySQL handler.
	PreHandleFunc func(context.Context, net.Conn, uint32) (net.Conn, error)
	// contains filtered or unexported fields
}

Listener is the MySQL server protocol listener.

func NewFromListener

func NewFromListener(
	l net.Listener,
	authServer AuthServer,
	handler Handler,
	connReadTimeout time.Duration,
	connWriteTimeout time.Duration,
	connBufferPooling bool,
	keepAlivePeriod time.Duration,
	flushDelay time.Duration,
) (*Listener, error)

NewFromListener creates a new mysql listener from an existing net.Listener

func NewListener

func NewListener(
	protocol, address string,
	authServer AuthServer,
	handler Handler,
	connReadTimeout time.Duration,
	connWriteTimeout time.Duration,
	proxyProtocol bool,
	connBufferPooling bool,
	keepAlivePeriod time.Duration,
	flushDelay time.Duration,
) (*Listener, error)

NewListener creates a new Listener.

func NewListenerWithConfig

func NewListenerWithConfig(cfg ListenerConfig) (*Listener, error)

NewListenerWithConfig creates new listener using provided config. There are no default values for config, so caller should ensure its correctness.

func (*Listener) Accept

func (l *Listener) Accept()

Accept runs an accept loop until the listener is closed.

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the listener address.

func (*Listener) Close

func (l *Listener) Close()

Close stops the listener, which prevents accept of any new connections. Existing connections won't be closed.

func (*Listener) Shutdown

func (l *Listener) Shutdown()

Shutdown closes listener and fails any Ping requests from existing connections. This can be used for graceful shutdown, to let clients know that they should reconnect to another server.

type ListenerConfig

type ListenerConfig struct {
	// Protocol-Address pair and Listener are mutually exclusive parameters
	Protocol            string
	Address             string
	Listener            net.Listener
	AuthServer          AuthServer
	Handler             Handler
	ConnReadTimeout     time.Duration
	ConnWriteTimeout    time.Duration
	ConnReadBufferSize  int
	ConnBufferPooling   bool
	ConnKeepAlivePeriod time.Duration
	FlushDelay          time.Duration
}

ListenerConfig should be used with NewListenerWithConfig to specify listener parameters.

type NoneGetter

type NoneGetter struct{}

NoneGetter holds the empty string

func (*NoneGetter) Get

func (ng *NoneGetter) Get() *querypb.VTGateCallerID

Get returns the empty string

type PacketComStmtPrepareOK added in v0.9.0

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

PacketComStmtPrepareOK contains the COM_STMT_PREPARE_OK packet details

type PacketOK added in v0.9.0

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

PacketOK contains the ok packet details

type PlainTextStorage added in v0.12.0

type PlainTextStorage interface {
	UserEntryWithPassword(conn *Conn, user string, password string, remoteAddr net.Addr) (Getter, error)
}

PlainTextStorage describes an object that is suitable to retrieve user information based on the plain text password of a user. This can be obtained through various Mysql authentication methods, such as `mysql_clear_passwrd`, `dialog` or `caching_sha2_password` in the full authentication handshake case of the latter.

This mechanism also would allow for picking your own password storage in the backend, such as BCrypt, SCrypt, PBKDF2 or Argon2 once the plain text is obtained.

When comparing plain text passwords directly, please ensure to use `subtle.ConstantTimeCompare` to prevent timing based attacks on the password.

type PrepareData

type PrepareData struct {
	ParamsType  []int32
	ColumnNames []string
	PrepareStmt string
	BindVars    map[string]*querypb.BindVariable
	StatementID uint32
	ParamsCount uint16
}

PrepareData is a buffer used for store prepare statement meta data

type Query

type Query struct {
	Database string
	Charset  *binlogdatapb.Charset
	SQL      string
}

Query contains data from a QUERY_EVENT.

func (Query) String

func (q Query) String() string

String pretty-prints a Query.

type Row

type Row struct {
	// NullIdentifyColumns describes which of the identify columns are NULL.
	// It is only set for UPDATE and DELETE events.
	NullIdentifyColumns Bitmap

	// NullColumns describes which of the present columns are NULL.
	// It is only set for WRITE and UPDATE events.
	NullColumns Bitmap

	// Identify is the raw data for the columns used to identify a row.
	// It is only set for UPDATE and DELETE events.
	Identify []byte

	// Data is the raw data.
	// It is only set for WRITE and UPDATE events.
	Data []byte
}

Row contains data for a single Row in a Rows event.

type Rows

type Rows struct {
	// Flags has the flags from the event.
	Flags uint16

	// IdentifyColumns describes which columns are included to
	// identify the row. It is a bitmap indexed by the TableMap
	// list of columns.
	// Set for UPDATE and DELETE.
	IdentifyColumns Bitmap

	// DataColumns describes which columns are included. It is
	// a bitmap indexed by the TableMap list of columns.
	// Set for WRITE and UPDATE.
	DataColumns Bitmap

	// Rows is an array of Row in the event.
	Rows []Row
}

Rows contains data from a {WRITE,UPDATE,DELETE}_ROWS_EVENT.

func (*Rows) StringIdentifiesForTests

func (rs *Rows) StringIdentifiesForTests(tm *TableMap, rowIndex int) ([]string, error)

StringIdentifiesForTests is a helper method to return the string identify of all columns in a row in a Row. Only use it in tests, as the returned values cannot be interpreted correctly without the schema. We assume everything is unsigned in this method.

func (*Rows) StringValuesForTests

func (rs *Rows) StringValuesForTests(tm *TableMap, rowIndex int) ([]string, error)

StringValuesForTests is a helper method to return the string value of all columns in a row in a Row. Only use it in tests, as the returned values cannot be interpreted correctly without the schema. We assume everything is unsigned in this method.

type SemiSyncType added in v0.20.0

type SemiSyncType int8
const (
	SemiSyncTypeUnknown SemiSyncType = iota
	SemiSyncTypeOff
	SemiSyncTypeSource
	SemiSyncTypeMaster
)

type StaticUserData

type StaticUserData struct {
	Username string
	Groups   []string
}

StaticUserData holds the username and groups

func (*StaticUserData) Get

Get returns the wrapped username and groups

type TableMap

type TableMap struct {
	// Flags is the table's flags.
	Flags uint16

	// Database is the database name.
	Database string

	// Name is the name of the table.
	Name string

	// Types is an array of MySQL types for the fields.
	Types []byte

	// CanBeNull's bits are set if the column can be NULL.
	CanBeNull Bitmap

	// Metadata is an array of uint16, one per column.
	// It contains a few extra information about each column,
	// that is dependent on the type.
	// - If the metadata is not present, this is zero.
	// - If the metadata is one byte, only the lower 8 bits are used.
	// - If the metadata is two bytes, all 16 bits are used.
	Metadata []uint16

	// ColumnCollationIDs contains information about the inherited
	// or implied column default collation and any explicit per-column
	// override for text based columns ONLY. This means that the
	// array position needs to be mapped to the ordered list of
	// text based columns in the table.
	ColumnCollationIDs []collations.ID
}

TableMap contains data from a TABLE_MAP_EVENT.

type TransactionPayload added in v0.17.0

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

func (*TransactionPayload) Close added in v0.21.0

func (tp *TransactionPayload) Close()

Close should be called in a defer where the TransactionPayload is used to ensure that the underlying reader and related resources used are cleaned up.

func (*TransactionPayload) GetNextEvent added in v0.21.0

func (tp *TransactionPayload) GetNextEvent() (BinlogEvent, error)

GetNextEvent returns the next binlog event that was contained within the compressed transaction payload. It will return io.EOF when there are no more events left in the payload.

type UnimplementedHandler added in v0.13.0

type UnimplementedHandler struct{}

UnimplementedHandler implemnts all of the optional callbacks so as to satisy the Handler interface. Intended to be embedded into your custom Handler implementation without needing to define every callback and to help be forwards compatible when new functions are added.

func (UnimplementedHandler) ComResetConnection added in v0.13.0

func (UnimplementedHandler) ComResetConnection(*Conn)

func (UnimplementedHandler) ConnectionClosed added in v0.13.0

func (UnimplementedHandler) ConnectionClosed(*Conn)

func (UnimplementedHandler) ConnectionReady added in v0.13.0

func (UnimplementedHandler) ConnectionReady(*Conn)

func (UnimplementedHandler) NewConnection added in v0.13.0

func (UnimplementedHandler) NewConnection(*Conn)

type UserValidator added in v0.12.0

type UserValidator interface {
	HandleUser(user string) bool
}

UserValidator is an interface that allows checking if a specific user will work for an auth method. This interface is called by all the default helpers that create AuthMethod instances for the various supported Mysql authentication methods.

Directories

Path Synopsis
charset/korean
Package korean provides Korean encodings such as EUC-KR.
Package korean provides Korean encodings such as EUC-KR.
charset/simplifiedchinese
Package simplifiedchinese provides Simplified Chinese encodings such as GBK.
Package simplifiedchinese provides Simplified Chinese encodings such as GBK.
vindex
text is a repository of text-related packages related to internationalization (i18n) and localization (l10n), such as character encodings, text transformations, and locale-specific text handling.
text is a repository of text-related packages related to internationalization (i18n) and localization (l10n), such as character encodings, text transformations, and locale-specific text handling.
vindex/collate
Package collate contains types for comparing and sorting Unicode strings according to a given collation order.
Package collate contains types for comparing and sorting Unicode strings according to a given collation order.
vindex/unicode
unicode holds packages with implementations of Unicode standards that are mostly used as building blocks for other packages in vitess.io/vitess/go/mysql/collations/vindex, layout engines, or are otherwise more low-level in nature.
unicode holds packages with implementations of Unicode standards that are mostly used as building blocks for other packages in vitess.io/vitess/go/mysql/collations/vindex, layout engines, or are otherwise more low-level in nature.
vindex/unicode/norm
Package norm contains types and functions for normalizing Unicode strings.
Package norm contains types and functions for normalizing Unicode strings.
Package endtoend is a test-only package.
Package endtoend is a test-only package.
Package fakesqldb provides a MySQL server for tests.
Package fakesqldb provides a MySQL server for tests.

Jump to

Keyboard shortcuts

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