- 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, wait_strategy_check_string: str = '.*: ready for connections.*: ready for connections.*', **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 formatmysql+dialect://username:password@host:port/database.>>> import sqlalchemy >>> from testcontainers.community.mysql import MySqlContainer >>> with MySqlContainer("mysql:8.0", 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
seedparameter 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.>>> import sqlalchemy >>> from testcontainers.community.mysql import MySqlContainer >>> with MySqlContainer(seed=f"{TEST_DIR}/community/mysql/seeds/", dialect="pymysql") as mysql: ... engine = sqlalchemy.create_engine(mysql.get_connection_url()) ... with engine.begin() as connection: ... query = "select name from stuff" ... result = connection.execute(sqlalchemy.text(query)) ... first_stuff, = result.fetchone()