Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sql.mysql.driver

import sql.mysql.driver

sql lifts part of this module out to its own top level; each name below is shown with the path that reaches it. Anything still spelled sql.mysql.driver.* needs import sql.mysql.driver.

Opening MySQL and MariaDB connections: reading a connection string, establishing the socket, upgrading it to TLS when asked, and authenticating.

Constants

DEFAULT_PORT

sql.mysql.DEFAULT_PORT = 3306

The port a MySQL server listens on unless told otherwise.

DEFAULT_TIMEOUT

sql.mysql.driver.DEFAULT_TIMEOUT = 30000

How long to wait for the socket and for each read, in milliseconds.

MAX_PARAMETERS

sql.mysql.MAX_PARAMETERS = 65535

Most parameters one statement can bind. The protocol counts them in a 16 bit field, so this is a hard limit rather than a configured one.

SSL_MODES

sql.mysql.SSL_MODES = [...]

What sslmode can be.

ModeMeaning
disableNever use TLS.
preferWhere the server offers it, unverified; in the clear where it does not.
requireInsist on TLS, without checking who the server is.
verifyInsist on TLS and check the server’s certificate and host name.

MySQL’s own spellings are accepted too. DISABLED, PREFERRED and REQUIRED mean what they say; VERIFY_CA and VERIFY_IDENTITY both become verify. That makes VERIFY_CA stricter here than on the command line, where it checks the certificate but not the name: the difference can only cause a connection to be refused, never to be wrongly trusted.

Classes

MySqlDriver

class sql.mysql.MySqlDriver < Driver

Opens MySQL connections.

MySqlDriver.name()

sql.mysql.MySqlDriver.name()

MySqlDriver.schemes()

sql.mysql.MySqlDriver.schemes()

MySqlDriver.capabilities()

sql.mysql.MySqlDriver.capabilities() -> dict

What MySQL can do.

last_insert_id is true and returning is false, which together are the most visible difference from PostgreSQL: an insert that wants its id back reads it from what the server reported rather than from a RETURNING clause, and Connection.insert() picks between the two on its own.

Returns dict

MySqlDriver.parse_dsn()

sql.mysql.MySqlDriver.parse_dsn(dsn) -> dict

Reads a connection string, in either of the two forms MySQL’s own tooling accepts.

A URI:

mysql://alice:secret@db.example.com:3306/app?sslmode=verify
mysql://localhost/app

Or keywords, as my.cnf spells them:

host=localhost port=3306 database=app user=alice password=secret

Anything absent is filled in the way the command line client fills it: the user defaults to root, the host to 127.0.0.1 and the port to 3306. No database is selected unless one is named.

Parameters

  • dsn (string|dict)

Returns dict

Raises ConnectionError if the string cannot be read.

MySqlDriver.connect()

sql.mysql.MySqlDriver.connect(options: dict) -> MySqlConnection

Opens the connection and authenticates.

Parameters

  • options (dict) — From parse_dsn().

Returns MySqlConnection

MariaDbDriver

class sql.mysql.MariaDbDriver < MySqlDriver

Opens MariaDB connections.

MariaDB speaks the same protocol, so everything about the exchange is shared. It differs in what it can be asked to do: RETURNING works there and does not on MySQL, which is the one capability that changes what the layer above generates.

A MariaDB server reached through mysql:// works, and simply reports the smaller set of capabilities.

MariaDbDriver.name()

sql.mysql.MariaDbDriver.name()

MariaDbDriver.schemes()

sql.mysql.MariaDbDriver.schemes()

MariaDbDriver.capabilities()

sql.mysql.MariaDbDriver.capabilities()

2026, Richard Ore and Zuri contributors