testcontainers-mysql

raw JSON →
0.0.1rc1 verified Sat May 09 auth: no python

MySQL component of testcontainers-python, providing lightweight MySQL database containers for integration testing. Compatible with testcontainers core. Current version: 0.0.1rc1 (pre-release). Release cadence: follows testcontainers-python releases (monthly).

pip install testcontainers-mysql
error ModuleNotFoundError: No module named 'testcontainers'
cause testcontainers core package is not installed.
fix
pip install testcontainers
error docker.errors.DockerException: Error while fetching server API version
cause Docker daemon is not running or user lacks permissions.
fix
Start Docker Desktop or run 'dockerd'; ensure user is in 'docker' group.
error testcontainers.mysql.container:error: MySQL container crashed on startup
cause Outdated or incompatible MySQL image specified, or memory constraints.
fix
Use a recent and stable image like 'mysql:8.0'. Increase Docker memory allocation if needed.
breaking MySQLContainer requires a Docker image tag (e.g., 'mysql:8.0') as of v0.0.1rc1. Older versions may have used a default tag, which no longer works.
fix Always specify an image tag: MySQLContainer('mysql:8.0').
gotcha get_connection_url() returns a JDBC-style URL (jdbc:mysql://...). Use get_connection_params() to get a dict suitable for Python drivers (pymysql, mysql-connector-python).
fix Use mysql.get_connection_params() for Python driver connections.
gotcha Container may fail silently if Docker is not running or if ports are occupied. Always check container logs or use wait_for() strategies.
fix Ensure Docker is running and free ports are available. Use with statement for automatic cleanup.

Start a MySQL 8.0 container, get connection parameters, and run a query.

from testcontainers.mysql import MySQLContainer

with MySQLContainer('mysql:8.0') as mysql:
    connection_url = mysql.get_connection_url()
    print(f"Connection URL: {connection_url}")
    # Use the connection URL with a MySQL driver
    import pymysql
    conn = pymysql.connect(**mysql.get_connection_params())
    cursor = conn.cursor()
    cursor.execute("SELECT version();")
    print(cursor.fetchone())
    conn.close()