MySQL Query Timeout

  • 7022168
  • 24-Oct-2017
  • 23-Jan-2020

Environment

Retain 4.x
MySQL 5.5+
MariaDB

Situation

When running a query in MySQL and it takes a long time to process from the command line, the following error is seen:
  • ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

DISCLAIMER:

This knowledge base (KB) article is provided for informational purposes only and as a courtesy service to you, our customer. GWAVA Technical Support does not have any database administration (DBA) expertise, nor does it provide DBA services or support. GWAVA is not responsible for the results of implementing any of the concepts contained in this KB article. Implementation of any of the concepts suggested in this KB article shall be done entirely at your own and sole risk, and GWAVA does not provide any kind of warranties whatsoever resulting from your decision of implementing any of the KB article’s concepts. It is up to you to do any research and to ensure yourself that any implementation and setup of any of the KB article’s concepts in your database system is correctly and properly executed. It is imperative that you have backups of your database system and storage directory before making any implementation. If you don’t have any DBA expertise, you should consult with a DBA expert before any implementation of the KB article’s concepts.  Under no circumstances, shall GWAVA, or any of its employees, be liable, in contract, tort, delict or otherwise, whether negligence is provable or not, for any direct, indirect, incidental, special, punitive, consequential or other damages, loss, cost or liability whatsoever that would result from or are related to the implementation of any of the concepts suggested in the KB article.

To the extent permitted by applicable law, GWAVA shall not be liable to you for any special, consequential, direct, indirect or similar damages, including any loss of data, arising out from migrating any type of messages, attachments, database, metadata in your Retain system to another server and/or location.

Resolution

The timeout can be increased by running the following from within mysql

SET innodb_lock_wait_timeout=100;

Some versions of MySQL and MariaDB do not support this method so an alternative is required:
  • Ensure /etc/my.cnf contains the !include line
# cat /etc/my.cnf | grep -v '#'
[client]

[mysqld]

bind-address    = 127.0.0.1

log-error       = /var/log/mysql/mysqld.log

secure_file_priv = /var/lib/mysql-files

innodb_file_format=Barracuda
innodb_file_per_table=ON

server-id       = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_multi]
mysqld     = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
log        = /var/log/mysqld_multi.log

!includedir /etc/my.cnf.d
  • Create a new file called /etc/my.cnf.d/custom.cnf with the following contents
[mysqld]
innodb_lock_wait_timeout=100
  • Restart the database
# systemctl restart mariadb.service 

Where the timeout is in seconds.  A higher value may be required in some circumstances.

Additional Information

To enter the database administration tool and check the settings

# mysql --user=root --password=<password entered during installation>
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.29-MariaDB SUSE package

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show global variables like '%innodb_lock%';
+--------------------------------+-------+
| Variable_name                  | Value |
+--------------------------------+-------+
| innodb_lock_schedule_algorithm | fcfs  |
| innodb_lock_wait_timeout       | 50    |
| innodb_locking_fake_changes    | OFF   |
| innodb_locks_unsafe_for_binlog | OFF   |
+--------------------------------+-------+
4 rows in set (0.00 sec)

MariaDB [(none)]> exit;
Bye