当前位置: 首页 > news >正文

A script to set mysql gtid-mode off by tortoise-orm

  • mysql_gtid.py
from __future__ import annotationsimport asyncio
import contextlib
import os
import sys
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanagerfrom tortoise import BaseDBAsyncClient, connections
from tortoise.backends.base.config_generator import expand_db_url
from tortoise.exceptions import OperationalError@asynccontextmanager
async def tortoise_context() -> AsyncGenerator[BaseDBAsyncClient]:password = os.getenv("TORTOISE_MYSQL_PASS", "123456")db_url = f"mysql://root:{password}@127.0.0.1:3306/mysql"connections_config = {"default": expand_db_url(db_url)}await connections._init(connections_config, create_db=False)try:yield connections.get("default")finally:await connections.close_all()class MysqlGtid:def __init__(self, conn: BaseDBAsyncClient) -> None:self.conn = connasync def get_var_value(self, statement: str) -> str:print(f"--> {statement}")result = await self.conn.execute_query_dict(statement)return str(result[0]["Value"])async def get_gtid_consistency(self) -> str:statement = "SHOW VARIABLES LIKE 'enforce_gtid_consistency';"return await self.get_var_value(statement)async def is_enforce_gtid(self) -> bool:return (await self.get_gtid_consistency()).upper() == "ON"async def get_gtid_mode_status(self) -> str:statement = "SHOW VARIABLES LIKE 'gtid_mode';"return await self.get_var_value(statement)async def execute_script(self, statement: str) -> None:print(f"--> {statement}")await self.conn.execute_script(statement)async def set_enforce_gtid_off(self, mode_on: bool, gtid_mode: str) -> None:statement = "SET GLOBAL enforce_gtid_consistency = OFF;"if mode_on:if gtid_mode == "ON":await self.execute_script("SET GLOBAL gtid_mode = ON_PERMISSIVE;")await self.execute_script("SET GLOBAL gtid_mode = OFF_PERMISSIVE;")await self.execute_script("SET GLOBAL gtid_mode = OFF;")await self.execute_script(statement)async def set_enforce_gtid_on(self, mode_on: bool, origin_gtid_mode: str) -> None:statement = "SET GLOBAL enforce_gtid_consistency = ON;"await self.execute_script(statement)if mode_on:current_status = (await self.get_gtid_mode_status()).upper()if current_status == origin_gtid_mode.upper():returnwith contextlib.suppress(OperationalError):if current_status == "OFF":await self.execute_script("SET GLOBAL gtid_mode = OFF_PERMISSIVE;")await self.execute_script("SET GLOBAL gtid_mode = ON_PERMISSIVE;")if origin_gtid_mode.upper() == "ON":await self.execute_script("SET GLOBAL gtid_mode = ON;")async def main() -> None:async with tortoise_context() as conn:db = MysqlGtid(conn)if "--list" in sys.argv:print(await db.get_gtid_consistency())print(await db.get_gtid_mode_status())elif "--on" in sys.argv:if (await db.get_gtid_mode_status()).upper() == "ON":print("gtid_mode is ON, nothing to do.")returnawait db.set_enforce_gtid_on(True, "ON")else:if not await db.is_enforce_gtid():print("enforce_gtid_consistency is OFF, nothing to do.")returnorigin_gtid_mode = await db.get_gtid_mode_status()gtid_mode = origin_gtid_mode.upper()mode_on = gtid_mode.startswith("ON")await db.set_enforce_gtid_off(mode_on, gtid_mode)if __name__ == "__main__":asyncio.run(main())

Usage::

python mysql_gtid.py --list
python mysql_gtid.py --on
python mysql_gtid.py --off
http://icebutterfly214.com/news/58965/

相关文章:

  • 球坐标系的大运动方程组
  • i2c linux
  • https linux
  • 获深圳人才集团认可!「张张讲AI」AI资讯公众号解读AI动态,讲师提供定制化咨询
  • Unable to add window -- token null is not valid; is your activity running?
  • 20232417 2025-2026-1 《网络与系统攻防技术》实验六实验报告
  • ElasticSearch索引库操作 - 努力-
  • MinIo介绍 - 努力-
  • BLOG1
  • 帮同学签了个到,我发现竟然能盗光他所有账号
  • 数学的大厦(五):除法、有理数、等价关系
  • history of linux
  • 兰州市一对一培训机构推荐,2026年最新课外辅导补习机构口碑深度测评排名榜
  • 决策单调性 dp 的分治解法(整体二分解法)
  • 2026年宿迁一对一家教机构推荐:五大辅导机构测评排行榜,综合实力全解析!
  • 2025云南曲靖市玉溪市一对一家教辅导测评排行榜:权威推荐高性价比选择
  • 2025年11月22日
  • C# Avalonia 18- ControlTemplates - FlipPanelTest
  • 崖山数据库导出 - 华
  • 使用C# Channel实现工位流水线调度系统
  • 英语_阅读_AI models_待读
  • 使用java实验电梯调度算法
  • 2025年不锈钢管锯片供货厂家权威推荐榜单:切H型钢/角钢切割/切碳素钢锯片源头厂家精选
  • 2025.11.18 写题记录
  • gvim linux
  • 2025年好吃不贵的餐厅服务权威推荐榜单:宝藏餐厅/好吃的餐厅/口碑好的餐厅服务精选
  • grub命令行启动linux
  • 2025 年 11 月方形冷却塔,圆形冷却塔,横流冷却塔,逆流冷却塔厂家最新推荐,聚焦资质、案例、售后的五家机构深度解读!
  • Grid-dp,交互
  • 计算机操作系统 - 设备管理 - 指南