Mysql5.7.31 使用定时器自动执行FLUSH HOSTS处理

最近数据库总是出现连接错误,导致阻塞。并提示以下错误:
Host is blocked because of many connection errors;unblock with 'mysqladmin flush-hosts'
我已经将数据库错误连接数调大了,但是还是没一会就出错。
这里手动的解决方法是去数据库执行脚本:FLUSH HOSTS;
也可以按照如下方法去配置一个定时任务,让MySQL自动执行

开启事件调度

临时启用事件调度

  1. 执行命令查看事件调度是否启用
    show VARIABLES LIKE '%event_scheduler%';
  2. 如果没有启用则执行以下命令(OFF或者0或者DISABLED)
    SET GLOBAL event_scheduler = 1;

永久的开启事件调度
在my.cnf中的[mysqld]部分添加如下内容,然后重启mysql
event_scheduler=ON

创建存储过程和定时器

  1. 创建存储过程
drop procedure if exists flush_hosts_proce

create procedure flush_hosts_proce()
begin
FLUSH HOSTS;
end
  1. 创建定时器
drop event if exists flush_hosts_event;
create event flush_hosts_event
	on schedule every 120 second
	on completion preserve disable
	do call flush_hosts_proce();

开启定时任务与查看事件

  1. 开启定时任务
    alter event flush_hosts_event on completion preserve enable;
  2. 查看事件
    select * from mysql.event;
致谢:参考原文链接:https://blog.csdn.net/vfsdfdsf/article/details/89674344
posted @ 2022-09-15 17:28  Huathy  阅读(100)  评论(0)    收藏  举报  来源