Mysql5.7.31 使用定时器自动执行FLUSH HOSTS处理
最近数据库总是出现连接错误,导致阻塞。并提示以下错误:
 Host is blocked because of many connection errors;unblock with 'mysqladmin flush-hosts'
 我已经将数据库错误连接数调大了,但是还是没一会就出错。
 这里手动的解决方法是去数据库执行脚本:FLUSH HOSTS;
 也可以按照如下方法去配置一个定时任务,让MySQL自动执行
开启事件调度
临时启用事件调度
- 执行命令查看事件调度是否启用
show VARIABLES LIKE '%event_scheduler%'; - 如果没有启用则执行以下命令(OFF或者0或者DISABLED)
SET GLOBAL event_scheduler = 1; 
永久的开启事件调度
 在my.cnf中的[mysqld]部分添加如下内容,然后重启mysql
 event_scheduler=ON
创建存储过程和定时器
- 创建存储过程
 
drop procedure if exists flush_hosts_proce
create procedure flush_hosts_proce()
begin
FLUSH HOSTS;
end
 
- 创建定时器
 
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();
 
开启定时任务与查看事件
- 开启定时任务
alter event flush_hosts_event on completion preserve enable; - 查看事件
select * from mysql.event; 
本文来自博客园,作者:Huathy,遵循 CC 4.0 BY-NC-SA 版权协议。转载请注明原文链接:https://www.cnblogs.com/huathy/p/17253807.html
                    
                
                
            
        
浙公网安备 33010602011771号