Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unlocking tables if thread is lost

Tags:

mysql

http://dev.mysql.com/doc/refman/5.0/en/internal-locking.html

The following is the extract from the documentation.

mysql> LOCK TABLES real_table WRITE, temp_table WRITE; mysql> INSERT INTO real_table SELECT * FROM temp_table; mysql> DELETE FROM temp_table; mysql> UNLOCK TABLES; 

My question is that if I use this logic in the shell script code, and if the thread is lost (not killed) before it reaches "unlock tables" statement, how will I know that some tables are locked and how do I unlock tables manually?

like image 935
shantanuo Avatar asked Mar 20 '12 08:03

shantanuo


People also ask

How do you force unlock a locked table in MySQL?

The correct way to use LOCK TABLES and UNLOCK TABLES with transactional tables, such as InnoDB tables, is to begin a transaction with SET autocommit = 0 (not START TRANSACTION ) followed by LOCK TABLES , and to not call UNLOCK TABLES until you commit the transaction explicitly.

How do you unlock a database table?

Use the UNLOCK TABLE statement in a database that does not support transaction logging to unlock a table that you previously locked with the LOCK TABLE statement. The UNLOCK TABLE statement is an extension to the ANSI/ISO standard for SQL.

What happens when a table is locked?

A locked table remains locked until you either commit your transaction or roll it back, either entirely or to a savepoint before you locked the table. A lock never prevents other users from querying the table. A query never places a lock on a table.


1 Answers

Here's what i do to FORCE UNLOCK FOR some locked tables in MySQL

1) Enter MySQL

mysql -u your_user -p 

2) Let's see the list of locked tables

mysql> show open tables where in_use>0; 

3) Let's see the list of the current processes, one of them is locking your table(s)

mysql> show processlist; 

4) Let's kill one of these processes

mysql> kill put_process_id_here; 
like image 75
Kostyantyn Avatar answered Oct 01 '22 03:10

Kostyantyn