Module materialize.mzcompose.services.mysql

Expand source code Browse git
# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.


from materialize.mzcompose.service import (
    Service,
)


def create_mysql_server_args(server_id: str, is_master: bool) -> list[str]:
    args = [
        "--log-bin=mysql-bin",
        "--gtid_mode=ON",
        "--enforce_gtid_consistency=ON",
        "--binlog-format=row",
        "--binlog-row-image=full",
        f"--server-id={server_id}",
    ]

    if not is_master:
        args.append("--log-slave-updates")
        args.append("--skip-replica-start")

    return args


class MySql(Service):
    DEFAULT_ROOT_PASSWORD = "p@ssw0rd"
    DEFAULT_VERSION = "8.0.35"

    DEFAULT_ADDITIONAL_ARGS = create_mysql_server_args(server_id="1", is_master=True)

    def __init__(
        self,
        root_password: str = DEFAULT_ROOT_PASSWORD,
        name: str = "mysql",
        version: str = DEFAULT_VERSION,
        port: int = 3306,
        volumes: list[str] = ["mydata:/var/lib/mysql-files"],
        additional_args: list[str] = DEFAULT_ADDITIONAL_ARGS,
    ) -> None:
        image = f"mysql:{version}"

        super().__init__(
            name=name,
            config={
                "image": image,
                "init": True,
                "ports": [port],
                "environment": [
                    f"MYSQL_ROOT_PASSWORD={root_password}",
                ],
                "command": [
                    "--default-authentication-plugin=mysql_native_password",
                    "--secure-file-priv=/var/lib/mysql-files",
                    *additional_args,
                ],
                "healthcheck": {
                    "test": [
                        "CMD",
                        "mysqladmin",
                        "ping",
                        f"--password={root_password}",
                        "--protocol=TCP",
                    ],
                    "interval": "1s",
                    "start_period": "60s",
                },
                "volumes": volumes,
            },
        )

Functions

def create_mysql_server_args(server_id: str, is_master: bool) ‑> list[str]
Expand source code Browse git
def create_mysql_server_args(server_id: str, is_master: bool) -> list[str]:
    args = [
        "--log-bin=mysql-bin",
        "--gtid_mode=ON",
        "--enforce_gtid_consistency=ON",
        "--binlog-format=row",
        "--binlog-row-image=full",
        f"--server-id={server_id}",
    ]

    if not is_master:
        args.append("--log-slave-updates")
        args.append("--skip-replica-start")

    return args

Classes

class MySql (root_password: str = 'p@ssw0rd', name: str = 'mysql', version: str = '8.0.35', port: int = 3306, volumes: list[str] = ['mydata:/var/lib/mysql-files'], additional_args: list[str] = ['--log-bin=mysql-bin', '--gtid_mode=ON', '--enforce_gtid_consistency=ON', '--binlog-format=row', '--binlog-row-image=full', '--server-id=1'])

A Docker Compose service in a Composition.

Attributes

name
The name of the service.
config
The definition of the service.
Expand source code Browse git
class MySql(Service):
    DEFAULT_ROOT_PASSWORD = "p@ssw0rd"
    DEFAULT_VERSION = "8.0.35"

    DEFAULT_ADDITIONAL_ARGS = create_mysql_server_args(server_id="1", is_master=True)

    def __init__(
        self,
        root_password: str = DEFAULT_ROOT_PASSWORD,
        name: str = "mysql",
        version: str = DEFAULT_VERSION,
        port: int = 3306,
        volumes: list[str] = ["mydata:/var/lib/mysql-files"],
        additional_args: list[str] = DEFAULT_ADDITIONAL_ARGS,
    ) -> None:
        image = f"mysql:{version}"

        super().__init__(
            name=name,
            config={
                "image": image,
                "init": True,
                "ports": [port],
                "environment": [
                    f"MYSQL_ROOT_PASSWORD={root_password}",
                ],
                "command": [
                    "--default-authentication-plugin=mysql_native_password",
                    "--secure-file-priv=/var/lib/mysql-files",
                    *additional_args,
                ],
                "healthcheck": {
                    "test": [
                        "CMD",
                        "mysqladmin",
                        "ping",
                        f"--password={root_password}",
                        "--protocol=TCP",
                    ],
                    "interval": "1s",
                    "start_period": "60s",
                },
                "volumes": volumes,
            },
        )

Ancestors

Class variables

var DEFAULT_ADDITIONAL_ARGS
var DEFAULT_ROOT_PASSWORD
var DEFAULT_VERSION