MySQL timer combat

1. Introduction
  In the commercial environment of the project, users reported a statistical problem. After investigation, it was found that the ID of the servers in the data reported by the two business servers did not match due to the addition of a business server. In the case of not releasing the revised version, I hope to solve this statistical problem quickly and quickly. After discussion, I decided to solve the problem from the database level, that is, adding triggers or timers to modify the ID data of the two servers to be consistent. , but considering that the trigger may have a great impact on performance, a timer is finally used to solve this problem.
  The project uses MySQL. The following describes the process of creating a MySQL timer.

2. The solution process
2.1 Configuration
1. First, check whether MySQL has enabled timed events (the default is closed), the command is as follows
/* View event_scheduler parameters */
SHOW VARIABLES LIKE '%event_scheduler%';


If the event_scheduler parameter is not ON, then modify the MySQL configuration file (my.cnf under Linux, my.ini under Windows), and add the following configuration under [mysqld]
[mysqld]
event_scheduler=ON


If the configuration file of MySQL is modified, please restart the MySQL service for it to take effect.

2.2 Create a test table
Use the following SQL to create a test table test_event
CREATE TABLE `test_event` (
   `id` bigint(20) NOT NULL AUTO_INCREMENT,
   `name` varchar(50) DEFAULT NULL,
   `create_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
   `update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


2.3 Create a timer
Use the following script to create a timer, the SQL is as follows:
DELIMITER $$
SET GLOBAL event_scheduler = ON$$
DROP EVENT IF EXISTS `insert_event_example`;
CREATE EVENT `insert_event_example`
ON SCHEDULE
   EVERY 5 SECOND
   STARTS '2016-11-29 00:30:00'
DO
BEGIN
    INSERT INTO test_event(`name`,`create_time`,`update_time`) VALUES(UUID(),NOW(),NOW());
END$$
DELIMITER ;


Among them:
(1) "SET GLOBAL event_scheduler = ON$$", set event_scheduler to ON;
quote

Special note :
This statement can set the timing task of opening MySQL, but once the mysql service is restarted, it will still be restored to the previous state, that is, if the configuration file is configured to be OFF, it will be changed to ON after executing this statement, but the event_scheduler will be restarted after the restart. It is OFF, which means that this statement only modifies the state of the server memory, and is only valid for this MySQL service. After the MySQL service is restarted, the server will re-read the configuration from the configuration file, so it is recommended to modify the configuration file. This configuration prevents scheduled tasks from failing after restarting the MySQL service;

(2) "ON SCHEDULE EVERY 5 SECOND STARTS '2016-11-29 00:30:00'", set the time of the timer, starting from '2016-11-29 00:30:00', executed every 5 seconds ;
(3) Between "BEGIN" and "END$$" is the SQL statement to be executed. If the executed statement is complex, it can be written as a function or stored procedure;

if the timer is successfully created, use the following command to get the result from the View the added timer
SHOW EVENTS;


2.4 Verify
Use the following SQL to view the data of the test table
mysql> select * from test_event;

The result is as follows, a record is automatically inserted every 5 seconds, indicating the scheduled task.
id  name                                 create_time         update_time        
1 ed2ecc05-b5fa-11e6-82f8-989096ac7070 2016-11-29 14:13:20 2016-11-29 14:13:20
2 f029c3ed-b5fa-11e6-82f8-989096ac7070 2016-11-29 14:13:25 2016-11-29 14:13:25
3 f324b979-b5fa-11e6-82f8-989096ac7070 2016-11-29 14:13:30 2016-11-29 14:13:30
4 f61feee8-b5fa-11e6-82f8-989096ac7070 2016-11-29 14:13:35 2016-11-29 14:13:35
5 f91aca53-b5fa-11e6-82f8-989096ac7070 2016-11-29 14:13:40 2016-11-29 14:13:40
6 fc15e1e1-b5fa-11e6-82f8-989096ac7070 2016-11-29 14:13:45 2016-11-29 14:13:45
7 ff10e0b4-b5fa-11e6-82f8-989096ac7070 2016-11-29 14:13:50 2016-11-29 14:13:50
8 020bfa59-b5fb-11e6-82f8-989096ac7070 2016-11-29 14:13:55 2016-11-29 14:13:55
9 0506f082-b5fb-11e6-82f8-989096ac7070 2016-11-29 14:14:00 2016-11-29 14:14:00
10 08020160-b5fb-11e6-82f8-989096ac7070 2016-11-29 14:14:05 2016-11-29 14:14:05
11 0afcfc6e-b5fb-11e6-82f8-989096ac7070 2016-11-29 14:14:10 2016-11-29 14:14:10
12 0df7fddd-b5fb-11e6-82f8-989096ac7070 2016-11-29 14:14:15 2016-11-29 14:14:15
13 10f2fbbc-b5fb-11e6-82f8-989096ac7070 2016-11-29 14:14:20 2016-11-29 14:14:20
14 13ee163d-b5fb-11e6-82f8-989096ac7070 2016-11-29 14:14:25 2016-11-29 14:14:25
15 16e8f939-b5fb-11e6-82f8-989096ac7070 2016-11-29 14:14:30 2016-11-29 14:14:30


3. About timer time setting
(1) Execute once, the format is as follows:
AT 'YYYY-MM-DD HH:MM.SS'/CURRENT_TIMESTAMP { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] }


(2) It has been executed regularly after creation, the format is as follows:
EVERY 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...]


(3) Specify the start time, end time and execution cycle in the following format:
EVERY 1  [HOUR|MONTH|WEEK|DAY|MINUTE|...]

STARTS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1[HOUR|MONTH|WEEK|DAY|MINUTE|...] }

ENDS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] }



4. References
[1] MySQL official website manual
http://dev.mysql.com/doc/refman/5.6/en/create-event.html
[2] MySQL creates timed tasks
http://blog.csdn.net/ mer1234567/article/details/7514855

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326492588&siteId=291194637