{"id":2088,"date":"2023-04-01T21:56:17","date_gmt":"2023-04-01T13:56:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2088"},"modified":"2023-04-06T14:49:51","modified_gmt":"2023-04-06T06:49:51","slug":"how-is-phantom-reading-resolved-in-mysql-innodb","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/how-is-phantom-reading-resolved-in-mysql-innodb\/","title":{"rendered":"\u5e7b\u8bfb\u5728 MySQL InnoDB \u4e2d\u662f\u88ab\u5982\u4f55\u89e3\u51b3\u7684\uff1f"},"content":{"rendered":"<p>\u5728MySQL\u4e8b\u52a1\u521d\u8bc6\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u5230\u4e0d\u540c\u7684\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u4f1a\u5f15\u53d1\u4e0d\u540c\u7684\u95ee\u9898\uff0c\u5982\u5728 RR \u7ea7\u522b\u4e0b\u4f1a\u51fa\u73b0\u5e7b\u8bfb\u3002\u4f46\u5982\u679c\u5c06\u5b58\u50a8\u5f15\u64ce\u9009\u4e3a InnoDB \uff0c\u5728 RR \u7ea7\u522b\u4e0b\uff0c\u5e7b\u8bfb\u7684\u95ee\u9898\u5c31\u4f1a\u88ab\u89e3\u51b3\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f1a\u5148\u4ecb\u7ecd\u4ec0\u4e48\u662f\u5e7b\u8bfb\u3001\u5e7b\u8bfb\u4f1a\u5e26\u6765\u5f15\u8d77\u90a3\u4e9b\u95ee\u9898\u4ee5\u53ca InnoDB \u89e3\u51b3\u5e7b\u8bfb\u7684\u601d\u8def\u3002<\/p>\n<blockquote>\n<p>\u5b9e\u9a8c\u73af\u5883\uff1aRR\uff0cMySQL 5.7.27<\/p>\n<\/blockquote>\n<p>\u4e3a\u4e86\u540e\u9762\u5b9e\u9a8c\u65b9\u4fbf\uff0c\u5047\u8bbe\u5728\u6570\u636e\u5e93\u4e2d\u6709\u8fd9\u6837\u4e00\u5f20\u8868\u4ee5\u53ca\u6570\u636e\uff0c\u6ce8\u610f\u8fd9\u91cc\u7684 d \u5217\u5e76\u6ca1\u7d22\u5f15\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-sql\">CREATE TABLE `t` (\n  `id` int(11) NOT NULL,\n  `c` int(11) DEFAULT NULL,\n  `d` int(11) DEFAULT NULL,\n  PRIMARY KEY (`id`),\n  KEY `c` (`c`)\n) ENGINE=InnoDB;\n\ninsert into t values(0,0,0),(5,5,5),\n(10,10,10),(15,15,15),(20,20,20),(25,25,25);<\/code><\/pre>\n<h2>\u4ec0\u4e48\u662f\u5e7b\u8bfb\uff1f<\/h2>\n<p>\u5e7b\u8bfb\uff1a\u662f\u6307\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u524d\u540e\u4e24\u6b21\u67e5\u8be2\u76f8\u540c\u8303\u56f4\u65f6\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u4e0d\u4e00\u81f4\uff0c\u540e\u4e00\u6b21\u67e5\u8be2\u5230\u65b0\u63d2\u5165\u7684\u884c\u3002<\/p>\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5728 RR \u7ea7\u522b\u4e0b\uff0c\u666e\u901a\u7684\u8bfb\u662f\u5feb\u7167\u8bfb\uff08\u4e00\u81f4\u6027\u8bfb\uff09\uff0c\u6240\u4ee5\u5e7b\u8bfb\u4ec5\u53d1\u751f\u5728\u5f53\u524d\u8bfb\u7684\u57fa\u7840\u4e0a\u3002<\/p>\n<p>\u4e3e\u4f8b\u6765\u8bf4\uff1a<\/p>\n<ul>\n<li><code>select * from t where d=0<\/code>\u5c31\u662f\u5feb\u7167\u8bfb\uff0c\u5bf9\u4e8e\u540c\u4e00\u4e2a\u4e8b\u52a1\u6765\u8bf4\uff0c\u6bcf\u6b21\u8bfb\u5230\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684<\/li>\n<li><code>select * from t where d=0 lock in share mode<\/code>\u6216<code>select * from t where d=0 for update<\/code>\u5c31\u662f\u5f53\u524d\u8bfb\uff0c\u603b\u662f\u8bfb\u53d6\u5f53\u524d\u6570\u636e\u884c\u7684\u6700\u65b0\u7248\u672c\uff0c\u5173\u4e8e\u6570\u636e\u884c\u7248\u672c\u95ee\u9898\u53ef\u53c2\u8003\u4e8b\u52a1\u7a76\u7adf\u6709\u6ca1\u6709\u88ab\u9694\u79bb<\/li>\n<\/ul>\n<p>\u56de\u5230\u5e7b\u8bfb\uff0c\u6709\u5982\u4e0b Session\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin:<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where d=5 for update;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">insert into t values(1,1,5);<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where d=5 for update;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">commit;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Session A \u7b2c\u4e00\u4e2a select \u7ed3\u679c\u662f\uff1a(5,5,5)\uff0c\u7b2c\u4e8c\u4e2a select \u7ed3\u679c\u662f(1,1,5)\u548c(5,5,5)\u3002\u7531\u4e8e\u4e24\u6b21\u5f53\u524d\u8bfb\u7684\u7ed3\u679c\u4e0d\u4e00\u81f4\uff0c\u8fd9\u5c31\u8868\u660e\u51fa\u73b0\u4e86\u5e7b\u8bfb\u3002\u6709\u4e00\u70b9\u9700\u8981\u8bf4\u660e\uff0c\u4f60\u5728\u5c1d\u8bd5 Session B \u4f1a\u88ab\u963b\u585e\uff0c\u56e0\u4e3a\u5728 RR \u7ea7\u522b\u4e0b\uff0c\u9ed8\u8ba4\u5df2\u7ecf\u5c06\u5e7b\u8bfb\u7684\u95ee\u9898\u7684\u89e3\u51b3\uff0c\u8fd9\u91cc\u4ec5\u4f5c\u4e3a\u601d\u8003\u7684\u8fc7\u7a0b\u3002<\/p>\n<h2>\u5e7b\u8bfb\u5e26\u6765\u7684\u95ee\u9898\uff1f<\/h2>\n<p>\u4e3a\u4e86\u66f4\u597d\u7684\u5c55\u73b0\u5e7b\u8bfb\u5e26\u6765\u7684\u95ee\u9898\uff0c\u4e3a Session A\uff0cB \u6dfb\u52a0\u4e00\u6761 SQL\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin:<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where d=5 for update;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">update t set d=100 where d=5;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">insert into t values(1,1,5);<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">update t set d=5 where id=1;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where d=5 for update;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">commit;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ol>\n<li>\u7834\u574f\u4e86\u8bed\u4e49<\/li>\n<\/ol>\n<p>\u65b0\u7684 Session B \u4e2d\uff0c\u9664\u4e86\u6dfb\u52a0\u4e00\u6761\u65b0\u8bb0\u5f55\u5916\uff0c\u8fd8\u4fee\u6539\u4e86\u65b0\u8bb0\u5f55\u7684 d \u503c\u3002\u8fd9\u5c31\u7834\u574f\u4e86 A \u7684\u8bed\u4e49\uff0cSession A \u7684\u76ee\u7684\u5c31\u662f\u9501\u4f4f\u6240\u6709 d=5 \u7684\u884c\uff0c\u4e0d\u8ba9\u5176\u88ab\u64cd\u4f5c\u3002<\/p>\n<ol start=\"2\">\n<li>\u6570\u636e\u4e00\u81f4\u6027\u7684\u95ee\u9898<\/li>\n<\/ol>\n<p>\u9501\u7684\u5b58\u5728\u5c31\u662f\u4e3a\u4e86\u907f\u514d\u5728\u5e76\u53d1\u6761\u4ef6\u4e0b\uff0c\u51fa\u73b0\u7684\u6570\u636e\u4e00\u81f4\u6027\u7684\u95ee\u9898\u3002\u8fd9\u91cc\u6211\u4eec\u770b\u4e0b A\uff0cB \u63d0\u4ea4\u540e\u6570\u636e\u5e93\u7684\u6570\u636e\u7ed3\u679c\uff1a<\/p>\n<p>id=1 \u63d2\u5165\u4e86\u4e00\u6761\u65b0\u7684\u8bb0\u5f55\uff0cid=5 \u7684\u8bb0\u5f55 d \u88ab\u4fee\u6539\u6210 100<\/p>\n<pre><code>(0,0,0),\n(1,5,5);\n(5,5,100),\n(10,10,10),\n(15,15,15),\n(20,20,20),\n(25,25,25);<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u7ed3\u679c\u770b\u4f3c\u6ca1\u6709\u95ee\u9898\uff0c\u8fd9\u91cc\u770b\u4e0b\u751f\u6210\u7684 binlog \u7684\u6267\u884c\u903b\u8f91\uff0c\u7531\u4e8e Session B \u5148\u63d0\u4ea4\uff0c\u6240\u4ee5\u5bf9\u5e94\u8bed\u53e5\u5728\u524d\uff1a<\/p>\n<pre><code class=\"language-sql\"># Session B \u5148\u6267\u884c\ninsert into t values(1,1,5); \/*(1,1,5)*\/\nupdate t set c=5 where id=1; \/*(1,5,5)*\/\n\n# Session A \u540e\u6267\u884c\nupdate t set d=100 where d=5; \/*\u6240\u6709d=5\u7684\u884c\uff0cd\u6539\u6210100*\/<\/code><\/pre>\n<p>\u5982\u679c\u62ff\u6b64 binlog \u8fdb\u884c\u6570\u636e\u6062\u590d\uff0c\u53ef\u89c1 id=1 \u7684\u8fd9\u6837\u884c\u88ab\u4fee\u6539\u6210\u4e86(1,5,100)\uff0c\u8fd9\u5c31\u51fa\u73b0\u4e86\u6570\u636e\u4e00\u81f4\u6027\u7684\u95ee\u9898\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u51b3\u5e7b\u8bfb\uff1f<\/h2>\n<p>\u5bf9\u4e8e<code>select * from t where d=5 for update;<\/code>\u6765\u8bf4\uff0c\u9501\u4f4fd=5\u5bf9\u5e94\u7684\u884c\u6216\u8005\u9501\u4f4f\u626b\u63cf\u8fc7\u7a0b\u4e2d\u6240\u6709\u7684\u884c\u90fd\u662f\u6ca1\u6709\u7528\u7684\uff0c\u56e0\u4e3a\u63d2\u5165\u5e76\u4e0d\u5f71\u54cd\u4e4b\u524d\u884c\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5 InnoDB \u4e3a\u4e86\u89e3\u51b3\u5e7b\u8bfb\uff0c\u5f15\u5165\u4e86\u65b0\u7684\u9501 &#8211; \u95f4\u9699\u9501\u3002<\/p>\n<p>\u95f4\u9699\u9501\uff0c\u4f1a\u5c06\u884c\u4e4b\u95f4\u7684\u7a7a\u9699\u9501\u4f4f\u3002\u6bd4\u5982\uff0c\u521d\u59cb\u5316\u662f\u63d2\u5165\u7684 6 \u4e2a\u503c\uff0c\u5c31\u4f1a\u4ea7\u751f 7 \u4e2a\u7a7a\u9699\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL\u5e7b\u8bfb-\u95f4\u9699\u9501.png\" alt=\"MySQL\u5e7b\u8bfb-\u95f4\u9699\u9501\" \/><\/p>\n<p>\u5f53\u518d\u6267\u884c<code>select * from t where d=5 for update;<\/code>\u65f6\uff0c\u4e0d\u4f46\u4f1a\u5c06\u5168\u8868\u7684\u6570\u636e\u884c\u9501\u4f4f\uff0c\u8fd8\u4f1a\u5c06\u95f4\u9699\u9501\u4f4f\u3002<\/p>\n<blockquote>\n<p>\u8fd9\u91cc\u63d0\u4e00\u4e0b\uff0c\u7531\u4e8e d \u6ca1\u6709\u7d22\u5f15\uff0c\u6240\u4ee5\u8d70\u5168\u8868\u626b\u63cf\uff0c\u4f1a\u5c06\u6574\u4e2a\u8868\u9501\u4f4f\u3002<\/p>\n<\/blockquote>\n<p>\u884c\u9501\uff08Record Lock\uff09\u6309\u7167\u7c7b\u578b\u5206\u4e3a\u8bfb\u9501\u548c\u5199\u9501\uff0c\u5e76\u4e14\u884c\u9501\u4e0e\u884c\u9501\u5728\u4e0d\u540c\u7684\u4e8b\u52a1\u95f4\u662f\u4e92\u65a5\u7684\u3002<\/p>\n<p>\u4f46\u95f4\u9699\u9501\u4e0d\u540c\uff0c\u6b63\u7531\u4e8e\u5b83\u89e3\u51b3\u7684\u662f\u5e7b\u8bfb\u63d2\u5165\u7684\u95ee\u9898\uff0c\u6240\u4ee5<strong>\u95f4\u9699\u9501\u4ec5\u4ec5\u5bf9\u63d2\u5165\u64cd\u4f5c\u672c\u8eab\u4e92\u65a5<\/strong>\uff0c\u4e0d\u540c\u4e8b\u52a1\u4e4b\u95f4\u7684\u95f4\u9699\u9501\u5e76\u4e0d\u4e92\u65a5\u3002<\/p>\n<p>\u6bd4\u5982\u4e0b\u9762\u8fd9\u4e24\u4e2a\u4e8b\u52a1\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin:<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where c=7 lock in share mode;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">update t set d=100 where d=5;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">begin;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">select * from t where c=7 lock in share mode;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7531\u4e8e c=7 \u8fd9\u6761\u8bb0\u5f55\u5e76\u4e0d\u5b58\u5728\uff0c\u51fa\u4e8e\u5171\u540c\u7684\u76ee\u7684\uff0c\u9632\u6b62\u5176\u4ed6\u503c\u7684\u63d2\u5165\u3002Session B \u4e0d\u4f1a\u88ab\u963b\u585e\u3002Session A \u548c Session B \u90fd\u4f1a\u4e3a\u5176\u52a0\u4e0a(5,10)\u7684\u95f4\u9699\u9501\u3002<\/p>\n<p>\u4e3a\u4e86\u52a0\u9501\u65f6\u7684\u65b9\u4fbf\uff0c\u95f4\u9699\u9501\u548c\u884c\u9501\u7684\u5408\u96c6\u79f0\u4e3a next-key lock\u3002\u884c\u9501\u9501\u4f4f\u7684\u662f\u5b58\u5728\u7684\u8bb0\u5f55\u884c\uff0c\u95f4\u9699\u9501\u9501\u4f4f\u7684\u662f\u884c\u4e4b\u95f4\u7684\u7a7a\u9699\u3002\u800c next-key lock \u9501\u4f4f\u7684\u662f\u4e24\u8005\u4e4b\u548c\uff0c\u6bd4\u5982<code>select * from t for update<\/code>\u9501\u4f4f\u7684\u5c31\u662f <code>(-\u221e,0]<\/code>\u3001<code>(0,5]<\/code>\u3001<code>(5,10]<\/code>\u3001<code>(10,15]<\/code>\u3001<code>(15,20]<\/code>\u3001<code>(20, 25]<\/code>\u3001<code>(25, +supremum]<\/code>\u3002<\/p>\n<blockquote>\n<p>(-\u221e,0]\uff0c\u7531\u95f4\u9699\u9501 (-\u221e,0) \u548c\u884c\u9501 0 \u7ec4\u6210\uff0c\u5176\u4ed6\u7c7b\u4f3c<br \/>\n+supremum \u8868\u793a InnoDB \u7ed9\u6bcf\u4e2a\u7d22\u5f15\u52a0\u4e86\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u6700\u5927\u503c<\/p>\n<\/blockquote>\n<h2>next -key lock \u5f71\u54cd\u5e76\u53d1\u600e\u4e48\u529e\uff1f<\/h2>\n<p>\u95f4\u9699\u9501\u7684\u5f15\u5165\uff0c\u867d\u7136\u89e3\u51b3\u4e86\u5e7b\u8bfb\u7684\u95ee\u9898\uff0c\u4f46\u540c\u65f6\u4e5f\u964d\u4f4e\u4e86\u5e76\u53d1\u5ea6\u3002<\/p>\n<p>\u6bd4\u5982\u4e0b\u9762\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u9501\u4f4f\u4e00\u884c\uff0c\u5982\u679c\u8be5\u884c\u4e0d\u5b58\u5728\u5c31\u63d2\u5165\u5426\u5219\u5c31\u66f4\u65b0\uff1a<\/p>\n<pre><code class=\"language-sql\">begin;\nselect * from t where id=N for update;\n\n\/*\u5982\u679c\u884c\u4e0d\u5b58\u5728*\/\ninsert into t values(N,N,N);\n\/*\u5982\u679c\u884c\u5b58\u5728*\/\nupdate t set d=N set id=N;\n\ncommit;<\/code><\/pre>\n<p>\u5f53\u67e5\u8be2\u4e00\u6761\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\u65f6\uff0c\u4f1a\u7ed9\u6240\u5728 id \u7684\u95f4\u9699\u52a0\u4e0a\u95f4\u9699\u9501\u3002\u5047\u5982\u540c\u65f6\u51fa\u73b0\u5e76\u53d1\u7684\u60c5\u51b5\uff0c\u7531\u4e8e\u95f4\u9699\u9501\u4e4b\u95f4\u4e0d\u51b2\u7a81\uff0c\u4e24\u4e2a\u4e8b\u52a1\u90fd\u4f1a\u52a0\u4e0a\u95f4\u9699\u9501\u3002\u4e4b\u540e\u6267\u884c\u63d2\u5165\u65f6\uff0c\u6bcf\u4e2a\u4e8b\u52a1\u7684\u63d2\u5165\u64cd\u4f5c\u4e0e\u53e6\u5916\u4e8b\u52a1\u7684\u95f4\u9699\u9501\u51fa\u73b0\u51b2\u7a81\uff0c\u8fdb\u800c\u5f15\u53d1\u6b7b\u9501\u3002<\/p>\n<p>\u7531\u6b64\u770b\u89c1\uff0c\u95f4\u9699\u9501\u7684\u5f15\u5165\u5bfc\u81f4\u540c\u6837\u7684\u8bed\u53e5\u9501\u4f4f\u66f4\u5927\u7684\u8303\u56f4\uff0c\u964d\u4f4e\u4e86\u5e76\u53d1\u5ea6\u3002<\/p>\n<p>\u5047\u5982\u4e1a\u52a1\u9700\u6c42\u5e76\u4e0d\u9700\u8981\u95f4\u9699\u9501\u600e\u4e48\u529e\uff0c\u8fd9\u65f6\u53ef\u4ee5\u5c06\u9694\u79bb\u7ea7\u522b RC\uff0c\u5728\u6b64\u7ea7\u522b\u4e0b\u5c31\u4e0d\u5b58\u5728\u95f4\u9699\u9501\u4e86\u3002\u7531\u6b64\u5f15\u51fa\u4e00\u4e2a\u95ee\u9898\uff0c\u4e3a\u4ec0\u4e48\u4e00\u822c\u5728 RC \u4e0b\uff0cbinlog \u7684\u683c\u5f0f\u8981\u8bbe\u7f6e\u6210 row \u5462\uff1f<\/p>\n<h2>\u4e3a\u4ec0\u4e48 \u5728 RC \u7ea7\u522b\u4e0b\uff0cbinlog \u683c\u5f0f\u8981\u8bbe\u7f6e\u6210 row\uff1f<\/h2>\n<p>\u5148\u6765\u770b\u4e0b binlog \u7684\u4e09\u79cd\u683c\u5f0f\uff1a<\/p>\n<ul>\n<li><code>--binlog-format=STATEMENT<\/code>\uff1a\u5728 Master \u5411 Slave \u540c\u6b65\u65f6\uff0c\u4f1a\u4ee5\u539f\u751f\u7684 SQL \u8bed\u53e5\u8fdb\u884c\u540c\u6b65\u3002<\/li>\n<li><code>--binlog-format=ROW<\/code>\uff1aMaster \u4f1a\u628a\u88ab\u64cd\u4f5c\u540e\u7684\u8868\u4e2d\u7684\u884c\u8bb0\u5f55\u5728\u65e5\u5fd7\u4e2d\uff0c \u5411 Slave \u540c\u6b65\u3002\u7b80\u5355\u6765\u8bf4\u540c\u6b65\u7684\u5c31\u662f\u8868\u4e2d\u7684\u6570\u636e\u3002<\/li>\n<li><code>--binlog-format=MIXED<\/code>\uff1a\u9ed8\u8ba4\u4f1a\u4ee5 STATEMENT \u7684\u65b9\u5f0f\u8bb0\u5f55\uff0c\u4f46\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u81ea\u52a8\u7684\u5207\u6362\u6210 ROW \u65b9\u5f0f\uff0c\u6bd4\u5982\u6267\u884c\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u51fd\u6570 UUID<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u91c7\u7528\u53cd\u8bc1\u6cd5\uff0c\u5982\u679c\u5728 RC \u7ea7\u522b\u4e0b\uff0c\u5c06 binlog \u7684\u683c\u5f0f\u8bbe\u7f6e\u6210 Statement \u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/p>\n<p>\u8fd8\u662f\u4f7f\u7528\u4e4b\u524d RR \u7ea7\u522b\u4e0b\u5e7b\u8bfb\u7684\u4f8b\u5b50\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin:<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">update t set d=100 where d=5;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">insert into t values(1,1,5);<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">update t set d=5 where id=1;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">commit;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0cBinlog \u65e5\u5fd7\u4e2d Session B \u5148\u6267\u884c\uff0cSession A \u540e\u6267\u884c\uff0cA \u4f1a\u628a id=1 \u4e2d d \u7684\u503c\u6539\u4e3a 100\uff0c\u51fa\u73b0\u4e86 binlog \u548c \u6570\u636e\u5e93\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u73b0\u8c61\u3002<\/p>\n<p>\u800c\u57fa\u4e8e ROW \u683c\u5f0f\u5219\u4e0d\u540c\uff0cbinlog \u65e5\u5fd7\u4e2d\u8bb0\u5f55\u7684\u662f\u88ab\u64cd\u4f5c\u540e\u7684\u6570\u636e\uff0c\u4e0d\u662f\u91cd\u65b0\u6267\u884c SQL \u81ea\u7136\u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5e7b\u8bfb\u7684\u95ee\u9898\uff0c\u77e5\u9053\u4e86 InnoDB \u4e3a\u4e86\u5728 RR \u7ea7\u522b\u4e0a\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u5f15\u5165\u4e86\u95f4\u9699\u9501\u3002\u5e76\u77e5\u9053\u4e86\u95f4\u9699\u9501\u4f1a\u964d\u4f4e\u5e76\u53d1\u7387\uff0c\u589e\u52a0\u6b7b\u9501\u60c5\u51b5\u7684\u53d1\u751f\u3002\u8fd8\u4e86\u89e3\u5230 next-key lock \u5176\u5b9e\u5c31\u662f\u884c\u9501\uff08Record Lock\uff09\u548c\u95f4\u9699\u9501\u7684\u5408\u96c6\u3002<\/p>\n<p>\u5728\u4e1a\u52a1\u4e0d\u9700\u8981 RR \u652f\u6301\u4e0b\uff0c\u5982\u679c\u60f3\u63d0\u9ad8\u5e76\u53d1\u7387\uff0c\u53ef\u4ee5\u5c06\u9694\u79bb\u7ea7\u522b\u8bbe\u7f6e\u6210 RC \u5e76\u5c06 binlog \u683c\u5f0f\u8bbe\u7f6e\u6210 row<\/p>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/binary-log-formats.html\" title=\"binlog-log-formats\">binlog-log-formats<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728MySQL\u4e8b\u52a1\u521d\u8bc6\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u5230\u4e0d\u540c\u7684\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u4f1a\u5f15\u53d1\u4e0d\u540c\u7684\u95ee\u9898\uff0c\u5982\u5728 RR \u7ea7\u522b\u4e0b\u4f1a\u51fa\u73b0\u5e7b\u8bfb\u3002\u4f46\u5982\u679c\u5c06\u5b58 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[526],"class_list":["post-2088","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-innodb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2088"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2088\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}