search
HomeDatabaseMysql Tutorialmysql定时器_MySQL

mysql定时器既是mysql的事件,在实际开发中,我们有时候需要定时去执行一些操作,大部分人通过ScheduledExecutorService类去创建定时,这种如果遇到大数据的更新的时候,运行速度比较忙,这时候我们可以考虑使用mysql定时器去执行SQL脚本文件。

要使用mysql定时器。

首先必须启动调度器“event_scheduler”。

 

查看当前是否已开启事件计划(调度器)有3种方法:

1) SHOW VARIABLES LIKE 'event_scheduler';

2) SELECT @@event_scheduler;

3) SHOW PROCESSLIST;

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

1) SET GLOBAL event_scheduler = 1;

2) SET @@global.event_scheduler = 1;

3) SET GLOBAL event_scheduler = ON;

4) SET @@global.event_scheduler = ON;

其次是创建定时器事件,可以通过sql创建,也可以安装Navicat Premium(mysql客户端)创建。

1)通过sql创建定时器事件:

create event if not exists eventJob
on schedule every 2 second STARTS '2015-07-14 00:00:00'
on completion PRESERVE

ENABLE
do call mypro();

上述sql脚本中“eventJob ”为定时器事件名称,“mypro”为mysql函数或者存储过程。sql脚本表示从 '2015-07-14 00:00:00'开始,每2秒钟执行mypro()函数一次。当然也可以设置结束时间。

2) 通过客户端创建定时器事件:

\

如图中所示,call关键字后是需要执行的函数或者存储过程;状态表示定时器事件的状态,是否启用,enable表示启用,disable表示不启用,可以手动设置也可以通过sql来进行设置; on completion设置为“PRESERVE”。

\

“计划”中标签中,主要是设置定时器的频率和开始结束时间。AT表示,从当前mysql数据库时间开始(这个时间是从启动器启用开始,即当状态为“ENABLE”时),根据一定频率的执行;EVERY是自定义开始时间和结束时间;相对来书EVERY比较灵活,使用方便。

最后,当然是设置定时器事件的状态。

在上一步中,我们无论是sql创建还是客户端创建都可以设置状态,但是当我们没有设置状态或者说关闭定时器事件的时候,就需要执行sql

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

Statement
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
Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

MySQL: How to Add a User RemotelyMySQL: How to Add a User RemotelyMay 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

The Ultimate Guide to MySQL String Data Types: Efficient Data StorageThe Ultimate Guide to MySQL String Data Types: Efficient Data StorageMay 12, 2025 am 12:05 AM

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

MySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMay 11, 2025 am 12:13 AM

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

MySQL: Should I use root user for my product?MySQL: Should I use root user for my product?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL String Data Types Explained: Choosing the Right Type for Your DataMySQL String Data Types Explained: Choosing the Right Type for Your DataMay 11, 2025 am 12:10 AM

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools