How can I stop a MySQL query if it takes too long?

后端 未结 9 1333
野趣味
野趣味 2020-12-02 09:22

Is it possible to timeout a query in MySQL?

That is, if any query exceeds the time I specify, it will be killed by MySQL and it will return an error instead of waiti

9条回答
  •  有刺的猬
    2020-12-02 09:44

    There is a nice Perl script on CPAN to do just this: http://search.cpan.org/~rsoliv/mysql-genocide-0.03/mysql-genocide

    One only needs to schedule it to run with the proper parameters. Create a CRONtab file /etc/cron.d/mysql_query_timeout to schedule it to run every minute:

    * * * * * root /path/to/mysql-genocide -t 7200 -s -K

    Where 7200 is the maxiumum allowed execution time in seconds. The -s switch filters out all except SELECT queries. The -K switch instructs the script to kill the matching processes.

    The root user should be able to run local mysql tools without authentication otherwise you will need to provide credentials on the command line.

提交回复
热议问题