Detecting locked tables (locked by LOCK TABLE)

前端 未结 8 1673
野趣味
野趣味 2020-11-29 18:46

Is there a way to detect locked tables in MySQL? I mean tables locked by the LOCK TABLE table WRITE/READ command.

(Note that readers interested in

8条回答
  •  青春惊慌失措
    2020-11-29 19:32

    The following answer was written by Eric Leschinki in 2014/15 at https://stackoverflow.com/a/26743484/1709587 (now deleted):

    Mini walkthrough on how to detect locked tables:

    This may prevent the database from enforcing atomicity in the affected tables and rows. The locks were designed to make sure things stay consistent and this procedure will prevent that process from taking place as designed.

    Create your table, insert some rows

    create table penguins(spam int, ham int);
    insert into penguins(spam, ham) values (3, 4);
    

    show open tables:

    show open tables like "penguins"
    

    prints:

    your_database penguins    0   0
    

    Penguins isn't locked, lets lock it:

    LOCK TABLES penguins READ;
    

    Check if it's locked:

    show open tables like "penguins"
    

    Prints:

    your_database, penguins 1, 0
    

    Aha! It is locked! Lets unlock it:

    unlock tables
    

    Now it is unlocked:

    show open tables like "penguins"
    

    Prints:

    your_database penguins    0   0
    

    show all current locks

    show open tables where in_use <> 0
    

    It would be much more helpful if the MySQL developers put this information in a regular table (so I can do a select my_items from my_table where my_clauses), rather than this stripped down 'show table' syntax from system variables.

提交回复
热议问题