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", envMYSQL_USER).mysql_password: The application user password configured in the container (default:"super-secret", envMYSQL_PASSWORD).mysql_root_password: The root password configured in the container (default:"super-secret", envMYSQL_ROOT_PASSWORD).mysql_database: The base database configured in the container (default:"db", envMYSQL_DATABASE;<mysql_database>_<worker>underpytest-xdist"database"isolation).mysql_port: Optional pinned host port for MySQL containers (default:None, envMYSQL_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.6mysql_57_service: MySQL 5.7mysql_8_service: MySQL 8.0mysql_84_service: MySQL 8.4 LTSmysql_96_service: MySQL 9.6 Innovationmysql_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:
ServiceContainerService handle for a running MySQL container.
- 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
sqlinside a MySQL container usingMYSQL_PWDauthentication.- Parameters:
container – Running MySQL container handle.
sql – SQL statement(s) passed to
mysql -e.user – MySQL user name.
password – Password passed via
MYSQL_PWDenvironment variable.database – Optional target database name.
batch – When
True, adds--batch --skip-column-namesfor 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_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_service(mysql_84_service: MySQLService) MySQLService[source]#
Provide the default MySQL container service.
- pytest_databases.docker.mysql.platform() str[source]#
Return the legacy
linux/x86_64platform string.Deprecated since version 0.20.0: Use
mysql_platforminstead.platformwill 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_schemaenabled by default.- Yields:
Active
MySQLServiceinstance bound todatabase(or<database>_<worker>under xdist).- Raises:
ValueError – If
databaseoruseris invalid.RuntimeError – If database provisioning, user grants, or
init_sqlfails.