天天看點

mysqldump 參數--lock-tables淺析

mysqldump有一個參數--lock-tables,以前對這個參數也沒有詳細了解過,直到上次有個網友問“參數lock-tables 是一次性鎖定目前庫的所有表,還是鎖定目前導出表?“ ,之前一直以為隻是鎖定目前導出表,後面看了參數說明後,

-l, --lock-tables   Lock all tables for read.

                      (Defaults to on; use --skip-lock-tables to disable.)

自己也不怎麼确認了,當時就測試了一下。準備一個稍微大一點的庫,如果資料庫太小,那麼可能mysqldum指令一下子就導出了所有庫,很難清晰的看到實驗結果。

執行下面指令做邏輯備份

[root@DB-Server ~]# mysqldump -u root -p --default-character-set=utf8  --opt --extended-insert=false --lock-tables MyDB > db_backup_MyDB.sql

Enter password:

同時立即執行下面指令 

mysqldump 參數--lock-tables淺析

如上截圖,執行mysqldump指令的時候,使用show open tables where in_use >0指令,你會看到MyDB裡面的所有表的In_use的值都為1,意味着是當執行mysqldump指令時,是一次性鎖定目前庫的所有表。而不是鎖定目前導出表。

In_use

The number of table locks or lock requests there are for the table. For example, if one client acquires a lock for a table using LOCK TABLE t1 WRITE, In_use will be 1. If another client issues LOCK TABLE t1 WRITE while the table remains locked, the client will block waiting for the lock, but the lock request causes In_use to be 2. If the count is zero, the table is open but not currently being used. In_use is also increased by the HANDLER ... OPEN statement and decreased by HANDLER ... CLOSE.

參考資料:

https://dev.mysql.com/doc/refman/5.7/en/show-open-tables.html

https://oracle-base.com/articles/mysql/mysql-identify-locked-tables

繼續閱讀