MySQL#

Integration with MySQL

Installation#

The fixture provides a running MySQL service and validates availability with the container’s bundled tools. Use the service attributes with the MySQL client, ORM, or application configuration you normally use.

Usage Example#

import mysql.connector
from pytest_databases.docker.mysql import MySQLService

pytest_plugins = ["pytest_databases.docker.mysql"]

def test(mysql_service: MySQLService) -> None:
    with mysql.connector.connect(
        host=mysql_service.host,
        port=mysql_service.port,
        user=mysql_service.user,
        database=mysql_service.db,
        password=mysql_service.password,
    ) as conn, conn.cursor() as cursor:
        cursor.execute("select 1 as is_available")
        resp = cursor.fetchone()
        assert resp is not None and resp[0] == 1
    exit_code, output = mysql_service.exec_sql("SELECT DATABASE()")
    assert exit_code == 0
    assert output.strip().decode() == mysql_service.db

MySQL containers start with --performance-schema=ON by default and grant global privileges to mysql_user (or use root directly when mysql_user is "root"). Under pytest-xdist with the default "database" isolation level, workers share one container per version and connect to <mysql_database>_<worker>. Custom fixtures calling provide_mysql_service() can pass extra mysqld command flags, env variables, host_port, and one-shot init_sql statements or .sql files.

Available Fixtures#

  • mysql_service: A fixture that provides a MySQL service (8.4 LTS).

  • mysql_user: The application user configured in the container (default: "app", env MYSQL_USER).

  • mysql_password: The application user password configured in the container (default: "super-secret", env MYSQL_PASSWORD).

  • mysql_root_password: The root password configured in the container (default: "super-secret", env MYSQL_ROOT_PASSWORD).

  • mysql_database: The base database configured in the container (default: "db", env MYSQL_DATABASE; <mysql_database>_<worker> under pytest-xdist "database" isolation).

  • mysql_port: Optional pinned host port for MySQL containers (default: None, env MYSQL_PORT).

  • mysql_platform: Container platform for MySQL 5.6 and 5.7 (default: "linux/x86_64").

  • xdist_mysql_isolation_level: Worker isolation mode: "database" (default) or "server".

The following version-specific fixtures are also available:

  • mysql_56_service: MySQL 5.6

  • mysql_57_service: MySQL 5.7

  • mysql_8_service: MySQL 8.0

  • mysql_84_service: MySQL 8.4 LTS

  • mysql_96_service: MySQL 9.6 Innovation

  • mysql_97_service: MySQL 9.7 Innovation

Service API#

MySQL container service fixtures.

class pytest_databases.docker.mysql.MySQLService(container: docker.models.containers.Container, host: str, port: int, db: str, user: str, password: str)[source]#

Bases: ServiceContainer

Service handle for a running MySQL container.

db: str#
user: str#
password: str#
exec_sql(sql: str, *, database: str | None = None, user: str | None = None, password: str | None = None, batch: bool = True) → tuple[int, bytes][source]#

Execute sql via the in-container mysql client over TCP loopback.

Returns:

Tuple of (exit_code, output_bytes).

__init__(container: docker.models.containers.Container, host: str, port: int, db: str, user: str, password: str) → None#
pytest_databases.docker.mysql.exec_mysql(container: docker.models.containers.Container, sql: str, *, user: str = 'root', password: str = '', database: str | None = None, batch: bool = True) → tuple[int, bytes][source]#

Execute sql inside a MySQL container using MYSQL_PWD authentication.

Parameters:
  • container – Running MySQL container handle.

  • sql – SQL statement(s) passed to mysql -e.

  • user – MySQL user name.

  • password – Password passed via MYSQL_PWD environment variable.

  • database – Optional target database name.

  • batch – When True, adds --batch --skip-column-names for clean scalar/tabular parsing.

Returns:

Tuple of (exit_code, output_bytes).

pytest_databases.docker.mysql.mysql_8_service(request: pytest.FixtureRequest, docker_service: DockerService, xdist_mysql_isolation_level: XdistIsolationLevel, mysql_user: str, mysql_password: str, mysql_root_password: str, mysql_database: str, mysql_port: int | None) → Generator[MySQLService, None, None]#

Provide a running MySQL 8 container service.

pytest_databases.docker.mysql.mysql_56_service(docker_service: DockerService, xdist_mysql_isolation_level: XdistIsolationLevel, mysql_platform: str | None, mysql_user: str, mysql_password: str, mysql_root_password: str, mysql_database: str, mysql_port: int | None) → Generator[MySQLService, None, None]#

Provide a running MySQL 5.6 container service.

pytest_databases.docker.mysql.mysql_57_service(docker_service: DockerService, xdist_mysql_isolation_level: XdistIsolationLevel, mysql_platform: str | None, mysql_user: str, mysql_password: str, mysql_root_password: str, mysql_database: str, mysql_port: int | None) → Generator[MySQLService, None, None]#

Provide a running MySQL 5.7 container service.

pytest_databases.docker.mysql.mysql_84_service(request: pytest.FixtureRequest, docker_service: DockerService, xdist_mysql_isolation_level: XdistIsolationLevel, mysql_user: str, mysql_password: str, mysql_root_password: str, mysql_database: str, mysql_port: int | None) → Generator[MySQLService, None, None]#

Provide a running MySQL 8.4 LTS container service.

pytest_databases.docker.mysql.mysql_96_service(request: pytest.FixtureRequest, docker_service: DockerService, xdist_mysql_isolation_level: XdistIsolationLevel, mysql_user: str, mysql_password: str, mysql_root_password: str, mysql_database: str, mysql_port: int | None) → Generator[MySQLService, None, None]#

Provide a running MySQL 9.6 container service.

pytest_databases.docker.mysql.mysql_97_service(request: pytest.FixtureRequest, docker_service: DockerService, xdist_mysql_isolation_level: XdistIsolationLevel, mysql_user: str, mysql_password: str, mysql_root_password: str, mysql_database: str, mysql_port: int | None) → Generator[MySQLService, None, None]#

Provide a running MySQL 9.7 container service.

pytest_databases.docker.mysql.mysql_database() → str[source]#

Return the MySQL database name.

pytest_databases.docker.mysql.mysql_password() → str[source]#

Return the MySQL application password.

pytest_databases.docker.mysql.mysql_platform(request: FixtureRequest) → str | None[source]#

Return the container platform used by MySQL 5.6 and 5.7, which publish x86_64 images only.

pytest_databases.docker.mysql.mysql_port() → int | None[source]#

Return an optional pinned host port for MySQL containers from MYSQL_PORT.

pytest_databases.docker.mysql.mysql_root_password() → str[source]#

Return the MySQL root password.

pytest_databases.docker.mysql.mysql_service(mysql_84_service: MySQLService) → MySQLService[source]#

Provide the default MySQL container service.

pytest_databases.docker.mysql.mysql_user() → str[source]#

Return the MySQL application user.

pytest_databases.docker.mysql.platform() → str[source]#

Return the legacy linux/x86_64 platform string.

Deprecated since version 0.20.0: Use mysql_platform instead. platform will be removed in v1.0.0.

pytest_databases.docker.mysql.provide_mysql_service(docker_service: DockerService, image: str, name: str, isolation_level: XdistIsolationLevel, platform: str | None, user: str, password: str, root_password: str, database: str, *, host_port: int | None = None, command: str | Sequence[str] | None = None, env: Mapping[str, str] | None = None, init_sql: str | Path | Sequence[str | Path] | None = None, timeout: int = 120, pause: float = 1.0) → Generator[MySQLService, None, None][source]#

Start and manage a MySQL container service with performance_schema enabled by default.

Yields:

Active MySQLService instance bound to database (or <database>_<worker> under xdist).

Raises:
  • ValueError – If database or user is invalid.

  • RuntimeError – If database provisioning, user grants, or init_sql fails.

pytest_databases.docker.mysql.xdist_mysql_isolation_level() → XdistIsolationLevel[source]#

Return the pytest-xdist isolation level for MySQL.