mysql

package
v0.17.4 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: Apache-2.0 Imports: 54 Imported by: 54

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
	// Offset from 0 where the 4 byte length is stored.
	BinlogEventLenOffset = 9
	// Byte length of the checksum suffix when the CRC32 algorithm is used.
	BinlogCRC32ChecksumLen = 4
)
View Source
const (
	TransactionPayloadCompressionZstd = 0
	TransactionPayloadCompressionNone = 255
)

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

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 (
	// CRUnknownError is CR_UNKNOWN_ERROR
	CRUnknownError = ErrorCode(2000)

	// CRConnectionError is CR_CONNECTION_ERROR
	// This is returned if a connection via a Unix socket fails.
	CRConnectionError = ErrorCode(2002)

	// CRConnHostError is CR_CONN_HOST_ERROR
	// This is returned if a connection via a TCP socket fails.
	CRConnHostError = ErrorCode(2003)

	// CRUnknownHost is CR_UNKNOWN_HOST
	// This is returned if the host name cannot be resolved.
	CRUnknownHost = ErrorCode(2005)

	// CRServerGone is CR_SERVER_GONE_ERROR.
	// This is returned if the client tries to send a command but it fails.
	CRServerGone = ErrorCode(2006)

	// CRVersionError is CR_VERSION_ERROR
	// This is returned if the server versions don't match what we support.
	CRVersionError = ErrorCode(2007)

	// CRServerHandshakeErr is CR_SERVER_HANDSHAKE_ERR
	CRServerHandshakeErr = ErrorCode(2012)

	// CRServerLost is CR_SERVER_LOST.
	// Used when:
	// - the client cannot write an initial auth packet.
	// - the client cannot read an initial auth packet.
	// - the client cannot read a response from the server.
	//     This happens when a running query is killed.
	CRServerLost = ErrorCode(2013)

	// CRCommandsOutOfSync is CR_COMMANDS_OUT_OF_SYNC
	// Sent when the streaming calls are not done in the right order.
	CRCommandsOutOfSync = ErrorCode(2014)

	// CRNamedPipeStateError is CR_NAMEDPIPESETSTATE_ERROR.
	// This is the highest possible number for a connection error.
	CRNamedPipeStateError = ErrorCode(2018)

	// CRCantReadCharset is CR_CANT_READ_CHARSET
	CRCantReadCharset = ErrorCode(2019)

	// CRSSLConnectionError is CR_SSL_CONNECTION_ERROR
	CRSSLConnectionError = ErrorCode(2026)

	// CRMalformedPacket is CR_MALFORMED_PACKET
	CRMalformedPacket = ErrorCode(2027)
)

Error codes for client-side errors. Originally found in include/mysql/errmsg.h and https://dev.mysql.com/doc/mysql-errors/en/client-error-reference.html

View Source
const (
	// Vitess specific errors, (100-999)
	ERNotReplica = ErrorCode(100)

	// unknown
	ERUnknownError = ErrorCode(1105)

	// internal
	ERInternalError = ErrorCode(1815)

	// unimplemented
	ERNotSupportedYet = ErrorCode(1235)
	ERUnsupportedPS   = ErrorCode(1295)

	// resource exhausted
	ERDiskFull               = ErrorCode(1021)
	EROutOfMemory            = ErrorCode(1037)
	EROutOfSortMemory        = ErrorCode(1038)
	ERConCount               = ErrorCode(1040)
	EROutOfResources         = ErrorCode(1041)
	ERRecordFileFull         = ErrorCode(1114)
	ERHostIsBlocked          = ErrorCode(1129)
	ERCantCreateThread       = ErrorCode(1135)
	ERTooManyDelayedThreads  = ErrorCode(1151)
	ERNetPacketTooLarge      = ErrorCode(1153)
	ERTooManyUserConnections = ErrorCode(1203)
	ERLockTableFull          = ErrorCode(1206)
	ERUserLimitReached       = ErrorCode(1226)

	// deadline exceeded
	ERLockWaitTimeout = ErrorCode(1205)

	// unavailable
	ERServerShutdown = ErrorCode(1053)

	// not found
	ERDbDropExists          = ErrorCode(1008)
	ERCantFindFile          = ErrorCode(1017)
	ERFormNotFound          = ErrorCode(1029)
	ERKeyNotFound           = ErrorCode(1032)
	ERBadFieldError         = ErrorCode(1054)
	ERNoSuchThread          = ErrorCode(1094)
	ERUnknownTable          = ErrorCode(1109)
	ERCantFindUDF           = ErrorCode(1122)
	ERNonExistingGrant      = ErrorCode(1141)
	ERNoSuchTable           = ErrorCode(1146)
	ERNonExistingTableGrant = ErrorCode(1147)
	ERKeyDoesNotExist       = ErrorCode(1176)

	// permissions
	ERDBAccessDenied            = ErrorCode(1044)
	ERAccessDeniedError         = ErrorCode(1045)
	ERKillDenied                = ErrorCode(1095)
	ERNoPermissionToCreateUsers = ErrorCode(1211)
	ERSpecifiedAccessDenied     = ErrorCode(1227)

	// failed precondition
	ERNoDb                          = ErrorCode(1046)
	ERNoSuchIndex                   = ErrorCode(1082)
	ERCantDropFieldOrKey            = ErrorCode(1091)
	ERTableNotLockedForWrite        = ErrorCode(1099)
	ERTableNotLocked                = ErrorCode(1100)
	ERTooBigSelect                  = ErrorCode(1104)
	ERNotAllowedCommand             = ErrorCode(1148)
	ERTooLongString                 = ErrorCode(1162)
	ERDelayedInsertTableLocked      = ErrorCode(1165)
	ERDupUnique                     = ErrorCode(1169)
	ERRequiresPrimaryKey            = ErrorCode(1173)
	ERCantDoThisDuringAnTransaction = ErrorCode(1179)
	ERReadOnlyTransaction           = ErrorCode(1207)
	ERCannotAddForeign              = ErrorCode(1215)
	ERNoReferencedRow               = ErrorCode(1216)
	ERRowIsReferenced               = ErrorCode(1217)
	ERCantUpdateWithReadLock        = ErrorCode(1223)
	ERNoDefault                     = ErrorCode(1230)
	ERMasterFatalReadingBinlog      = ErrorCode(1236)
	EROperandColumns                = ErrorCode(1241)
	ERSubqueryNo1Row                = ErrorCode(1242)
	ERUnknownStmtHandler            = ErrorCode(1243)
	ERWarnDataOutOfRange            = ErrorCode(1264)
	ERNonUpdateableTable            = ErrorCode(1288)
	ERFeatureDisabled               = ErrorCode(1289)
	EROptionPreventsStatement       = ErrorCode(1290)
	ERDuplicatedValueInType         = ErrorCode(1291)
	ERSPDoesNotExist                = ErrorCode(1305)
	ERNoDefaultForField             = ErrorCode(1364)
	ErSPNotVarArg                   = ErrorCode(1414)
	ERRowIsReferenced2              = ErrorCode(1451)
	ErNoReferencedRow2              = ErrorCode(1452)
	ERDupIndex                      = ErrorCode(1831)
	ERInnodbReadOnly                = ErrorCode(1874)

	// already exists
	ERDbCreateExists = ErrorCode(1007)
	ERTableExists    = ErrorCode(1050)
	ERDupEntry       = ErrorCode(1062)
	ERFileExists     = ErrorCode(1086)
	ERUDFExists      = ErrorCode(1125)

	// aborted
	ERGotSignal          = ErrorCode(1078)
	ERForcingClose       = ErrorCode(1080)
	ERAbortingConnection = ErrorCode(1152)
	ERLockDeadlock       = ErrorCode(1213)

	// invalid arg
	ERUnknownComError              = ErrorCode(1047)
	ERBadNullError                 = ErrorCode(1048)
	ERBadDb                        = ErrorCode(1049)
	ERBadTable                     = ErrorCode(1051)
	ERNonUniq                      = ErrorCode(1052)
	ERWrongFieldWithGroup          = ErrorCode(1055)
	ERWrongGroupField              = ErrorCode(1056)
	ERWrongSumSelect               = ErrorCode(1057)
	ERWrongValueCount              = ErrorCode(1058)
	ERTooLongIdent                 = ErrorCode(1059)
	ERDupFieldName                 = ErrorCode(1060)
	ERDupKeyName                   = ErrorCode(1061)
	ERWrongFieldSpec               = ErrorCode(1063)
	ERParseError                   = ErrorCode(1064)
	EREmptyQuery                   = ErrorCode(1065)
	ERNonUniqTable                 = ErrorCode(1066)
	ERInvalidDefault               = ErrorCode(1067)
	ERMultiplePriKey               = ErrorCode(1068)
	ERTooManyKeys                  = ErrorCode(1069)
	ERTooManyKeyParts              = ErrorCode(1070)
	ERTooLongKey                   = ErrorCode(1071)
	ERKeyColumnDoesNotExist        = ErrorCode(1072)
	ERBlobUsedAsKey                = ErrorCode(1073)
	ERTooBigFieldLength            = ErrorCode(1074)
	ERWrongAutoKey                 = ErrorCode(1075)
	ERWrongFieldTerminators        = ErrorCode(1083)
	ERBlobsAndNoTerminated         = ErrorCode(1084)
	ERTextFileNotReadable          = ErrorCode(1085)
	ERWrongSubKey                  = ErrorCode(1089)
	ERCantRemoveAllFields          = ErrorCode(1090)
	ERUpdateTableUsed              = ErrorCode(1093)
	ERNoTablesUsed                 = ErrorCode(1096)
	ERTooBigSet                    = ErrorCode(1097)
	ERBlobCantHaveDefault          = ErrorCode(1101)
	ERWrongDbName                  = ErrorCode(1102)
	ERWrongTableName               = ErrorCode(1103)
	ERUnknownProcedure             = ErrorCode(1106)
	ERWrongParamCountToProcedure   = ErrorCode(1107)
	ERWrongParametersToProcedure   = ErrorCode(1108)
	ERFieldSpecifiedTwice          = ErrorCode(1110)
	ERInvalidGroupFuncUse          = ErrorCode(1111)
	ERTableMustHaveColumns         = ErrorCode(1113)
	ERUnknownCharacterSet          = ErrorCode(1115)
	ERTooManyTables                = ErrorCode(1116)
	ERTooManyFields                = ErrorCode(1117)
	ERTooBigRowSize                = ErrorCode(1118)
	ERWrongOuterJoin               = ErrorCode(1120)
	ERNullColumnInIndex            = ErrorCode(1121)
	ERFunctionNotDefined           = ErrorCode(1128)
	ERWrongValueCountOnRow         = ErrorCode(1136)
	ERInvalidUseOfNull             = ErrorCode(1138)
	ERRegexpError                  = ErrorCode(1139)
	ERMixOfGroupFuncAndFields      = ErrorCode(1140)
	ERIllegalGrantForTable         = ErrorCode(1144)
	ERSyntaxError                  = ErrorCode(1149)
	ERWrongColumnName              = ErrorCode(1166)
	ERWrongKeyColumn               = ErrorCode(1167)
	ERBlobKeyWithoutLength         = ErrorCode(1170)
	ERPrimaryCantHaveNull          = ErrorCode(1171)
	ERTooManyRows                  = ErrorCode(1172)
	ERLockOrActiveTransaction      = ErrorCode(1192)
	ERUnknownSystemVariable        = ErrorCode(1193)
	ERSetConstantsOnly             = ErrorCode(1204)
	ERWrongArguments               = ErrorCode(1210)
	ERWrongUsage                   = ErrorCode(1221)
	ERWrongNumberOfColumnsInSelect = ErrorCode(1222)
	ERDupArgument                  = ErrorCode(1225)
	ERLocalVariable                = ErrorCode(1228)
	ERGlobalVariable               = ErrorCode(1229)
	ERWrongValueForVar             = ErrorCode(1231)
	ERWrongTypeForVar              = ErrorCode(1232)
	ERVarCantBeRead                = ErrorCode(1233)
	ERCantUseOptionHere            = ErrorCode(1234)
	ERIncorrectGlobalLocalVar      = ErrorCode(1238)
	ERWrongFKDef                   = ErrorCode(1239)
	ERKeyRefDoNotMatchTableRef     = ErrorCode(1240)
	ERCyclicReference              = ErrorCode(1245)
	ERIllegalReference             = ErrorCode(1247)
	ERDerivedMustHaveAlias         = ErrorCode(1248)
	ERTableNameNotAllowedHere      = ErrorCode(1250)
	ERCollationCharsetMismatch     = ErrorCode(1253)
	ERWarnDataTruncated            = ErrorCode(1265)
	ERCantAggregate2Collations     = ErrorCode(1267)
	ERCantAggregate3Collations     = ErrorCode(1270)
	ERCantAggregateNCollations     = ErrorCode(1271)
	ERVariableIsNotStruct          = ErrorCode(1272)
	ERUnknownCollation             = ErrorCode(1273)
	ERWrongNameForIndex            = ErrorCode(1280)
	ERWrongNameForCatalog          = ErrorCode(1281)
	ERBadFTColumn                  = ErrorCode(1283)
	ERTruncatedWrongValue          = ErrorCode(1292)
	ERTooMuchAutoTimestampCols     = ErrorCode(1293)
	ERInvalidOnUpdate              = ErrorCode(1294)
	ERUnknownTimeZone              = ErrorCode(1298)
	ERInvalidCharacterString       = ErrorCode(1300)
	ERQueryInterrupted             = ErrorCode(1317)
	ERTruncatedWrongValueForField  = ErrorCode(1366)
	ERIllegalValueForType          = ErrorCode(1367)
	ERDataTooLong                  = ErrorCode(1406)
	ErrWrongValueForType           = ErrorCode(1411)
	ERNoSuchUser                   = ErrorCode(1449)
	ERForbidSchemaChange           = ErrorCode(1450)
	ERWrongValue                   = ErrorCode(1525)
	ERDataOutOfRange               = ErrorCode(1690)
	ERInvalidJSONText              = ErrorCode(3140)
	ERInvalidJSONTextInParams      = ErrorCode(3141)
	ERInvalidJSONBinaryData        = ErrorCode(3142)
	ERInvalidJSONCharset           = ErrorCode(3144)
	ERInvalidCastToJSON            = ErrorCode(3147)
	ERJSONValueTooBig              = ErrorCode(3150)
	ERJSONDocumentTooDeep          = ErrorCode(3157)

	// max execution time exceeded
	ERQueryTimeout = ErrorCode(3024)

	ErrCantCreateGeometryObject      = ErrorCode(1416)
	ErrGISDataWrongEndianess         = ErrorCode(3055)
	ErrNotImplementedForCartesianSRS = ErrorCode(3704)
	ErrNotImplementedForProjectedSRS = ErrorCode(3705)
	ErrNonPositiveRadius             = ErrorCode(3706)

	// server not available
	ERServerIsntAvailable = ErrorCode(3168)
)

Error codes for server-side errors. Originally found in include/mysql/mysqld_error.h and https://dev.mysql.com/doc/mysql-errors/en/server-error-reference.html The below are in sorted order by value, grouped by vterror code they should be bucketed into. See above reference for more information on each code.

View Source
const (
	// SSUnknownSqlstate is ER_SIGNAL_EXCEPTION in
	// include/mysql/sql_state.h, but:
	// const char *unknown_sqlstate= "HY000"
	// in client.c. So using that one.
	SSUnknownSQLState = "HY000"

	// SSNetError is network related error
	SSNetError = "08S01"

	// SSWrongNumberOfColumns is related to columns error
	SSWrongNumberOfColumns = "21000"

	// SSWrongValueCountOnRow is related to columns count mismatch error
	SSWrongValueCountOnRow = "21S01"

	// SSDataTooLong is ER_DATA_TOO_LONG
	SSDataTooLong = "22001"

	// SSDataOutOfRange is ER_DATA_OUT_OF_RANGE
	SSDataOutOfRange = "22003"

	// SSConstraintViolation is constraint violation
	SSConstraintViolation = "23000"

	// SSCantDoThisDuringAnTransaction is
	// ER_CANT_DO_THIS_DURING_AN_TRANSACTION
	SSCantDoThisDuringAnTransaction = "25000"

	// SSAccessDeniedError is ER_ACCESS_DENIED_ERROR
	SSAccessDeniedError = "28000"

	// SSNoDB is ER_NO_DB_ERROR
	SSNoDB = "3D000"

	// SSLockDeadlock is ER_LOCK_DEADLOCK
	SSLockDeadlock = "40001"

	// SSClientError is the state on client errors
	SSClientError = "42000"

	// SSDupFieldName is ER_DUP_FIELD_NAME
	SSDupFieldName = "42S21"

	// SSBadFieldError is ER_BAD_FIELD_ERROR
	SSBadFieldError = "42S22"

	// SSUnknownTable is ER_UNKNOWN_TABLE
	SSUnknownTable = "42S02"

	// SSQueryInterrupted is ER_QUERY_INTERRUPTED;
	SSQueryInterrupted = "70100"
)

Sql states for errors. Originally found in include/mysql/sql_state.h

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'"

	// DetectSchemaChange query detects if there is any schema change from previous copy.
	DetectSchemaChange = `` /* 524-byte string literal not displayed */

	// DetectSchemaChangeOnlyBaseTable query detects if there is any schema change from previous copy excluding view tables.
	DetectSchemaChangeOnlyBaseTable = `` /* 655-byte string literal not displayed */

	// ClearSchemaCopy query clears the schemacopy table.
	ClearSchemaCopy = `delete from %s.schemacopy where table_schema = database()`

	// InsertIntoSchemaCopy query copies over the schema information from information_schema.columns table.
	InsertIntoSchemaCopy = `` /* 206-byte string literal not displayed */

	// FetchUpdatedTables queries fetches all information about updated tables
	FetchUpdatedTables = `select  ` + fetchColumns + `
from %s.schemacopy
where table_schema = database() and
	table_name in ::tableNames
order by table_name, ordinal_position`

	// FetchTables queries fetches all information about tables
	FetchTables = `select ` + fetchColumns + `
from %s.schemacopy
where table_schema = database()
order by table_name, ordinal_position`

	// GetColumnNamesQueryPatternForTable is used for mocking queries in unit tests
	GetColumnNamesQueryPatternForTable = `SELECT COLUMN_NAME.*TABLE_NAME.*%s.*`
)

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 (
	// 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 FilePosFlavorID = "FilePos"

FilePosFlavorID is the string identifier for the filePos flavor.

View Source
const (
	FlagLogEventArtificial = 0x20
)
View Source
const GRFlavorID = "MysqlGR"

GRFlavorID is the string identifier for the MysqlGR flavor.

View Source
const MariadbFlavorID = "MariaDB"

MariadbFlavorID is the string identifier for the MariaDB flavor.

View Source
const (
	// MaximumPositionSize is the maximum size of a replication position.
	// It is used as the maximum column size in the reparent_journal table
	// and other related tables. A row has a maximum size of 65535 bytes.
	// So we want to stay under that. We use VARBINARY so the character
	// set doesn't matter, we only store ascii characters anyway.
	MaximumPositionSize = 64000
)
View Source
const Mysql56FlavorID = "MySQL56"

Mysql56FlavorID is the string identifier for the Mysql56 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 = `` /* 856-byte string literal not displayed */

TablesWithSize80 is a query to select table along with size for mysql 8.0

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_tablespaces` per query. Note the following:

  • We use UNION ALL to deal differently with partitioned tables vs. non-partitioned tables. Originally, the query handled both, but that introduced "WHERE ... OR" conditions that led to poor query optimization. By separating to UNION ALL we remove all "OR" conditions.
  • We utilize `INFORMATION_SCHEMA`.`TABLES`.`CREATE_OPTIONS` column to do early pruning before the JOIN.
  • `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)
  • `create_options` is NULL for views, and therefore we need an additional UNION ALL to include views

Variables

View Source
var (
	// ErrNotReplica means there is no replication status.
	// Returned by ShowReplicationStatus().
	ErrNotReplica = NewSQLError(ERNotReplica, 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),
}, {
	Name:         "i.file_size",
	Type:         querypb.Type_INT64,
	ColumnLength: 11,
	Charset:      collations.CollationBinaryID,
	Flags:        uint32(querypb.MySqlFlag_BINARY_FLAG | querypb.MySqlFlag_NUM_FLAG),
}, {
	Name:         "i.allocated_size",
	Type:         querypb.Type_INT64,
	ColumnLength: 11,
	Charset:      collations.CollationBinaryID,
	Flags:        uint32(querypb.MySqlFlag_BINARY_FLAG | querypb.MySqlFlag_NUM_FLAG),
}}

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

View Source
var (
	// BinglogMagicNumber is 4-byte number at the beginning of every binary log
	BinglogMagicNumber = []byte{0xfe, 0x62, 0x69, 0x6e}
)
View Source
var CharacterSetEncoding = map[string]encoding.Encoding{
	"cp850":   charmap.CodePage850,
	"koi8r":   charmap.KOI8R,
	"latin1":  charmap.Windows1252,
	"latin2":  charmap.ISO8859_2,
	"ascii":   nil,
	"hebrew":  charmap.ISO8859_8,
	"greek":   charmap.ISO8859_7,
	"cp1250":  charmap.Windows1250,
	"gbk":     simplifiedchinese.GBK,
	"latin5":  charmap.ISO8859_9,
	"utf8":    nil,
	"utf8mb3": nil,
	"cp866":   charmap.CodePage866,
	"cp852":   charmap.CodePage852,
	"latin7":  charmap.ISO8859_13,
	"utf8mb4": nil,
	"cp1251":  charmap.Windows1251,
	"cp1256":  charmap.Windows1256,
	"cp1257":  charmap.Windows1257,
	"binary":  nil,
}

CharacterSetEncoding maps a charset name to a golang encoder. golang does not support encoders for all MySQL charsets. A charset not in this map is unsupported. A trivial encoding (e.g. utf8) has a `nil` encoder

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.

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

Functions

func AllPositionsComparable

func AllPositionsComparable(positions []Position) bool

AllPositionsComparable returns true if all positions in the supplied list are comparable with one another, and false if any are non-comparable.

func BaseShowTablesRow

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

BaseShowTablesRow returns the fields from a BaseShowTables or BaseShowTablesForTable command.

func ConvertStateToMySQLErrorCode added in v0.16.0

func ConvertStateToMySQLErrorCode(state vterrors.State) string

ConvertStateToMySQLErrorCode returns MySQL error code for the given vterrors.State If the state is == 0, an empty string is returned

func ConvertStateToMySQLState added in v0.16.0

func ConvertStateToMySQLState(state vterrors.State) string

ConvertStateToMySQLState returns MySQL state for the given vterrors.State If the state is == 0, an empty string is returned

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 EncodeGTID

func EncodeGTID(gtid GTID) string

EncodeGTID returns a string that contains both the flavor and value of the GTID, so that the correct parser can be selected when that string is passed to DecodeGTID.

func EncodePosition

func EncodePosition(rp Position) string

EncodePosition returns a string that contains both the flavor and value of the Position, so that the correct parser can be selected when that string is passed to DecodePosition.

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 InitAuthServerClientCert

func InitAuthServerClientCert()

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

func InitAuthServerStatic

func InitAuthServerStatic()

InitAuthServerStatic Handles initializing the AuthServerStatic if necessary.

func IsConnErr

func IsConnErr(err error) bool

IsConnErr returns true if the error is a connection error.

func IsConnLostDuringQuery added in v0.13.0

func IsConnLostDuringQuery(err error) bool

IsConnLostDuringQuery returns true if the error is a CRServerLost error. Happens most commonly when a query is killed MySQL server-side.

func IsEphemeralError added in v0.15.0

func IsEphemeralError(err error) bool

IsEphemeralError returns true if the error is ephemeral and the caller should retry if possible. Note: non-SQL errors are always treated as ephemeral.

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 IsSchemaApplyError added in v0.8.0

func IsSchemaApplyError(err error) bool

IsSchemaApplyError returns true when given error is a MySQL error applying schema change

func IsTooManyConnectionsErr added in v0.9.0

func IsTooManyConnectionsErr(err error) bool

IsTooManyConnectionsErr returns true if the error is due to too many connections.

func MatchSourceHost added in v0.12.0

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

MatchSourceHost validates host entry in auth configuration

func NewSQLErrorFromError

func NewSQLErrorFromError(err error) error

NewSQLErrorFromError returns a *SQLError from the provided error. If it's not the right type, it still tries to get it from a regexp. Notes about the `error` return type: The function really returns *SQLError or `nil`. Seemingly, the function could just return `*SQLError` type. However, it really must return `error`. The reason is the way `golang` treats `nil` interfaces vs `nil` implementing values. If this function were to return a nil `*SQLError`, the following undesired behavior would happen:

var err error
err = NewSQLErrorFromError(nil) // returns a nil `*SQLError`
if err != nil {
  doSomething() // this actually runs
}

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 PrimaryStatusToProto added in v0.11.0

func PrimaryStatusToProto(s PrimaryStatus) *replicationdatapb.PrimaryStatus

PrimaryStatusToProto translates a PrimaryStatus to proto3.

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 ReplicationStatusToProto

func ReplicationStatusToProto(s ReplicationStatus) *replicationdatapb.Status

ReplicationStatusToProto translates a Status to proto3.

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 ServerVersionAtLeast added in v0.14.0

func ServerVersionAtLeast(serverVersion string, parts ...int) (bool, error)

ServerVersionAtLeast returns true if current server is at least given value. Example: if input is []int{8, 0, 23}... the function returns 'true' if we're on MySQL 8.0.23, 8.0.24, ...

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 Subtract added in v0.15.0

func Subtract(lhs, rhs string) (string, error)

Subtract takes in two Mysql56GTIDSets as strings and subtracts the second from the first The result is also a string. An error is thrown if parsing is not possible for either GTIDSets

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

	// 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) (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) (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 list of BinlogEvents contained
	// within the compressed transaction.
	TransactionPayload(BinlogFormat) ([]BinlogEvent, 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 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 5.6.33-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 CapableOf added in v0.14.0

type CapableOf func(capability FlavorCapability) (bool, error)

func GetFlavor added in v0.14.0

func GetFlavor(serverVersion string, flavorFunc func() flavor) (f flavor, capableOf 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).

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 (*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) 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) 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) 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() (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) 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) IsUnixSocket added in v0.12.0

func (c *Conn) IsUnixSocket() bool

IsUnixSocket returns true if this connection is over a Unix socket.

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() (Position, error)

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

func (*Conn) PrimaryPosition added in v0.11.0

func (c *Conn) PrimaryPosition() (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) 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() bool

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 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 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, 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() (PrimaryStatus, error)

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

func (*Conn) ShowReplicationStatus

func (c *Conn) ShowReplicationStatus() (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 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 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 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) WaitUntilFilePositionCommand

func (c *Conn) WaitUntilFilePositionCommand(ctx context.Context, pos Position) (string, error)

WaitUntilFilePositionCommand returns the SQL command to issue to wait until the given position, until the context expires for the file position flavor. The command returns -1 if it times out. It returns NULL if GTIDs are not enabled.

func (*Conn) WaitUntilPositionCommand

func (c *Conn) WaitUntilPositionCommand(ctx context.Context, pos Position) (string, error)

WaitUntilPositionCommand returns the SQL command to issue to wait until the given position, until the context expires. The command returns -1 if it times out. It returns NULL if GTIDs are not enabled.

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 `json:"host"`
	Port       int    `json:"port"`
	Uname      string `json:"uname"`
	Pass       string `json:"pass"`
	DbName     string `json:"dbname"`
	UnixSocket string `json:"unix_socket"`
	Charset    string `json:"charset"`
	Flags      uint64 `json:"flags"`
	Flavor     string `json:"flavor,omitempty"`

	// 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 `json:"ssl_mode"`
	SslCa            string        `json:"ssl_ca"`
	SslCaPath        string        `json:"ssl_ca_path"`
	SslCert          string        `json:"ssl_cert"`
	SslCrl           string        `json:"ssl_crl"`
	SslKey           string        `json:"ssl_key"`
	TLSMinVersion    string        `json:"tls_min_version"`
	ServerName       string        `json:"server_name"`
	ConnectTimeoutMs uint64        `json:"connect_timeout_ms"`

	// 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
}

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 ErrorCode added in v0.17.0

type ErrorCode uint16

func (ErrorCode) ToString added in v0.17.0

func (e ErrorCode) ToString() string

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 FlavorCapability added in v0.14.0

type FlavorCapability int
const (
	NoneFlavorCapability          FlavorCapability = iota // default placeholder
	FastDropTableFlavorCapability                         // supported in MySQL 8.0.23 and above: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-23.html
	TransactionalGtidExecutedFlavorCapability
	InstantDDLFlavorCapability
	InstantAddLastColumnFlavorCapability
	InstantAddDropVirtualColumnFlavorCapability
	InstantAddDropColumnFlavorCapability
	InstantChangeColumnDefaultFlavorCapability
	InstantExpandEnumCapability
	MySQLJSONFlavorCapability
	MySQLUpgradeInServerFlavorCapability
	DynamicRedoLogCapacityFlavorCapability // supported in MySQL 8.0.30 and above: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-30.html
	DisableRedoLogFlavorCapability         // supported in MySQL 8.0.21 and above: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-21.html
)

type GTID

type GTID interface {
	// String returns the canonical printed form of the GTID as expected by a
	// particular flavor of MySQL.
	String() string

	// Flavor returns the key under which the corresponding GTID parser function
	// is registered in the gtidParsers map.
	Flavor() string

	// SourceServer returns the ID of the server that generated the transaction.
	SourceServer() any

	// SequenceNumber returns the ID number that increases with each transaction.
	// It is only valid to compare the sequence numbers of two GTIDs if they have
	// the same domain value.
	SequenceNumber() any

	// SequenceDomain returns the ID of the domain within which two sequence
	// numbers can be meaningfully compared.
	SequenceDomain() any

	// GTIDSet returns a GTIDSet of the same flavor as this GTID, containing only
	// this GTID.
	GTIDSet() GTIDSet
}

GTID represents a Global Transaction ID, also known as Transaction Group ID. Each flavor of MySQL has its own format for the GTID. This interface is used along with various MysqlFlavor implementations to abstract the differences.

Types that implement GTID should use a non-pointer receiver. This ensures that comparing GTID interface values with == has the expected semantics.

func DecodeGTID

func DecodeGTID(s string) (GTID, error)

DecodeGTID converts a string in the format returned by EncodeGTID back into a GTID interface value with the correct underlying flavor.

func MustDecodeGTID

func MustDecodeGTID(s string) GTID

MustDecodeGTID calls DecodeGTID and panics on error.

func MustParseGTID

func MustParseGTID(flavor, value string) GTID

MustParseGTID calls ParseGTID and panics on error.

func ParseGTID

func ParseGTID(flavor, value string) (GTID, error)

ParseGTID calls the GTID parser for the specified flavor.

type GTIDSet

type GTIDSet interface {
	// String returns the canonical printed form of the set as expected by a
	// particular flavor of MySQL.
	String() string

	// Flavor returns the key under which the corresponding parser function is
	// registered in the transactionSetParsers map.
	Flavor() string

	// ContainsGTID returns true if the set contains the specified transaction.
	ContainsGTID(GTID) bool

	// Contains returns true if the set is a superset of another set. All implementations should return false if
	// other GTIDSet is not the right concrete type for that flavor.
	Contains(GTIDSet) bool

	// Equal returns true if the set is equal to another set.
	Equal(GTIDSet) bool

	// AddGTID returns a new GTIDSet that is expanded to contain the given GTID.
	AddGTID(GTID) GTIDSet

	// Union returns a union of the receiver GTIDSet and the supplied GTIDSet.
	Union(GTIDSet) GTIDSet

	// Union returns a union of the receiver GTIDSet and the supplied GTIDSet.
	Last() string
}

GTIDSet represents the set of transactions received or applied by a server. In some flavors, a single GTID is enough to specify the set of all transactions that came before it, but in others a more complex structure is required.

GTIDSet is wrapped by replication.Position, which is a concrete struct. When sending a GTIDSet over RPCs, encode/decode it as a string. Most code outside of this package should use replication.Position rather than GTIDSet.

func ParseFilePosGTIDSet added in v0.8.0

func ParseFilePosGTIDSet(s string) (GTIDSet, error)

ParseFilePosGTIDSet is registered as a GTIDSet parser.

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 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)
}

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,
) (*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,
) (*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
}

ListenerConfig should be used with NewListenerWithConfig to specify listener parameters.

type MariadbGTID

type MariadbGTID struct {
	// Domain is the ID number of the domain within which sequence numbers apply.
	Domain uint32
	// Server is the ID of the server that generated the transaction.
	Server uint32
	// Sequence is the sequence number of the transaction within the domain.
	Sequence uint64
}

MariadbGTID implements GTID.

func (MariadbGTID) Flavor

func (gtid MariadbGTID) Flavor() string

Flavor implements GTID.Flavor().

func (MariadbGTID) GTIDSet

func (gtid MariadbGTID) GTIDSet() GTIDSet

GTIDSet implements GTID.GTIDSet().

func (MariadbGTID) SequenceDomain

func (gtid MariadbGTID) SequenceDomain() any

SequenceDomain implements GTID.SequenceDomain().

func (MariadbGTID) SequenceNumber

func (gtid MariadbGTID) SequenceNumber() any

SequenceNumber implements GTID.SequenceNumber().

func (MariadbGTID) SourceServer

func (gtid MariadbGTID) SourceServer() any

SourceServer implements GTID.SourceServer().

func (MariadbGTID) String

func (gtid MariadbGTID) String() string

String implements GTID.String().

type MariadbGTIDSet

type MariadbGTIDSet map[uint32]MariadbGTID

MariadbGTIDSet implements GTIDSet.

func (MariadbGTIDSet) AddGTID

func (gtidSet MariadbGTIDSet) AddGTID(other GTID) GTIDSet

AddGTID implements GTIDSet.AddGTID().

func (MariadbGTIDSet) Contains

func (gtidSet MariadbGTIDSet) Contains(other GTIDSet) bool

Contains implements GTIDSet.Contains().

func (MariadbGTIDSet) ContainsGTID

func (gtidSet MariadbGTIDSet) ContainsGTID(other GTID) bool

ContainsGTID implements GTIDSet.ContainsGTID().

func (MariadbGTIDSet) Equal

func (gtidSet MariadbGTIDSet) Equal(other GTIDSet) bool

Equal implements GTIDSet.Equal().

func (MariadbGTIDSet) Flavor

func (gtidSet MariadbGTIDSet) Flavor() string

Flavor implements GTIDSet.Flavor()

func (MariadbGTIDSet) Last

func (gtidSet MariadbGTIDSet) Last() string

Last returns the last gtid

func (MariadbGTIDSet) String

func (gtidSet MariadbGTIDSet) String() string

String implements GTIDSet.String()

func (MariadbGTIDSet) Union

func (gtidSet MariadbGTIDSet) Union(other GTIDSet) GTIDSet

Union implements GTIDSet.Union(). This is a pure method, and does not mutate the receiver.

type Mysql56GTID

type Mysql56GTID struct {
	// Server is the SID of the server that originally committed the transaction.
	Server SID
	// Sequence is the sequence number of the transaction within a given Server's
	// scope.
	Sequence int64
}

Mysql56GTID implements GTID

func (Mysql56GTID) Flavor

func (gtid Mysql56GTID) Flavor() string

Flavor implements GTID.Flavor().

func (Mysql56GTID) GTIDSet

func (gtid Mysql56GTID) GTIDSet() GTIDSet

GTIDSet implements GTID.GTIDSet().

func (Mysql56GTID) SequenceDomain

func (gtid Mysql56GTID) SequenceDomain() any

SequenceDomain implements GTID.SequenceDomain().

func (Mysql56GTID) SequenceNumber

func (gtid Mysql56GTID) SequenceNumber() any

SequenceNumber implements GTID.SequenceNumber().

func (Mysql56GTID) SourceServer

func (gtid Mysql56GTID) SourceServer() any

SourceServer implements GTID.SourceServer().

func (Mysql56GTID) String

func (gtid Mysql56GTID) String() string

String implements GTID.String().

type Mysql56GTIDSet

type Mysql56GTIDSet map[SID][]interval

Mysql56GTIDSet implements GTIDSet for MySQL 5.6.

func NewMysql56GTIDSetFromSIDBlock

func NewMysql56GTIDSetFromSIDBlock(data []byte) (Mysql56GTIDSet, error)

NewMysql56GTIDSetFromSIDBlock builds a Mysql56GTIDSet from parsing a SID Block. This is the reverse of the SIDBlock method.

Expected format:

# bytes field
8       nSIDs

(nSIDs times)

16      SID
8       nIntervals

(nIntervals times)

8       start
8       end

func ParseMysql56GTIDSet added in v0.16.0

func ParseMysql56GTIDSet(s string) (Mysql56GTIDSet, error)

ParseMysql56GTIDSet is registered as a GTIDSet parser.

https://dev.mysql.com/doc/refman/5.6/en/replication-gtids-concepts.html

func (Mysql56GTIDSet) AddGTID

func (set Mysql56GTIDSet) AddGTID(gtid GTID) GTIDSet

AddGTID implements GTIDSet.

func (Mysql56GTIDSet) Contains

func (set Mysql56GTIDSet) Contains(other GTIDSet) bool

Contains implements GTIDSet.

func (Mysql56GTIDSet) ContainsGTID

func (set Mysql56GTIDSet) ContainsGTID(gtid GTID) bool

ContainsGTID implements GTIDSet.

func (Mysql56GTIDSet) Difference

func (set Mysql56GTIDSet) Difference(other Mysql56GTIDSet) Mysql56GTIDSet

Difference will supply the difference between the receiver and supplied Mysql56GTIDSets, and supply the result as a Mysql56GTIDSet.

func (Mysql56GTIDSet) Equal

func (set Mysql56GTIDSet) Equal(other GTIDSet) bool

Equal implements GTIDSet.

func (Mysql56GTIDSet) Flavor

func (Mysql56GTIDSet) Flavor() string

Flavor implements GTIDSet.

func (Mysql56GTIDSet) Last

func (set Mysql56GTIDSet) Last() string

Last returns the last gtid as string For gtidset having multiple SIDs or multiple intervals it just returns the last SID with last interval

func (Mysql56GTIDSet) SIDBlock

func (set Mysql56GTIDSet) SIDBlock() []byte

SIDBlock returns the binary encoding of a MySQL 5.6 GTID set as expected by internal commands that refer to an "SID block".

e.g. https://dev.mysql.com/doc/internals/en/com-binlog-dump-gtid.html

func (Mysql56GTIDSet) SIDs

func (set Mysql56GTIDSet) SIDs() []SID

SIDs returns a sorted list of SIDs in the set.

func (Mysql56GTIDSet) String

func (set Mysql56GTIDSet) String() string

String implements GTIDSet.

func (Mysql56GTIDSet) Union

func (set Mysql56GTIDSet) Union(other GTIDSet) GTIDSet

Union implements GTIDSet.Union().

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 Position

type Position struct {

	// GTIDSet is the underlying GTID set. It must not be anonymous,
	// or else Position would itself also implement the GTIDSet interface.
	GTIDSet GTIDSet
	// contains filtered or unexported fields
}

Position represents the information necessary to describe which transactions a server has seen, so that it can request a replication stream from a new source that picks up where it left off.

This must be a concrete struct because custom Unmarshalers can't be registered on an interface.

The == operator should not be used with Position, because the underlying GTIDSet might use slices, which are not comparable. Using == in those cases will result in a run-time panic.

func AppendGTID

func AppendGTID(rp Position, gtid GTID) Position

AppendGTID returns a new Position that represents the position after the given GTID is replicated.

func DecodePosition

func DecodePosition(s string) (rp Position, err error)

DecodePosition converts a string in the format returned by EncodePosition back into a Position value with the correct underlying flavor.

func MustParsePosition

func MustParsePosition(flavor, value string) Position

MustParsePosition calls ParsePosition and panics on error.

func ParsePosition

func ParsePosition(flavor, value string) (rp Position, err error)

ParsePosition calls the parser for the specified flavor.

func (Position) AtLeast

func (rp Position) AtLeast(other Position) bool

AtLeast returns true if this position is equal to or after another.

func (*Position) Comparable

func (rp *Position) Comparable(other Position) bool

Comparable returns whether the receiver is comparable to the supplied position, based on whether one of the two positions contains the other.

func (Position) Equal

func (rp Position) Equal(other Position) bool

Equal returns true if this position is equal to another.

func (Position) IsZero

func (rp Position) IsZero() bool

IsZero returns true if this is the zero value, Position{}.

func (Position) MarshalJSON

func (rp Position) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler.

func (*Position) MatchesFlavor

func (rp *Position) MatchesFlavor(flavor string) bool

MatchesFlavor will take a flavor string, and return whether the positions GTIDSet matches the supplied flavor. The caller should use the constants Mysql56FlavorID, MariadbFlavorID, or FilePosFlavorID when supplying the flavor string.

func (Position) String

func (rp Position) String() string

String returns a string representation of the underlying GTIDSet. If the set is nil, it returns "<nil>" in the style of Sprintf("%v", nil).

func (*Position) UnmarshalJSON

func (rp *Position) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler.

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 PrimaryStatus added in v0.11.0

type PrimaryStatus struct {
	// Position represents the server's GTID based position.
	Position Position
	// FilePosition represents the server's file based position.
	FilePosition Position
}

PrimaryStatus holds replication information from SHOW MASTER STATUS.

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 ReplicationState added in v0.14.0

type ReplicationState int32
const (
	ReplicationStateUnknown ReplicationState = iota
	ReplicationStateStopped
	ReplicationStateConnecting
	ReplicationStateRunning
)

func ReplicationStatusToState added in v0.14.0

func ReplicationStatusToState(s string) ReplicationState

ReplicationStatusToState converts a value you have for the IO thread(s) or SQL thread(s) or Group Replication applier thread(s) from MySQL or intermediate layers to a mysql.ReplicationState. on,yes,true == ReplicationStateRunning off,no,false == ReplicationStateStopped connecting == ReplicationStateConnecting anything else == ReplicationStateUnknown

type ReplicationStatus

type ReplicationStatus struct {
	// Position is the current position of the replica. For GTID replication implementations
	// it is the executed GTID set. For file replication implementation, it is same as
	// FilePosition
	Position Position
	// RelayLogPosition is the Position that the replica would be at if it
	// were to finish executing everything that's currently in its relay log.
	// However, some MySQL flavors don't expose this information,
	// in which case RelayLogPosition.IsZero() will be true.
	// If ReplicationLagUnknown is true then we should not rely on the seconds
	// behind value and we can instead try to calculate the lag ourselves when
	// appropriate. For MySQL GTID replication implementation it is the union of
	// executed GTID set and retrieved GTID set. For file replication implementation,
	// it is same as RelayLogSourceBinlogEquivalentPosition
	RelayLogPosition Position
	// FilePosition stores the position of the source tablets binary log
	// upto which the SQL thread of the replica has run.
	FilePosition Position
	// RelayLogSourceBinlogEquivalentPosition stores the position of the source tablets binary log
	// upto which the IO thread has read and added to the relay log
	RelayLogSourceBinlogEquivalentPosition Position
	// RelayLogFilePosition stores the position in the relay log file
	RelayLogFilePosition  Position
	SourceServerID        uint32
	IOState               ReplicationState
	LastIOError           string
	SQLState              ReplicationState
	LastSQLError          string
	ReplicationLagSeconds uint32
	ReplicationLagUnknown bool
	SourceHost            string
	SourcePort            int32
	SourceUser            string
	ConnectRetry          int32
	SourceUUID            SID
	SQLDelay              uint32
	AutoPosition          bool
	UsingGTID             bool
	HasReplicationFilters bool
	SSLAllowed            bool
}

ReplicationStatus holds replication information from SHOW SLAVE STATUS.

func ProtoToReplicationStatus

func ProtoToReplicationStatus(s *replicationdatapb.Status) ReplicationStatus

ProtoToReplicationStatus translates a proto Status, or panics.

func (*ReplicationStatus) FindErrantGTIDs

func (s *ReplicationStatus) FindErrantGTIDs(otherReplicaStatuses []*ReplicationStatus) (Mysql56GTIDSet, error)

FindErrantGTIDs can be used to find errant GTIDs in the receiver's relay log, by comparing it against all known replicas, provided as a list of ReplicationStatus's. This method only works if the flavor for all retrieved ReplicationStatus's is MySQL. The result is returned as a Mysql56GTIDSet, each of whose elements is a found errant GTID. This function is best effort in nature. If it marks something as errant, then it is for sure errant. But there may be cases of errant GTIDs, which aren't caught by this function.

func (*ReplicationStatus) Healthy added in v0.14.0

func (s *ReplicationStatus) Healthy() bool

Healthy returns true if both the SQL IO components are healthy

func (*ReplicationStatus) IOHealthy added in v0.14.0

func (s *ReplicationStatus) IOHealthy() bool

IOHealthy returns true if the IO thread is running OR, the IO thread is connecting AND there's no IO error from the last attempt to connect to the source.

func (*ReplicationStatus) Running added in v0.14.0

func (s *ReplicationStatus) Running() bool

Running returns true if both the IO and SQL threads are running.

func (*ReplicationStatus) SQLHealthy added in v0.14.0

func (s *ReplicationStatus) SQLHealthy() bool

SQLHealthy returns true if the SQLState is running. For consistency and to support altering this calculation in the future.

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 SID

type SID [16]byte

SID is the 16-byte unique ID of a MySQL 5.6 server.

func ParseSID

func ParseSID(s string) (sid SID, err error)

ParseSID parses an SID in the form used by MySQL 5.6.

func (SID) String

func (sid SID) String() string

String prints an SID in the form used by MySQL 5.6.

type SQLError

type SQLError struct {
	Num     ErrorCode
	State   string
	Message string
	Query   string
}

SQLError is the error structure returned from calling a db library function

func NewSQLError

func NewSQLError(number ErrorCode, sqlState string, format string, args ...any) *SQLError

NewSQLError creates a new SQLError. If sqlState is left empty, it will default to "HY000" (general error). TODO: Should be aligned with vterrors, stack traces and wrapping

func (*SQLError) Error

func (se *SQLError) Error() string

Error implements the error interface

func (*SQLError) Number

func (se *SQLError) Number() ErrorCode

Number returns the internal MySQL error code.

func (*SQLError) SQLState

func (se *SQLError) SQLState() string

SQLState returns the SQLSTATE value.

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
}

TableMap contains data from a TABLE_MAP_EVENT.

type TransactionPayload added in v0.17.0

type TransactionPayload struct {
	Size             uint64
	CompressionType  uint64
	UncompressedSize uint64
	Payload          []byte
	Events           []BinlogEvent
}

func (*TransactionPayload) Decode added in v0.17.0

func (tp *TransactionPayload) Decode(data []byte) error

Decode decodes and decompresses 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.
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