当前位置:网站首页>mysql-全局锁和表锁
mysql-全局锁和表锁
2022-07-05 23:38:00 【阿杜】
MYSQL里面的锁可以分为:全局锁,表级锁,行级锁
1 全局锁
对整个数据库实例加锁
MYSQL提供全局读锁的方法:Flush tables with read lock(FTWRL)
这个命令可以使整个库处于只读状态,使用该命令后后,数据更新语句、数据定义语句和更新类事务的提交语句等操作都会被阻塞
1.1 使用场景
全局逻辑备份
风险:
- 如果主库备份,在备份期间不能更新,业务摆停
- 如果从库备份,备份期间不能执行主库同步的binlog,导致主从延迟
官方自带的逻辑备份工具mysqlddump,当mysqldump 使用参数–single-transaction的时候,会启动一个事务,确保拿到一致性视图。由于MVCC的支持,这个过程中的数据时可以正常更新的
一致性读是好,但前提是引擎要支持这个隔离级别
如果要全库只读,为什么不使用set global readonly=true的方式???
1 在有些系统中,readonly的值会被用做其它逻辑,比如判断主备库。所以修改global变量的方式影响太大
2 在异常处理机制上有差异,如果执行FTWRL命令之后由于客户端发生异常断开,那么MYSQL会自动释放这个全局锁,整个库可以回到正常更新的状态。而将整个库设置为readonly之后,如果客户端发生异常,则数据库会一直保持readonly状态,这样会导致整个库长时间处于不可写状态,风险较高
2 表级锁
MYSQL里面的表级锁有两种,一种是表锁,一种是元数据锁(meta data lock, MDL)
2.1 表锁
表锁的语法: lock tables…read/write
可以使用unlock tables 主动释放锁,也可以在客户端断开的时候自动释放。lock tables语法除了会限制别的线程的读写外,也限定了本线程接下来的操作对象
对于InnoDB这种支持行锁的引擎,一般 不使用lock tables命令来控制并发,毕竟锁住整个表的影响还是很大。
2.2 MDL
不需要显式使用,在访问一个表的时候会自动加上
作用:保证读写的正确性
1 在对一个表做增删改查操作的时候,加MDL读锁
2 对表结构变更操作的时候,加MDL写锁
3 读锁之间不互斥
4 读写锁之间,写锁之间是互斥的,用来保证变更表架构操作的安全性
5 MDL会直到事务提交才会释放,在做表结构变更的时候,一定要小心不要导致锁住线上查询和更新
边栏推荐
- How to get all the values stored in localstorage
- C# 文件与文件夹操作
- Brushless drive design -- on MOS drive circuit
- 俄外交部:日韩参加北约峰会影响亚洲安全稳定
- GFS分布式文件系統
- What if the C disk is not enough? Let's see how I can clean up 25g of temp disk space after I haven't redone the system for 4 years?
- How to improve eloquence
- Attacking technology Er - Automation
- CloudCompare&PCL 点云随机添加噪声
- Common static methods of math class
猜你喜欢
Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布
5. Logistic regression
[original] what is the core of programmer team management?
20220703 周赛:知道秘密的人数-动规(题解)
Open source CRM customer relationship system management system source code, free sharing
动态规划 之 打家劫舍
What if the C disk is not enough? Let's see how I can clean up 25g of temp disk space after I haven't redone the system for 4 years?
The use of El cascader and the solution of error reporting
Huawei simulator ENSP - hcip - MPLS experiment
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
随机推荐
Switching power supply buck circuit CCM and DCM working mode
How to insert data into MySQL database- How can I insert data into a MySQL database?
QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
White hat talks about web security after reading 2
Neural structured learning - Part 2: training with natural graphs
Spire Office 7.5.4 for NET
How to rotate the synchronized / refreshed icon (EL icon refresh)
21. PWM application programming
C# Linq Demo
(4) UART application design and simulation verification 2 - RX module design (stateless machine)
el-cascader的使用以及报错解决
"14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
GFS Distributed File System
Différence entre hors bande et en bande
Spire. PDF for NET 8.7.2
idea 连接mysql ,直接贴配置文件的url 比较方便
Qcombox (rewrite) + qcompleter (auto completion, auto loading the drop-down options of qcombox, setting the background color)
【SQL】各主流数据库sql拓展语言(T-SQL 、 PL/SQL、PL/PGSQL)
总结了 800多个 Kubectl 别名,再也不怕记不住命令了!
LeetCode——Add Binary