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>
相关推荐
阿华的代码王国33 分钟前
MySQL ------- 索引(B树B+树)
数据库·mysql
Hello.Reader1 小时前
StarRocks实时分析数据库的基础与应用
大数据·数据库
执键行天涯1 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
liupenglove1 小时前
golang操作mysql利器-gorm
mysql·golang
yanglamei19621 小时前
基于GIKT深度知识追踪模型的习题推荐系统源代码+数据库+使用说明,后端采用flask,前端采用vue
前端·数据库·flask
叫我:松哥1 小时前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap
工作中的程序员2 小时前
ES 索引或索引模板
大数据·数据库·elasticsearch
严格格2 小时前
三范式,面试重点
数据库·面试·职场和发展
微刻时光2 小时前
Redis集群知识及实战
数据库·redis·笔记·学习·程序人生·缓存
单字叶2 小时前
MySQL数据库
数据库·mysql