当前位置:网站首页>MySQL 45 lecture learning notes (VI) global lock
MySQL 45 lecture learning notes (VI) global lock
2022-07-04 06:37:00 【Tom Kong】
One . Global lock : Lock the entire database instance
Mysql It provides a way to add global read lock The order is
Flush tables with read lock(FTWRL)
When you need to make the whole library Read only status , You can use this command , Statements from other threads will Blocked
data to update sentence ( Data Additions and deletions ),
data Definition sentence ( Include Build table , Modify table structure etc. )
Update transaction Submit sentence
Typical use scenario of global lock -- Make a full library logical backup
That is the The whole database, every table select Come out and save it as text During the backup process The entire library is read-only , But let The whole library is read-only, which is dangerous
- Main database backup , Update cannot be performed during backup , Business basically stopped
- Backup from the library , During the backup, the slave database cannot synchronize with the master database binlog, It can lead to Master-slave delay
- No locks Words , The library that the backup system backs up is not a logical point in time , This view is logically inconsistent .
The official logic backup tool is mysqldump
When mysqldump, Using parameter -single-transaction When , A transaction will be started before importing data , To ensure a consistent view
because MVCC This data can be updated normally .
Table lock MySql There are two types of lock at the inner table level :
- One is watch lock ,
- One 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 grammar In addition to restricting the reading and writing of other threads Also limited The next operation object of this thread .
- For example , If in a thread A perform lock tables t1 t2 write This statement , Other threads write t1, Reading and writing t2 All of the statements will be blocked
- meanwhile , Threads A In execution unlock tables Previously, you could only read t1, Reading and writing t2 The operation of . Linking t1 Not allowed , Naturally, you can't access other tables .
When there is no finer grained lock , Table locking is the most common way to handle concurrency .
Two . The watch lock is MDL(metadata lock)
- MDL You don't need to explicitly use , When accessing a table, it will be automatically added
- MDL The role of is , Ensure the correctness of reading and writing
- If a query is traversing data in a table , Another thread changes the table structure during execution , Delete a list , Then the result obtained by the query thread does not match the table structure , Definitely not .
- therefore , stay MySQL5.5 Version to introduce MDL, When a table is adding, deleting, modifying, and querying , add MDL Read the lock , When it comes to watch When making structural changes , add MDL Write lock
- Read locks are not mutually exclusive , So you can have multiple threads to add, delete, modify and query a table at the same time
- Read and write locks are mutually exclusive , Write locks are mutually exclusive , To ensure the security of the operation to change the structure of the table . therefore , If two threads want to add fields to a table at the same time , One of them has to wait for the other to finish executing .
Add fields to a small table , Cause the whole library to hang
- In the transaction MDL lock . Apply at the beginning of statement execution , But it will not be released immediately after the statement ends , Instead, it will be released after the whole transaction is committed .
- First of all, we have to deal with long affairs , The transaction does not commit , Will always occupy MDL lock . stay MySQL Of information_schema Library innoDB_trx In the table , You can find the current transaction in progress 、
- You have to DDL The changed table happens to have a long transaction running , Consider suspending DDL perhaps kill Long Affairs
Special scenes
If the table you want to change is a hotspot table , Although the amount of data is small , But the requests are frequent , And you have to add a field , What should you do
- stay alert table Statement to set the waiting time , If the specified waiting time can be obtained MDL It's better to write lock , Don't block the following business statements if you can't get them , Give up first , Then the developer or DBA Repeat the process by retrying the command .
边栏推荐
- C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
- SQL injection SQL lab 11~22
- 198. House raiding
- ABAP:OOALV实现增删改查功能
- 2022年,或許是未來10年經濟最好的一年,2022年你畢業了嗎?畢業後是怎麼計劃的?
- leetcode 310. Minimum Height Trees
- Tar source code analysis 6
- 8. Factory method
- 2022, peut - être la meilleure année économique de la prochaine décennie, avez - vous obtenu votre diplôme en 2022? Comment est - ce prévu après la remise des diplômes?
- Manually page the list (parameter list, current page, page size)
猜你喜欢
QT qtablewidget table column top requirements ideas and codes
Tree DP
List of top ten professional skills required for data science work
4G wireless all network solar hydrological equipment power monitoring system bms110
雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
Uniapp custom environment variables
Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
《ClickHouse原理解析与应用实践》读书笔记(4)
【问题记录】03 连接MySQL数据库提示:1040 Too many connections
leetcode 310. Minimum Height Trees
随机推荐
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
Mysql 45讲学习笔记(七)行锁
STM32 单片机ADC 电压计算
How does the recv of TCP socket receive messages of specified length?
微信小程序使用rich-text中图片宽度超出问题
centos8安装mysql.7 无法开机启动
Code rant: from hard coding to configurable, rule engine, low code DSL complexity clock
Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
Considerations for testing a website
17-18. Dependency scope and life cycle plug-ins
198. House raiding
[Android reverse] function interception (CPU cache mechanism | CPU cache mechanism causes function interception failure)
[problem record] 03 connect to MySQL database prompt: 1040 too many connections
C實現貪吃蛇小遊戲
QT qtablewidget table column top requirements ideas and codes
采用中微BATG135实现IIC数据/指令交互
Mysql 45讲学习笔记(十三)表数据删掉一半,表文件大小不变
Mysql 45讲学习笔记(六)全局锁
uniapp 自定义环境变量