Mysql 锁总结
锁

部分总结参考博客
http://b.codejs.cc/articles/2017/10/23/1508749325215.html
http://blog.csdn.net/cug_jiang126com/article/details/50544728
MyISAM 只支持表锁
show create table crm_member; ##查看引擎
alter table crm_member engine = MyISAM; ##更改表引擎
读锁演示
Session 1 读锁 其它session可以读不可写
lock table crm_member read;
Session 1 锁等待时间设置
select @@global.lock_wait_timeout; ##查看锁等待时间设置
set @@global.lock_wait_timeout = 20; ##设置锁等待时间为20秒
Session 2 查询数据 不影响
MariaDB [member]> select name from crm_member where id = 4;
+------+
| name |
+------+
| 222 |
+------+
1 row in set (0.00 sec)
Session 2 更新数据
MariaDB [member]>
MariaDB [member]> update crm_member set name = '222' where id = 4;
## 进入锁等待
Session 1 查看进程
show processlist;
| 97 | root | localhost | member | Query | 120 | Waiting for table level lock | update crm_member set name = '222' where id = 4 | 0.000 |
## 等待一个表级别的锁
Session 1 释放锁
MariaDB [member]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
Session 2 获得锁 立即更新数据
MariaDB [member]> update crm_member set name = '222' where id = 4;
Query OK, 0 rows affected (3 min 9.38 sec)
Rows matched: 1 Changed: 0 Warnings: 0
Session 2 锁等待超时
MariaDB [member]> update crm_member set name = '222' where id = 4;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
写锁演示
Session 1 添加写锁
MariaDB [member]> lock table crm_member write;
Query OK, 0 rows affected (0.00 sec)
Session 1 添加写锁
MariaDB [member]> lock table crm_member write;
Query OK, 0 rows affected (0.00 sec)
Session 2 读数据
MariaDB [member]> select name from crm_member where id = 4;
Session 1 查看进程
MariaDB [member]> show processlist;
| 109 | root | localhost | member | Query | 2 | Waiting for table metadata lock | select name from crm_member where id = 4 | 0.000 |
## 等待一个元数据的锁
Session 2 读数据 锁等待超时
MariaDB [member]> select name from crm_member where id = 4;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
InnoDB 锁 既支持表锁(与MyISAM相同) 也支持 行锁。
排它锁演示
方式1 Session 1 直接update 其它session不可以读不可写 除非快照读
MariaDB [member]> begin;
Query OK, 0 rows affected (0.00 sec)
MariaDB [member]> update crm_member set name = 'aaaaaa' where id = 4;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
方式2 Session 1 for-update 其它session不可以读不可写 除非快照读
MariaDB [member]> begin;
Query OK, 0 rows affected (0.00 sec)
MariaDB [member]> select name from crm_member where id = 4 for update;
Session 2 方式1 事务方式 开始更新操作相同的记录
MariaDB [member]> begin;
Query OK, 0 rows affected (0.00 sec)
MariaDB [member]> update crm_member set name = 'bbb' where id = 4;
## 进入锁等待
Session 2 方式2 普通更新操作
MariaDB [member]> update crm_member set name = 'bbb' where id = 4;
Session 1 查看进程
MariaDB [member]> show processlist;
+-----+------+---------------------+--------+---------+-------+----------+-------------------------------------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+-----+------+---------------------+--------+---------+-------+----------+-------------------------------------------------+----------+
| 109 | root | localhost | member | Query | 2 | updating | update crm_member set name = 'ccc' where id = 4 | 0.000 |
+-----+------+---------------------+--------+---------+-------+----------+-------------------------------------------------+----------+
## 有一个更新等待
Session 2 锁等待超时
MariaDB [member]> update crm_member set name = 'bbb' where id = 4;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Session 1 查看数据 已经更改
MariaDB [member]> select name from crm_member where id = 4;
+------+
| name |
+------+
| ccc |
+------+
Session 2 读取到的数据没有更改 只能读取未提交版本的数据
MariaDB [member]> select name from crm_member where id = 4;
+------+
| name |
+------+
| bbb |
+------+
Mysql 锁总结的更多相关文章
- mysql锁
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有数 ...
- Mysql锁初步
存储引擎 要了解mysql的锁,就要先从存储引擎说起. 常用存储引擎列表如下图所示: 最常使用的两种存储引擎: Myisam是Mysql的默认存储引擎.当create创建新表时,未指定新表的存储引擎时 ...
- mysql锁表机制及相关优化
(该文章为方便自己查阅,也希望对大家有所帮助,转载于互联网) 1. 锁机制 当前MySQL支持 ISAM, MyISAM, MEMORY (HEAP) 类型表的表级锁,BDB 表支持页级锁,InnoD ...
- MySQL锁系列3 MDL锁
http://www.cnblogs.com/xpchild/p/3790139.html MySQL为了保护数据字典元数据,使用了metadata lock,即MDL锁,保证在并发的情况下,结构 ...
- 01 MySQL锁概述
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O 等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有 ...
- Mysql锁机制介绍
Mysql锁机制介绍 一.概况MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking ...
- MySQL锁等待分析【2】
MySQL锁等待分析[1]中对锁等待的分析是一步一步来的.虽然最后是分析出来了,可是用时是比较长的:理清各个表之间的关系后,得到如下SQL语句,方便以后使用 select block_trx.trx_ ...
- MySQL锁与MVCC
--MySQL锁与MVCC --------------------2014/06/29 myisam表锁比较简单,这里主要讨论一下innodb的锁相关问题. innodb相比oracle锁机制简单许 ...
- MySQL锁总结
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/78 MySQL 锁基础 参考了何登成老师文章的结构MySQL 加 ...
- Mysql锁机制--并发事务带来的更新丢失问题
Mysql 系列文章主页 =============== 刚开始学习 Mysql 锁的时候,觉得 Mysql 使用的是行锁,再加上其默认的可重复读的隔离级别,那就应该能够自动解决并发事务更新的问题.可 ...
随机推荐
- Integer Cache(带你脱坑)
Integer Cache 废话不多说----->直接上代码: public class IntegerDemo { public static void main(String[] args) ...
- Spark-SQL连接Hive
第一步:修个Hive的配置文件hive-site.xml 添加如下属性,取消本地元数据服务: <property> <name>hive.metastore.local< ...
- php服务端接收post的json数据
最近用到ext与PHP交互,ext把json数据post给PHP,但在PHP里面$_post获取不到,$_REQUEST也获取不到,但是通过firedebug看到的请求信息确实是把JSON数据post ...
- P2006 赵神牛的游戏
题目描述 在DNF 中,赵神牛有一个缔造者,他一共有k点法力值,一共有m个技能,每个技能耗费的法力值为a[i],可以造成的伤害为b[i],而boss 的体力值为n,请你求出它放哪个技能,才可以打死bo ...
- qt read excel
void exceladapter::readfile(QString filename, QString sheetname, int colNo){ QSqlDatabase db = QSqlD ...
- iOS圆形图片裁剪,原型图片外面加一个圆环
/** * 在圆形外面加一个圆环 */ - (void)yuanHuan{ //0.加载图片 UIImage *image = [UIImage imageNamed:@"AppIcon1 ...
- (转)关于IC设计的想法 Author :Fengzhepianzhou
一.工具的使用 工欲善其事,必先利其器.我们做IC设计的需要掌握的工具:仿真(vcs.modelsim),综合工具(dc.QS.ISE),时序分析(pt.其他的).以及后端的一些工具,比如astro. ...
- 【HEVC简介】High Level Syntax
参考文献:见<High Efficiency Video Coding (HEVC)>High Level Syntax章节 <HEVC标准介绍.HEVC帧间预测论文笔记>系列 ...
- postgres的强制类型转换与时间函数
一.类型转换postgres的类型转换:通常::用来做类型转换,timestamp到date用的比较多select now()::dateselect now()::varchar 示例1:日期的 ...
- (译)IOS block编程指南 1 介绍
Introduction(介绍) Block objects are a C-level syntactic and runtime feature. They are similar to stan ...