WinServer2022通过定时任务监视MySQL的Slave服务并自动启动Slave服务

WinServer 2022 配置定时任务

在Windows Server 2022上部署了Mysql数据库,Mysql数据库启动或者重启时,Slave主从复制线程没有开启。需要编写脚本周期判断Mysql服务是否正常运行,如果正常运行,则启动Slave服务。

1、 编写监视脚本

①、 编写脚本

示例脚本如下:

**"MysqlSlaveMonitor.bat"**脚本内容如下:

shell 复制代码
@echo off
setlocal
 
:: 设置MySQL服务器的用户名和密码
set MYSQL_USER=root
set MYSQL_PASS=root123
 
:: 设置MySQL服务器的主机地址和端口
set HOST=localhost
set PORT=3306
 
:: 运行mysql命令并检查Slave状态
mysql -u%MYSQL_USER% -p%MYSQL_PASS% -h%HOST% -P%PORT% -e "show slave status\G" | findstr  "Slave_IO_Running:" | findstr "Yes"
 
:: 检查返回值,如果包含"Yes",则表示Slave正在运行
if %ERRORLEVEL% equ 0 (
    echo Slave is running.
) else (
    echo Slave is not running.
    :: 启动Slave服务
    mysql -u%MYSQL_USER% -p%MYSQL_PASS% -h%HOST% -P%PORT% -e "start slave;"
)
 
endlocal

②、 部署脚本

"MysqlSlaveMonitor.bat" 脚本部署到指定位置,比如:可以放到 C:\Program Files\MySQL\ 目录下 。

2、 配置Windows定时任务

【1】、 任务计划程序

通过 "Win" + "R" 组合键,调出 "运行" 界面,输入 taskschd.msc 并回车。

【2】、 创建NK8500任务分组

右键 "任务计划程序库" ,选择【新文件夹】选项,创建任务分组。

输入文件夹名称。

创建完成。

【3】、 创建任务

右键刚才创建的任务组,选择【创建任务】选项。

【4】、 创建任务-常规

创建任务对话框,选择【常规】页面,输入"名称"、"描述" 等参数。

参数配置完后,点击【确定】按钮。

点击【确定】按钮时弹出的提示框 "必须输入任务名和至少一个操作" 忽略,点击【确定】即可。

【5】、 创建任务-触发器

创建任务对话框,选择【触发器】页面,点击【新建】按钮。

设置任务 "重复任务间隔" 和 "持续时间" 类型。

配置完后,点击【确定】按钮。

【6】、 创建任务-操作

创建任务对话框,选择【操作】页面,点击【新建】按钮。

在弹出的新建操作 对话框中的 "程序和脚本" 中选择 刚才编写的 bat 脚本。

操作完后,点击【确定】按钮。

点击【确定】按钮,弹出用户账号密码输入对话框,输入用户密码即可。

用户密码输入后,系统会回到主页,如下图所示:

【7】、 创建任务-条件

双击任务列表中的 **"MySQL主从Slave服务监视" **任务。

弹出的对话框,选择【条件】页面,取消勾选标注选项。

点击【确定】,弹出对话框,输入用户密码,并确定。

【8】、 创建任务-设置

双击任务列表中的 **"MySQL主从Slave服务监视" **任务。

弹出的对话框,选择【设置】页面,勾选标注选项。。

点击【确定】,弹出对话框,输入用户密码,并确定。

【9】、 创建任务完成

通过点击【刷新】按钮,更新任务执行状态。

3、 验证

①、 关闭Slave服务

人工关闭 Slave 服务。

shell 复制代码
C:\Users\Administrator>mysql -uroot -proot123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.1.70-community-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql>
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

②、 查询Slave服务

过3分钟查询 Slave 服务状态。

shell 复制代码
C:\Users\Administrator>mysql -uroot -proot123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.1.70-community-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.58.1
                  Master_User: sync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-binlog.000020
          Read_Master_Log_Pos: 106
               Relay_Log_File: zxdd1-relay-bin.000035
                Relay_Log_Pos: 2240629
        Relay_Master_Log_File: master-binlog.000018
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2240480
              Relay_Log_Space: 295381944
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno:
               Last_SQL_Error: 
1 row in set (0.00 sec)

mysql>
相关推荐
yumgpkpm13 小时前
华为鲲鹏 Aarch64 环境下多 Oracle 、mysql数据库汇聚到Cloudera CDP7.3操作指南
大数据·数据库·mysql·华为·oracle·kafka·cloudera
1024小神13 小时前
为已有nextjs项目添加supabase数据库,不再需要冗余后端
数据库
best_virtuoso14 小时前
PostgreSQL PostGIS安装与配置,现有数据库启用PostGIS扩展
数据库·postgresql
橙汁味的风14 小时前
3关系型数据库的SQL语言
数据库·sql
学编程的董14 小时前
07 计算字段的创建与使用 - 数据转换的艺术
数据库·oracle
程序员云帆哥14 小时前
MySQL JDBC Driver URL参数配置规范
数据库·mysql·jdbc
TDengine (老段)15 小时前
TDengine 数学函数 FLOOR 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
大气层煮月亮15 小时前
Oracle EBS ERP开发——报表生成Excel标准模板设计
数据库·oracle·excel
云和数据.ChenGuang15 小时前
达梦数据库的命名空间
数据库·oracle
三三木木七16 小时前
mysql拒绝连接
数据库·mysql