Mysql event (timed task)

mysql create task (event)
  1. Check whether the database event is turned on, if event_scheduler is equal to NO, it means turn on
    SELECT @@event_scheduler;
    SHOW VARIABLES LIKE'event_scheduler';
    SELECT @@event_scheduler;
  2. Turn on task (event)
    a, set global event_scheduler= 1;
    b. SET GLOBAL event_scheduler = ON;
    c. Add event_scheduler=ON in the [mysqld] section of my.cnf and restart mysql.
  3. Close event
    SET GLOBAL event_scheduler = OFF;
  4. Syntax:

CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT ‘comment’]
DO event_body;
schedule:
AT timestamp [+ INTERVAL interval] …
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] …]
[ENDS timestamp [+ INTERVAL interval] …]
interval:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR |
DAY_MINUTE |DAY_SECOND | HOUR_MINUTE |
HOUR_SECOND | MINUTE_SECOND}

Detailed parameter description:
DEFINER: Define the user who checks the permissions when the event is executed.
ON SCHEDULE schedule: Define the execution time and time interval.
ON COMPLETION [NOT] PRESERVE: Define whether the event is executed once or permanently. The default is one execution, that is, NOT PRESERVE.
ENABLE | DISABLE | DISABLE ON SLAVE: Define whether the event is opened or closed after creation, and closed from above. If the slave server automatically synchronizes the statement that creates an event on the master, it will automatically add DISABLE ON SLAVE.
COMMENT'comment': Define the comment of the event.
5. Turn on the event
alter event event_name (event name) ON COMPLETION PRESERVE ENABLE;
6. Turn off the event
alter event event_name (event name) ON COMPLETION PRESERVE DISABLE;
7. Delete the event
DROP EVENT [IF EXISTS] event_name (event name)
8. Case

CREATE TABLE test (
id int(11) NOT NULL AUTO_INCREMENT,
t1 datetime DEFAULT NULL,
id2 int(11) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=106 DEFAULT CHARSET=utf8

CREATE EVENT IF NOT EXISTS e_test_1 ON SCHEDULE EVERY 3 SECONDON COMPLETION PRESERVE

DO INSERT INTO test(id,t1) VALUES(NULL,NOW()); CREATE EVENT IF NOT EXISTS e_test_2ON SCHEDULE
AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
DO TRUNCATE TABLE test; 1、创建过程CREATE PROCEDURE pro_test()
BEGIN
INSERT INTO test(id,t1,id2) VALUES(NULL,NOW(),‘1000000’);
END2、调用过程CREATE EVENT IF NOT EXISTS e_test_3 ON SCHEDULE EVERY 3 SECONDON COMPLETION PRESERVE
DO CALL pro_test();

9. About the permission of the event (Access denied for
  user'root '@'%' to database'xxxx') After creating a new database and a new user on the mysql remotely connected to Naicat Premium, add this new user to the user When the database permissions appear:
  access denied for user'root'@'%' to database xxxx prompt.
  The reason for the error is that the root user is on the MYSQL remote connection and does not have the authorization of this new database. This problem should not exist when using mysql locally.
  Solution, perform authorization:
    UPDATE mysql.user SET Event_priv ='Y' WHERE HOST='%' AND USER='root';
    FLUSH PRIVILEGES;
    grant all PRIVILEGES on xxxx.* to root@'%' identified by'password' with grant option;
    grant all on xxxx.* to'root'@'%' identified by'password' with grant option;
    xxxx is the created database, and password is the password of root. Please make changes according to actual requirements.

Guess you like

Origin blog.csdn.net/weixin_42094764/article/details/114665771