当前位置:网站首页>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会直到事务提交才会释放,在做表结构变更的时候,一定要小心不要导致锁住线上查询和更新
边栏推荐
- GFS Distributed File System
- 15 MySQL stored procedures and functions
- poj 2762 Going from u to v or from v to u? (infer whether it is a weak link diagram)
- My colleagues quietly told me that flying Book notification can still play like this
- 开源crm客户关系统管理系统源码,免费分享
- Brushless drive design -- on MOS drive circuit
- el-cascader的使用以及报错解决
- White hat talks about web security after reading 2
- 《牛客刷verilog》Part III Verilog企业真题
- orgchart. JS organization chart, presenting structural data in an elegant way
猜你喜欢
[original] what is the core of programmer team management?
保研笔记一 软件工程与计算卷二(1-7章)
Tips for using pads router
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
妙才周刊 - 8
[classical control theory] summary of automatic control experiment
21. PWM application programming
GFS分布式文件系統
Fiddler Everywhere 3.2.1 Crack
698. 划分为k个相等的子集 ●●
随机推荐
20220703 week race: number of people who know the secret - dynamic rules (problem solution)
Zhongjun group launched electronic contracts to accelerate the digital development of real estate enterprises
GFS分布式文件系统
Huawei simulator ENSP - hcip - MPLS experiment
Asynchronous task Whenall timeout - Async task WhenAll with timeout
C# Linq Demo
有什么不起眼却挣钱的副业?
Neural structured learning 4 antagonistic learning for image classification
保研笔记二 软件工程与计算卷二(13-16章)
CIS基准测试工具kube-bench使用
Comparison between webgl and webgpu [3] - vertex buffer
GFS distributed file system
Spire. PDF for NET 8.7.2
Opencvsharp (C openCV) shape detection and recognition (with source code)
Use CAS instead of synchronized
21. PWM application programming
Russian Foreign Ministry: Japan and South Korea's participation in the NATO summit affects security and stability in Asia
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
The interface of grafana tool displays an error, incluxdb error
【LeetCode】5. Valid palindrome