mysql.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from materialize.mzcompose.service import (
  10. Service,
  11. )
  12. def create_mysql_server_args(server_id: str, is_master: bool) -> list[str]:
  13. args = [
  14. "--log-bin=mysql-bin",
  15. "--gtid_mode=ON",
  16. "--enforce_gtid_consistency=ON",
  17. "--binlog-format=row",
  18. "--binlog-row-image=full",
  19. f"--server-id={server_id}",
  20. ]
  21. if not is_master:
  22. args.append("--log-slave-updates")
  23. args.append("--skip-replica-start")
  24. return args
  25. class MySql(Service):
  26. DEFAULT_ROOT_PASSWORD = "p@ssw0rd"
  27. DEFAULT_VERSION = "9.4.0"
  28. DEFAULT_ADDITIONAL_ARGS = create_mysql_server_args(server_id="1", is_master=True)
  29. def __init__(
  30. self,
  31. root_password: str = DEFAULT_ROOT_PASSWORD,
  32. name: str = "mysql",
  33. version: str = DEFAULT_VERSION,
  34. port: int = 3306,
  35. volumes: list[str] = ["mydata:/var/lib/mysql-files"],
  36. additional_args: list[str] = DEFAULT_ADDITIONAL_ARGS,
  37. ) -> None:
  38. image = f"mysql:{version}"
  39. super().__init__(
  40. name=name,
  41. config={
  42. "image": image,
  43. "init": True,
  44. "ports": [port],
  45. "environment": [
  46. f"MYSQL_ROOT_PASSWORD={root_password}",
  47. ],
  48. "command": [
  49. "--secure-file-priv=/var/lib/mysql-files",
  50. *additional_args,
  51. ],
  52. "healthcheck": {
  53. "test": [
  54. "CMD",
  55. "mysqladmin",
  56. "ping",
  57. f"--password={root_password}",
  58. "--protocol=TCP",
  59. ],
  60. "interval": "1s",
  61. "start_period": "60s",
  62. },
  63. "volumes": volumes,
  64. },
  65. )