Unlocking tables if thread is lost

Mysql

Mysql Problem Overview


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?

Mysql Solutions


Solution 1 - Mysql

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;

Solution 2 - Mysql

>how will I know that some tables are locked?

You can use SHOW OPEN TABLES command to view locked tables.

>how do I unlock tables manually?

If you know the session ID that locked tables - 'SELECT CONNECTION_ID()', then you can run KILL command to terminate session and unlock tables.

Solution 3 - Mysql

With Sequel Pro:

Restarting the app unlocked my tables. It resets the session connection.

NOTE: I was doing this for a site on my local machine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshantanuoView Question on Stackoverflow
Solution 1 - MysqlKostyantynView Answer on Stackoverflow
Solution 2 - MysqlDevartView Answer on Stackoverflow
Solution 3 - MysqlkevnkView Answer on Stackoverflow