class MySqlContainer(image: str = 'mysql:latest', dialect: str | None = None, username: str | None = None, root_password: str | None = None, password: str | None = None, dbname: str | None = None, port: int = 3306, seed: str | None = None, **kwargs)ΒΆ

MySql database container.

Example

The example will spin up a MySql database to which you can connect with the credentials passed in the constructor. Alternatively, you may use the get_connection_url() method which returns a sqlalchemy-compatible url in format mysql+dialect://username:password@host:port/database.

>>> import sqlalchemy
>>> from testcontainers.mysql import MySqlContainer

>>> with MySqlContainer("mysql:5.7.17", dialect="pymysql") as mysql:
...     engine = sqlalchemy.create_engine(mysql.get_connection_url())
...     with engine.begin() as connection:
...         result = connection.execute(sqlalchemy.text("select version()"))
...         version, = result.fetchone()

The optional seed parameter enables arbitrary SQL files to be loaded. This is perfect for schema and sample data. This works by mounting the seed to /docker-entrypoint-initdb.d/, which containerized MySQL are set up to load automatically.