Nate Tracy-Amoroso

Home > How to setup a query timeout for mysql 5.7 on Ubuntu 18.04

Open up /etc/mysql/mysql.conf.d/mysqld.cnf and add a max_execution_time line to the mysqld section

# /etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]
# 
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
max_execution_time = 30000 <---- 30s

In general this file is loaded because /etc/mysql/my.cnf includes it. /etc/mysql/conf.d/ is client settings and /etc/mysql/mysql.conf.d/ is server settings (like timeout).

# /etc/mysql/my.cnf

# The MySQL database server configuration file.
#
# ...
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/ <---

I like this because occassionally some user will come up with an API request that triggers a query which takes hours and this will lookup the whole server until I fix it manually. Even a long timeout like 30s means these users don’t take down the service for very long.