search
HomeDatabaseMysql TutorialMySQL 事件调度器的操作流程

MySQL 事件调度器的操作流程

Jun 07, 2016 pm 04:14 PM
mysqlmaineventintroduceoperatearticleprocessScheduling

下面的文章主要介绍的是MySQL 事件调度器(Event Scheduler),我们这次试验的事件调度器是在 MySQL 5.1 的环境中进行的,其新增另一个相关功能,可以用来作为一个新的定时任务调度器。 取代部分原先只能用操作系统任务调度器才能完成的定时功能。 一、概述 事

下面的文章主要介绍的是MySQL 事件调度器(Event Scheduler),我们这次试验的事件调度器是在 MySQL 5.1 的环境中进行的,其新增另一个相关功能,可以用来作为一个新的定时任务调度器。

取代部分原先只能用操作系统任务调度器才能完成的定时功能。
一、概述

事件调度器是在 MySQL 5.1 中新增的另一个特色功能,可以作为定时任务调度器,取代部分原先只能用操作系统任务调度器才能完成的定时功>能。例如,Linux 中的 crontabe 只能精确到每分钟执行一次,而 MySQL事件调度器则可以实现每秒钟执行一个任务,这在一些对实时性要>求较高的环境下就非常实用了。

事件调度器是定时触发执行的,在这个角度上也可以称作是"临时的触发器"。触发器只是针对某个表产生的事件执行一些语句,而事件调度器则是在某一个(间隔)时间执行一些语句。

事件是由一个特定的线程来管理的,也就是所谓的"事件调度器"。启用MySQL 事件调度器后,拥有 SUPER 权限的账户执行 SHOW PROCESSLIST 就可以看到这个线程了。通过设定全局变量event_scheduler 的值即可动态的控制事件调度器是否启用。

<ol class="dp-xml">
<li class="alt"><span><span>(root:localhost:)test</span><span class="tag">></span><span> SET GLOBAL </span><span class="attribute">event_scheduler</span><span> = </span><span class="attribute-value">ON</span><span>;  </span></span></li>
<li>
<span>(root:localhost:)test</span><span class="tag">></span><span> show processlist\G </span>
</li>
</ol>

4. row

<ol class="dp-xml">
<li class="alt"><span><span>Id: 46147  </span></span></li>
<li><span>User: event_scheduler  </span></li>
<li class="alt"><span>Host: localhost  </span></li>
<li><span>db: NULL  </span></li>
<li class="alt"><span>Command: Daemon  </span></li>
<li><span>Time: 1  </span></li>
<li class="alt"><span>State: Waiting on empty queue  </span></li>
<li><span>Info: NULL  </span></li>
</ol>


如上,该线程的所有者是 event_scheduler。

二、应用案例

实现MySQL 事件调度器本,案例是利用 event scheduler 的特性,每秒钟调用一次存储过程,用于判断 SLAVE 是否正常运行,如果发现 SLAVE 关闭了,忽略 0 次错误,然后重新启动 SLAVE。

首先创建存储过程

<ol class="dp-xml">
<li class="alt"><span><span>delimiter //  </span></span></li>
<li><span>create procedure `Slave_Monitor`()  </span></li>
<li class="alt"><span>begin  </span></li>
<li><span>SELECT VARIABLE_VALUE INTO @SLAVE_STATUS   </span></li>
<li class="alt"><span>FROM information_schema.GLOBAL_STATUS   </span></li>
<li>
<span>WHERE </span><span class="attribute">VARIABLE_NAME</span><span>=</span><span class="attribute-value">'SLAVE_RUNNING'</span><span>;  </span>
</li>
<li class="alt"><span>IF ('ON' != @SLAVE_STATUS) THEN  </span></li>
<li>
<span>SET GLOBAL </span><span class="attribute">SQL_SLAVE_SKIP_COUNTER</span><span>=</span><span class="attribute-value">0</span><span>;  </span>
</li>
<li class="alt"><span>SLAVE START;  </span></li>
<li><span>END IF;  </span></li>
<li class="alt"><span>end; //  </span></li>
<li><span>delimiter ;  </span></li>
</ol>

由于存储过程中无法调用类似 SHOW SLAVE STATUS 这样的语句,因此无法得到确切的复制错误信息和错误代码,不能进一步的处理 SLAVE 停止的各种情况。

接着,创建任务

<ol class="dp-xml">
<li class="alt"><span><span>CREATE EVENT IF NOT EXISTS `Slave_Monitor`  </span></span></li>
<li><span>ON SCHEDULE EVERY 5 SECOND  </span></li>
<li class="alt"><span>ON COMPLETION PRESERVE  </span></li>
<li><span>DO  </span></li>
<li class="alt"><span>CALL Slave_Monitor();  </span></li>
</ol>

创建了一个任务,每 5秒钟 执行一次,任务结束后依旧保留该任务,而不是删除。当然了,在本例中的任务不会结束,除非将它手动禁止了。

如果在运行中想要临时关闭一下某个任务,执行 ALTER EVENT 语句即可:

<ol class="dp-xml">
<li class="alt"><span><span>(root:localhost:)test</span><span class="tag">></span><span> alter event `Slave_Monitor` ON   </span></span></li>
<li><span>COMPLETION PRESERVE DISABLE;  </span></li>
<li class="alt">
<span>(root:localhost:)test</span><span class="tag">></span><span> alter event `Slave_Monitor` ON   </span>
</li>
<li><span>COMPLETION PRESERVE ENABLE; </span></li>
</ol>

以上的相关内容就是对MySQL 事件调度器的介绍,望你能有所收获。


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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment