简体   繁体   English

Mysql事件不起作用

[英]Mysql Event Not Working

I have added the following simple test event on my mysql database via phpmyadmin: 我通过phpmyadmin在我的mysql数据库中添加了以下简单的测试事件:

CREATE DEFINER=`root`@`localhost` EVENT `my_event` 
ON SCHEDULE EVERY 1 MINUTE STARTS '2013-05-27 00:00:00' 
ON COMPLETION NOT PRESERVE ENABLE DO 
BEGIN
    UPDATE `test` SET `name`="z";
END

My environment is mac + MAMP Pro. 我的环境是mac + MAMP Pro。 I am expecting to change all rows on my 'test' table with name 'z' within a minute. 我希望在一分钟内更改名为'z'的'test'表中的所有行。 But not happening so. 但事情并非如此。

Do I have to something additional to get my events start working? 我需要额外的东西来让我的活动开始工作吗?

Output of "SHOW PROCESSLIST": 输出“SHOW PROCESSLIST”: 在此输入图像描述

Thanks. 谢谢。

Events are run by the scheduler, which is not started by default. 事件由调度程序运行,默认情况下不会启动。 Using SHOW PROCESSLIST is possible to check whether it is started. 使用SHOW PROCESSLIST可以检查它是否已启动。 If not, run the command 如果没有,请运行该命令

 SET GLOBAL event_scheduler = ON;

to run it. 运行它。

For those wondering how to enable it by default at startup, add the following to your config file (my.ini, my.cnf): 对于那些想知道如何在启动时默认启用它的人,将以下内容添加到配置文件(my.ini,my.cnf):

#Event scheduler can be set to 1 (On), 0 (Off), or Disabled
event_scheduler=1

Restart of the service is required in this case, so if you want minimal disruption, add this to the config file, and then run the SQL: 在这种情况下需要重新启动服务,因此如果您想要最小的中断,请将其添加到配置文件,然后运行SQL:

SET GLOBAL event_scheduler = ON;

That way, it will run for the current process, and if the server is restarted it will still work. 这样,它将运行当前进程,如果服务器重新启动,它仍然可以工作。

Note that this doesn't work if the event_scheduler was set to disabled. 请注意,如果event_scheduler设置为disabled,则此方法无效。 In that case the only option is to restart the service. 在这种情况下,唯一的选择是重新启动服务。

Verify if the event_scheduler is On - execute the following command: 验证event_scheduler是否为On - 执行以下命令:

SHOW PROCESSLIST;

It'll output a table/entries, you must look for an entry with User event_scheduler , and Command Daemon : 它将输出一个表/条目,您必须查找具有User event_schedulerCommand Daemon的条目:

Id           User         Host      db  Command Time    State            Info
22870   event_scheduler localhost   \N   Daemon  23    Waiting for next activation  \N

OR, you can also verify using the following command: 或者,您也可以使用以下命令进行验证:

SELECT @@global.event_scheduler;

The result should be ON , otherwise set it off (will get 0 for the command), as stated in the next section. 结果应为ON ,否则将其设置为关闭(命令将为0 ),如下一节所述。


If you don't have any such entry (as above), you may start the event scheduler using the following command: 如果您没有任何此类条目(如上所述),则可以使用以下命令启动事件调度程序:

 SET GLOBAL event_scheduler = ON;

Once done, you can verify if it has been executed properly using the SHOW PROCESSLIST command, as mentioned above. 完成后,您可以使用SHOW PROCESSLIST命令验证它是否已正确执行,如上所述。

If you want your event_scheduler to startup automatically every time mysql server restarts, anywhere under the [mysqld] section of the my.ini or my.cnf file that you find in /etc/mysql you should place 如果你希望每次mysql服务器重启时你的event_scheduler自动启动,你在/etc/mysql找到的my.inimy.cnf文件的[mysqld]部分下的任何地方都应该放置

[mysqld]

# turning on event_scheduler  
event_scheduler=ON

restart mysql to check if it is running (in command line terminal!) 重启mysql以检查它是否正在运行(在命令行终端!)

sudo service mysql restart

then check your processlist 然后检查您的进程列表

SHOW PROCESSLIST

you can check if your events are running by checking the last time they ran 您可以通过检查上次运行时检查您的事件是否正在运行

SELECT * FROM INFORMATION_SCHEMA.events

Temporal

SET GLOBAL event_scheduler = ON;

Permanent (needs restart) 永久(需要重启)

In your config file (In Ubuntu it's /etc/mysql/mysql.cnf ): 在您的配置文件中(在Ubuntu中它是/etc/mysql/mysql.cnf ):

[mysqld]
event_scheduler = ON

Notes: 笔记:

The event_scheduler variable can have this possible states: event_scheduler变量可以具有以下可能的状态:

  • OFF (or 0 ) (default) (或0 )(默认)
  • ON (or 1 ) ON (或1
  • DISABLED : you cannot use the temporal enabling , you can only change state through the config file and restarting the server 禁用 :您不能使用临时启用 ,您只能通过配置文件更改状态并重新启动服务器

WARNING: Keywords ON / OFF are preferred over their numerical equivalents. 警告:关键字ON / OFF优先于其数字等价物。 And in fact Mysql Workbench doesn't recognize the configuration event_scheduler=1 , it shows as OFF in the Options File section. 事实上, Mysql Workbench无法识别配置event_scheduler=1 ,它在Options File部分显示为OFF Tested in Ubuntu with Mysql Workbench 8.0.17 and Mysql Server 5.7.27 使用Mysql Workbench 8.0.17和Mysql Server 5.7.27在Ubuntu中测试

Although ON and OFF have numeric equivalents, the value displayed for event_scheduler by SELECT or SHOW VARIABLES is always one of OFF , ON , or DISABLED . 尽管ONOFF具有数字等效值,但SELECT或SHOW VARIABLES为event_scheduler显示的值始终为OFFONDISABLED之一 DISABLED has no numeric equivalent. DISABLED没有数字等价物。 For this reason, ON and OFF are usually preferred over 1 and 0 when setting this variable. 因此,设置此变量时, ONOFF通常优先于10

Source: https://dev.mysql.com/doc/refman/5.7/en/events-configuration.html 资料来源: https//dev.mysql.com/doc/refman/5.7/en/events-configuration.html

I would just like to add to this thread. 我想加入这个帖子。 I dumped my database to another server and as a result the definer of my event had no such grant defined for the user. 我将我的数据库转储到另一台服务器,因此我的事件的定义者没有为用户定义这样的授权。 I updated my definer with 我更新了我的定义器

ALTER DEFINER='root'@'localhost' EVENT event.name COMMENT '';

Make sure your definer has the correct PRIVILEGES. 确保您的定义者具有正确的特权。

Remember to add in ' Commit ', after ' DO BEGIN ' or ' DO '. 记得在' DO BEGIN '或' DO '之后添加' Commit '。 Works for me after that. 之后为我工作。

I just figured out that on MariaDB, after adding an event (in my case, it was the first one), you have to restart the event-scheduler 我刚刚在MariaDB上发现,添加一个事件后(在我的情况下,它是第一个),你必须重新启动事件调度程序

 SET GLOBAL event_scheduler = OFF;

and then 然后

 SET GLOBAL event_scheduler = ON;

to make it actually bring the scheduler into "waiting for activation"-state. 使它实际上使调度程序进入“等待激活”状态。

Try 尝试

SET GLOBAL event_scheduler = ON;
DELIMITER $$
CREATE DEFINER=`root`@`db01` EVENT `PRICEALERT_STATUS` 
ON SCHEDULE EVERY 1 DAY STARTS TIMESTAMP(CURRENT_DATE) 
DO BEGIN
// Your Query
END $$
DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM