Deep Understanding of MySQL Global Lock and Table Lock

Deep Understanding of MySQL Global Lock and Table Lock

Time:2019-7-6

Preface

According to the range of locks, the locks in MySQL can be roughly divided into global locks, table locks and row locks.

Line locks have been mentioned in previous articles

1. Global Lock

A global lock is to lock the entire database instance. MySQL provides a way to add a global read lock. The command isFlush tables with read lock (FTWRL)鞠柄。

When you need to make the whole library read-only, you can use this command, and then the following statements of other threads will be blocked: data update statements (data addition and deletion), data definition statements (including table building, table structure modification, etc.) and submission statements for updating class transactions.

1.1 Global Lock Use Scenario

A typical use scenario for global locks is mysqldump. Re-ownership

That is to say, every table in the whole library is selected and saved into text.

There used to be a way to ensure that no other threads would update the database through FTWRL, and then backup the entire database. Note that the entire library is completely read-only during the backup process.

The danger of database read-only state:

If you backup on the main repository, you can’t perform updates during the backup period, and the business can basically stop. If you are backing up from the library, the binlog synchronized from the main library cannot be executed from the library during the backup, which will cause the master-slave delay.

Note:The logical backup above is not added--single-transactionparameter

It doesn’t seem very good to add a global lock. But think about it, why do backups lock? Let’s see what’s wrong with unlocking?

1.2 Problems arising from unlocking

For example, mobile card, purchase package information

There are two tables, u_acount (for balance sheet) and u_pricing (tariff package).
Steps:
1. Data user A balance in u_account table: 300
Data User A Package in u_pricing Table: Empty

2. Initiate backup, backup u_account table first in the backup process, Backup this table, then u_account user balance is 300
3. At this time, the set user purchased a tariff package 100, and the purchase was completed, which was written into the u_print package table to purchase successfully, and the data during the backup period.
4. Backup Completion

As a result of the backup, the data in the u_account table has not changed, and the data in the u_price table has recently purchased the tariff package 100.

When this backup file is used to recover data, user A earns 100 yuan. Is the user comfortable? But think about the company’s interests.

That is to say, unlocked, the database backed up by the backup system is not a logical time point, and the data is logically inconsistent.

1.3 Why Global Read Lock (FTWRL)

Some may wonder that mysqldump is the official logical backup tool. When mysqldump uses the parameter single-transaction, a transaction is started before the data is imported to ensure that a consistent snapshot view is obtained. Due to the support of MVCC, the data can be updated normally in this process.

Why do we need FTWRL?
Consistency reading is good, but only if the engine supports this isolation level. For example, for MyISAM, an engine that does not support transactions, if there are updates in the backup process, only the latest data can always be retrieved, then the consistency of backups will be destroyed. At this point, we need to use the FTWRL command.

Therefore, the single-transaction method applies only to libraries where all tables use a transaction engine. If a table uses an engine that does not support transactions, then backup can only be done through the FTWRL method. This is often one of the reasons why DBA requires business developers to use InnoDB instead of MyISAM.

1.4 Global Lock: Two Methods

I. FLUSH TABLES WRITE READ LOCK

2. set global readonly = true

Why not use set global readonly = true since you want to read-only the whole library? It’s true that readonly can also make the whole library read-only, but I would recommend FTWRL for several reasons:

First, in some systems, readonly values are used for other logic, such as judging whether a library is a master or a standby. Therefore, the way to modify global variables has a greater impact, and I do not recommend that you use them.

Second, there are differences in exception handling mechanisms. If the client disconnects abnormally after executing the FTWRL command, MySQL automatically releases the global lock and the entire library returns to a state that can be updated normally. When the whole library is set to read only, if the client has an exception, the database will remain read only, which will cause the whole library to be in an unwritable state for a long time, and the risk is high.

Third, readonly is invalid for super user privileges

Note: Business updates are not only the addition and deletion of data (DML), but also the addition of fields and other operations to modify the table structure (DDL). Either way, after a library is globally locked, you will be locked if you add fields to any table in it.

Even if it’s not locked globally, it’s not easy to add fields. There are table-level locks.

2. Table level locks

There are two kinds of table level locks in MySQL: one is table lock, the other is meta data lock (MDL).

2.1 Table Lock

Lock tables table name read; This table can be read, can not be added or deleted in DDL and dml, can only read table data.

Lock tables table name read; neither read nor write

image

The grammar of table locks is lock tables. Read/write. Similar to FTWRL, unlock tables can be used to actively release locks or automatically release locks when the client is disconnected. It is important to note that the lock tables syntax not only limits the reads and writes of other threads, but also the next operation objects of this thread.

For example, if lock tables T1 read and T2 write are executed in thread A, the statements written by other threads T1 and T2 will be blocked. A t the same time, thread A can only read and write T1 and T2 before unlock tables are executed. Even writing T1 is not allowed, and other tables cannot be accessed naturally.

Table locks are the most common way to handle concurrency when there are no finer-grained locks. For InnoDB, which supports row locks, the lock tables command is generally not used to control concurrency. After all, the impact of locking the entire table is still too large.

2.2 MDL Lock

Another type of table level lock is MDL (metadata lock). MDL does not need to be explicitly used and is automatically added when accessing a table. The function of MDL is to ensure the correctness of reading and writing. You can imagine that if a query is traversing data in a table and another thread changes the table structure and deletes a column during execution, then the query thread will get a result that is different from the table structure.

Therefore, MDL is introduced in MySQL version 5.5. When adding, deleting and modifying a table, MDL read lock is added, and MDL write lock is added when changing the structure of a table.

  • Read locks are not mutually exclusive, so you can have multiple threads to add, delete and modify a table at the same time.
  • The read-write locks and write locks are mutually exclusive to ensure the security of the structure operation of the change table. Therefore, if two threads want to add fields to a table at the same time, one of them will wait for the other to finish executing before executing.

Although MDL locks are added by default, they are a mechanism that you can’t ignore.

For example, in the following example, I often see people fall into this pit: add a field to a small table, causing the whole library to hang.

Surely you know that adding fields to a table, or modifying fields, or indexing, requires scanning the entire table’s data. You must be very careful when operating large watches to avoid any impact on online services. In fact, even small tables can cause problems if they are operated carelessly. Let’s look at the following sequence of operations, assuming that table t is a small table.

image
image

show full processlistView MDL lock details

image

We can see that session A starts first, and then an MDL read lock is added to table t. Because session B also needs MDL read locks, it can be executed normally.

Session C will then be blocked because session A’s MDL read lock has not been released, and session C needs MDL write lock, so it can only be blocked.

It doesn’t matter if only session C itself is blocked, but then all requests for new MDL read locks on table T will also be blocked by session C. As mentioned earlier, all additions, deletions and alterations to tables need to apply for MDL read locks first, and then they are locked, which means that the performance is completely unreadable.

If the queries on a table are frequent and the client has a retry mechanism, that is to say, a new session will be requested again after the timeout, the library’s threads will soon be full.

MDL locks in transactions are applied at the beginning of statement execution, but they are not released immediately after statement completion, but are released after the whole transaction is committed.
Note: In general, line locks have lock timeout time. However, there is no timeout limit for MDL locks, and as long as transactions are not committed, they will always be locked.

2.2.1 How to Solve the MDL Lock

Didn’t that say, commit or roll back the transaction? So we need to find this business.

How to find this transaction, pass through the ____________information_schema.innodb_trxView transaction execution time

# View transactions over 60s
mysql> select * from information_schema.innodb_trx where TIME_TO_SEC(timediff(now(),trx_started))>60\G;
Trx_start indicates when the transaction is executed

# View the current time of the system
mysql> select now();
image

Transaction start time and system now time, look at how long the transaction has been executed.

View the thread ID

image

How to handle the thread ID of this long transaction

First, look at the show full process list; which field of host is in it, and who is actually connected to the database. Example: I have a localhost environment on top, go in commit or / rollback. If it’s not a localhost environment, the program will kill it when it connects.

2.2.2 Funny things happened to me

The last time a DBA asked me, it caused a lot of master-slave latency and said how to solve it.

I said how do you solve the delay? Do you know the specific reasons for the delay?

TA told me that multi-threading was turned on, but the latency was still very large, and it was rarely used.

I said how do you know master-slave delay, need to open multi-threaded replication to solve, TA told me, blogs on the Internet do not say that, I spit out old blood.

Later, I asked TA what the master-slave delays were doing under normal conditions, and Ta told me that the alter table structure had been modified.

Then let TA see if it was caused by MDL lock, let tashow full processlistAt first glance, it is the cause of MDL lock.

Then talk to ta about finding a long business, and then discuss with the developer after finding out what the long business is doing, can’t you kill it?

Note:This is what I really met. People asked me such questions. First of all, you need to know what you did beforehand, and then solve the problem. Basically, you need to know the reason, and then avoid it next time.

There are also online environments, system versions, application versions, problems encountered, and whether you are the same, sometimes do not blindly believe.

2.3 How to add fields to small tables safely?

First of all, we need to solve long transactions. If the transaction is not committed, it will always occupy the MDL lock. In the innodb_trx table of MySQL’s information_schema library, you can find the currently executing transactions. If you happen to have a long transaction executing on your DDL change table, consider suspending the DDL or killing the long transaction first. This is why it is necessary to make DDL changes during the low peak period. Of course, we should also consider what DDL to do, referring to the official online ddl.

2.4 online DDL process

  • Write locks with MDL
  • Degraded to MDL Read Lock
  • Really do DDL
  • Upgrade to MDL Write Lock
  • Release MDL locks

1, 2, 4, 5 have very short execution time if there is no lock conflict. Step 3 takes up most of the DDL time, during which the table can read and write data normally, so it is called “online”

summary

Above is the whole content of this article. I hope the content of this article has some reference value for your study or work. Thank you for your support to developpaer.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末灸促,一起剝皮案震驚了整個(gè)濱河市叛拷,隨后出現(xiàn)的幾起案子准给,更是在濱河造成了極大的恐慌橄务,老刑警劉巖误债,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件票编,死亡現(xiàn)場離奇詭異,居然都是意外死亡蟀架,警方通過查閱死者的電腦和手機(jī)瓣赂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來辜窑,“玉大人钩述,你說我怎么就攤上這事∧滤椋” “怎么了?”我有些...
    開封第一講書人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵职恳,是天一觀的道長所禀。 經(jīng)常有香客問我方面,道長,這世上最難降的妖魔是什么色徘? 我笑而不...
    開封第一講書人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任恭金,我火速辦了婚禮,結(jié)果婚禮上褂策,老公的妹妹穿的比我還像新娘横腿。我一直安慰自己,他們只是感情好斤寂,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開白布耿焊。 她就那樣靜靜地躺著,像睡著了一般遍搞。 火紅的嫁衣襯著肌膚如雪罗侯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,050評(píng)論 1 291
  • 那天溪猿,我揣著相機(jī)與錄音钩杰,去河邊找鬼。 笑死诊县,一個(gè)胖子當(dāng)著我的面吹牛讲弄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播依痊,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼垂睬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了抗悍?” 一聲冷哼從身側(cè)響起驹饺,我...
    開封第一講書人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎缴渊,沒想到半個(gè)月后赏壹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡衔沼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年蝌借,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片指蚁。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡菩佑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出凝化,到底是詐尸還是另有隱情稍坯,我是刑警寧澤,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布,位于F島的核電站瞧哟,受9級(jí)特大地震影響混巧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜勤揩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一咧党、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧陨亡,春花似錦傍衡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至虐急,卻和暖如春箱残,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背止吁。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來泰國打工被辑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人敬惦。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓盼理,卻偏偏與公主長得像,于是被迫代替她去往敵國和親俄删。 傳聞我的和親對(duì)象是個(gè)殘疾皇子宏怔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容