当前位置:网站首页>Read mysql45 - a simple understanding of global locks and table locks
Read mysql45 - a simple understanding of global locks and table locks
2022-06-25 16:25:00 【Hurry to Friday】
Global lock
Global lock is to lock the entire database instance ,mysql A global read lock command is provided :Flush tables with read lock (FTWRL)
When you need to keep the entire library in a read-only state , You can use the entire command , When the entire command is executed , All write operations are blocked :
Data update statement ( Data addition, deletion and modification )、 Data definition statement ( Include Build table 、 Modify table structure, etc ) Commit statements for and update class transactions .
The background of global lock is that the database needs to be backed up , At this time FTWRL Command prevents other threads from writing to the database , At this time, the entire library is in a read-only state .
If the backup is not locked, the result is :
Back up before purchasing the course , But only backup A Of account, Will be A The account of 200 Backup , Then back up after purchasing the course course,
After that, if you use the backup data to restore the database , It will lead to A Of account still 200, however coures There will be one more course ; It's not right .
If you back up course, Purchase course , Backup again account, It will lead to course still null, however account Less .
So if the backup is not locked , This will cause problems in the data obtained from the backup data . If during the backup operation , Open transaction , At the end of the backup , Resubmission complete , To ensure that the backup data is correct .
The official logic backup tool is mysqldump. When mysqldump Using parameter –single-transaction When , guide A transaction is started before the data , To ensure a consistent view . And because the MVCC Support for , The data in this process is Can be updated normally .
For the storage engine MylSAM This engine does not support transactions , You need to use it FTWRL The command ensures that the entire library is read-only during backup .
set global readonly=true s You can also make the database read-only , There are two differences :
- , In some systems ,readonly The value of will be used to do other logic , For example, it is used to judge whether a database is the primary database or the standby database library . therefore , modify global The way the variables affect the surface is larger , Not recommended .
- There are differences in exception handling mechanisms . If you execute FTWRL After the command, due to the abnormal disconnection of the client , that MySQL This global lock will be released automatically , The whole library can be updated normally . Instead, set the entire library to readonly after , If the client has an exception , Then the database will remain readonly state , This will lead to the whole The library is not writable for a long time , High risk .
Table lock
There are two kinds of table level locks : One is watch lock ; The other is metadata lock (meta data lock,MDL).
The syntax of table lock is lock tables …read/write . And FTWRL similar , It can be used unlock tables Active release lock , It can also be released automatically when the client is disconnected . We need to pay attention to ,lock tables Syntax will restrict the reading and writing of other threads Outside , It also defines the next operation objects of this thread . for instance , If in a thread A In the implementation of lock tables t1 read, t2 write; This statement , Other threads write t1、 read Write t2 All of the statements will be blocked . meanwhile , Threads A In execution unlock tables Before , It can only be read t1、 Reading and writing t2 Gymnastics do . Linking t1 Not allowed , Naturally, you can't access other tables .
Another type of table level lock is another MDL M (metadata lock) m .MDL You don't need to explicitly use , When accessing a table, it will be Automatically add .
When modifying the table structure , Add or modify fields , When deleting fields , Will add one to the watch MDL Write lock , Do not write data at this time ;
When modifying data , Add data or update data , When deleting data , Add a to the table MDL Read the lock , The data can be read only after the operation is completed .
- Read locks are not mutually exclusive , So you can have multiple threads to add, delete, modify and query a table at the same time .
- Between read lock and write lock 、 Write locks and write locks are mutually exclusive , To ensure the security of the operation to change the structure of the table . therefore , If there are two lines The program should add fields to a table at the same time , One of them can't be executed until the other has finished .
Add fields to a table , Or modify the fields , Or the Caucasian , Need to scan the data of the whole table .

sessionA Start... First , Query data , then sessionB start-up , Query data , next sessionC start-up , Modify table structure , It was blocked at this time , It will lead to sessionD Also blocked .
sessionC The blocking operation may be due to sessionA The read operation of is not over yet . Because read locks and write locks are mutually exclusive , So in sessionA Of MDL Before the read lock is released ,sessionC No MDL Write the lock .
If the query statement of this table is very high frequency , And the client has a retry mechanism , Is to create a new one after the query timeout session Query again , This will cause the thread pool of this library to be full . So when modifying the table structure or data , If there is a long transaction , And never submitted , We need to consider first kill Drop the transaction or suspend the modification operation .
边栏推荐
- Prototype chain analysis
- Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022
- Servlet详解
- Prototype mode
- Go language - lock operation
- In the wechat environment, H5 jumps to the specified page of the applet
- 不要小看了积分商城,它的作用可以很大!
- The style of the mall can also change a lot. DIY can learn about it!
- Precautions for function default parameters (formal parameter angle)
- Beginner bug set
猜你喜欢

Xinlou: Huawei's seven-year building journey of sports health

The release of autok3s v0.5.0 continues to be simple and friendly

Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022

iVX低代码平台系列详解 -- 概述篇(一)

Overall MySQL architecture and statement execution process

error Parsing error: Unexpected reserved word ‘await‘.
Gold three silver four, an article to solve the resume and interview

Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution

AutoK3s v0.5.0 发布 延续简约和友好

Understanding of reflection part
随机推荐
Navicat Premium 15 for Mac(数据库开发工具)中文版
深入理解和把握数字经济的基本特征
Why does golang's modification of slice data affect the data of other slices?
The style of the mall can also change a lot. DIY can learn about it!
What are the reasons why the game industry needs high defense servers?
Hash table, generic
Continuously improve the overall performance of adaoracle Oracle Oracle
Flutter textfield setting can input multiple lines
Constructor Pattern
[Third Party framework] retrofit2 (2) - add point configuration of network access framework
Activation and value transfer of activity
Consumer and producer cases of inter thread synchronization (condition variable)
Error: homebrew core is a shallow clone
User registration, information writing to file
2021, committed to better development
Message format of Modbus (PLC)
Final, override, polymorphic, abstract, interface
Problems caused by using ApplicationContext to render layout
什么是骨干网
Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected