Home > Database > Mysql Tutorial > 逐步讲解MySQL中定时事件计划的创建_MySQL

逐步讲解MySQL中定时事件计划的创建_MySQL

WBOY
Release: 2016-05-27 13:44:47
Original
1047 people have browsed it

一、使用过程
1.查看当前是否已开启事件计划(调度器)有3种方法:

SHOW VARIABLES LIKE 'event_scheduler';
SELECT @@event_scheduler;
SHOW PROCESSLIST;
Copy after login


2. 开启事件计划(调度器)开关有4种方法:

SET GLOBAL event_scheduler = 1;
SET @@global.event_scheduler = 1;
SET GLOBAL event_scheduler = ON;
SET @@global.event_scheduler = ON;
Copy after login

键值1或者ON表示开启;0或者OFF表示关闭;

3.关于事件计划的权限:
单独使用event调用SQL语句时,查看和创建需要用户具有event权限,调用该SQL语句时,需要用户具有执行该SQL的权限。Event权 限的设置保存在mysql.user表和mysql.db表的Event_priv字段中。(FLUSH PRIVILEGES;)
当event和procedure配合使用的时候,查看和创建存储过程需要用户具有create routine权限,调用存储过程执行时需要使用excute权限,存储过程调用具体的SQL语句时,需要用户具有执行该SQL的权限。

SELECT HOST,USER,Event_priv FROM mysql.user;
Copy after login

201653145320225.jpg (339×169)

获取当前登陆的用户和数据库:SELECT CURRENT_USER(), SCHEMA();
从Figure1可以知道bfsql@%是没有Event_priv权限的,在该用户下创建事件的时候会出现下面的错误:
Error Code: 1044Access denied for user 'bfsql'@'%' to database 'blog'
如果出现上面的错误,执行下面的SQL就可以给bfsql@%赋予创建Event的权限:

UPDATE mysql.user SET Event_priv = 'Y' WHERE HOST='%' AND USER='bfsql'; 
FLUSH PRIVILEGES; 
Copy after login

最后,你可以通过SHOW GRANTS FOR 'bfsql'@'%';查看所有权限;

4.创建事件:
(1)创建事件的语法如下:

CREATE EVENT [IF NOT EXISTS] event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']
DO sql_statement
Copy after login

(2)创建事件的示例如下:

DELIMITER $$
CREATE EVENT IF NOT EXISTS e_blog
ON SCHEDULE EVERY 30 SECOND
ON COMPLETION PRESERVE
DO BEGIN
CALL MoveBlogData();
END$$
DELIMITER ;
Copy after login


DO sql_statement字段表示该event需要执行的SQL语句或存储过程。这里的SQL语句可以是复合语句,使用BEGIN和END标识符将复合SQL语句按照执行顺序放在之间。

--从现在开始每隔九天定时执行 
CREATE EVENT EVENT1 
ON SCHEDULE EVERY 9 DAY STARTS NOW() 
ON COMPLETION PRESERVE ENABLE 
DO 
  BEGIN 
    CALL TOTAL(); 
  END 
 
 
--每个月的一号凌晨1 点执行 
CREATE EVENT EVENT2   
ON SCHEDULE EVERY 1 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH),INTERVAL 1 HOUR) 
ON COMPLETION PRESERVE ENABLE 
DO 
  BEGIN 
    CALL STAT(); 
  END 
 
---每个季度一号的凌晨2点执行 
CREATE EVENT TOTAL_SEASON_EVENT 
ON SCHEDULE EVERY 1 QUARTER STARTS DATE_ADD(DATE_ADD(DATE( CONCAT(YEAR(CURDATE()),'-',ELT(QUARTER(CURDATE()),1,4,7,10),'-',1)),INTERVAL 1 QUARTER),INTERVAL 2 HOUR) 
ON COMPLETION PRESERVE ENABLE 
DO 
  BEGIN 
    CALL SEASON_STAT(); 
  END 
 
--每年1月1号凌晨四点执行 
CREATE EVENT TOTAL_YEAR_EVENT 
ON SCHEDULE EVERY 1 YEAR STARTS DATE_ADD(DATE(CONCAT(YEAR(CURDATE()) + 1,'-',1,'-',1)),INTERVAL 4 HOUR) 
ON COMPLETION PRESERVE ENABLE 
DO 
  BEGIN 
    CALL YEAR_STAT(); 
  END 
Copy after login


5.事件开启与关闭:
开启某事件:

ALTER EVENT e_test ON COMPLETION PRESERVE ENABLE;
Copy after login

关闭某事件:

ALTER EVENT e_test ON COMPLETION PRESERVE DISABLE;
Copy after login

二、实例:
mysql定时器是系统给提供了event,而oracle里面的定时器是系统给提供的job。废话少说,下面创建表:

create table mytable (
id int auto_increment not null,
name varchar(100) not null default '',
introduce text not null,
createtime timestamp not null,
constraint pk_mytable primary key(id)
)
Copy after login


创建存储过程,这里的存储过程主要提供给mysql的定时器event来调用去执行:

create procedure mypro()
BEGIN
insert into mytable (name,introduce,createtime) values ('1111','inner mongolia',now());
end;
Copy after login

这里只是简单的写了一下,只是为了说明例子。


紧接着创建mysql的定时器event:

create event if not exists eventJob 
on schedule every 1 second 
on completion PRESERVE
do call mypro();
Copy after login

这里设置为每一秒执行一次

至此所有的准备工作已经写完了,做完这些,mysql要想利用定时器必须的做准备工作,就是把mysql的定时器给开启了:

SET GLOBAL event_scheduler = 1; -- 启动定时器
SET GLOBAL event_scheduler = 0; -- 停止定时器
Copy after login

紧接着还要开启事件:

ALTER EVENT eventJob ON COMPLETION PRESERVE ENABLE;  -- 开启事件
ALTER EVENT eventJob ON COMPLETION PRESERVE DISABLE; -- 关闭事件


SHOW VARIABLES LIKE '%sche%'; -- 查看定时器状态

Copy after login

至此,你去数据库里面的表mytable里面看下,系统会每隔一秒去插入一条数据,嘻嘻,任务完成了。

select * from mytable
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template