{"id":5794,"date":"2024-11-14T08:49:44","date_gmt":"2024-11-14T00:49:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5794\/"},"modified":"2024-11-14T08:49:44","modified_gmt":"2024-11-14T00:49:44","slug":"mysql%e9%94%81%e6%9c%ba%e5%88%b6%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5794\/","title":{"rendered":"mysql\u9501\u673a\u5236\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  mysql \u9501\u673a\u5236\u7528\u4e8e\u63a7\u5236\u5bf9\u6570\u636e\u7684\u5e76\u53d1\u8bbf\u95ee\uff0c\u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u3002\u5e38\u89c1\u7684\u9501\u7c7b\u578b\u5305\u62ec\uff1a\u8868\u9501\uff1a\u9501\u5b9a\u6574\u4e2a\u8868\u3002\u884c\u9501\uff1a\u9501\u5b9a\u7279\u5b9a\u6570\u636e\u884c\u3002\u95f4\u9699\u9501\uff1a\u9501\u5b9a\u6307\u5b9a\u8303\u56f4\u7684\u6f5c\u5728\u884c\u3002\u4f7f\u7528 mysql \u9501\u9700\u4f7f\u7528 lock \u5173\u952e\u5b57\uff0c\u5982\uff1alock table table_name [mode] [option]\u5176\u4e2d [mode] \u6307\u5b9a\u9501\u7c7b\u578b\uff0c\u5982 read \u6216 write\uff1b[option] \u6307\u5b9a\u989d\u5916\u9501\u9009\u9879\uff0c\u5982 nowait\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/02\/2024060201512820905.jpg\" class=\"aligncenter\" title=\"mysql\u9501\u673a\u5236\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"mysql\u9501\u673a\u5236\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u9501\u673a\u5236\u7684\u7528\u6cd5<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f MySQL \u9501\uff1f<\/strong><br \/>MySQL \u9501\u662f\u4e00\u79cd\u6570\u636e\u5e93\u673a\u5236\uff0c\u7528\u4e8e\u63a7\u5236\u5bf9\u6570\u636e\u7684\uff0c\u9632\u6b62\u591a\u4e2a\u7528\u6237\u540c\u65f6\u66f4\u6539\u76f8\u540c\u7684\u6570\u636e\uff0c\u4ece\u800c\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>MySQL \u5e38\u89c1\u7684\u9501\u7c7b\u578b\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u8868\u9501 (Table Lock)\uff1a<\/strong>\u9501\u5b9a\u6574\u4e2a\u8868\uff0c\u5305\u62ec\u8868\u7684\u6240\u6709\u884c\u548c\u5217\u3002<\/li>\n<li> <strong>\u884c\u9501 (Row Lock)\uff1a<\/strong>\u53ea\u9501\u5b9a\u7279\u5b9a\u7684\u6570\u636e\u884c\uff0c\u5176\u4ed6\u884c\u4e0d\u53d7\u5f71\u54cd\u3002<\/li>\n<li> <strong>\u95f4\u9699\u9501 (Gap Lock)\uff1a<\/strong>\u9501\u5b9a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6f5c\u5728\u884c\uff08\u5373\u4f7f\u5b83\u4eec\u4e0d\u5b58\u5728\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u9501\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528 MySQL \u9501\u9700\u8981\u5728 SQL \u8bed\u53e5\u4e2d\u4f7f\u7528 LOCK \u5173\u952e\u5b57\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>LOCK [TABLE | ROW] table_name [AS alias]\n[mode] [option]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>mode \u6307\u5b9a\u9501\u7684\u7c7b\u578b\uff08\u5982 READ\u3001WRITE\uff09\uff0c\u9ed8\u8ba4\u4e3a READ\u3002<\/li>\n<li>option \u6307\u5b9a\u989d\u5916\u7684\u9501\u9009\u9879\uff08\u5982 NOWAIT\uff09\uff0c\u9ed8\u8ba4\u4e3a WAIT\u3002<\/li>\n<\/ul>\n<p><strong>MySQL \u9501\u7684\u7528\u6cd5\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><strong>\u8868\u9501<\/strong><\/p>\n<pre>LOCK TABLE my_table WRITE;\n-- \u6267\u884c\u64cd\u4f5c\nUNLOCK TABLES;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u884c\u9501<\/strong><\/p>\n<pre>LOCK TABLE my_table ROWS (id = 1) FOR UPDATE;\n-- \u6267\u884c\u64cd\u4f5c\nUNLOCK TABLES;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u95f4\u9699\u9501<\/strong><\/p>\n<pre>LOCK TABLE my_table RANGE (id &gt;= 10 AND id &lt; 20);\n-- \u6267\u884c\u64cd\u4f5c\nUNLOCK TABLES;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u65f6\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u5c3d\u91cf\u4f7f\u7528\u884c\u9501\u6216\u95f4\u9699\u9501\uff0c\u907f\u514d\u4f7f\u7528\u8868\u9501\uff0c\u4ee5\u63d0\u9ad8\u5e76\u53d1\u6027\u3002<\/li>\n<li>\u53ea\u6709\u5728\u5fc5\u8981\u65f6\u624d\u4f7f\u7528\u9501\uff0c\u56e0\u4e3a\u9501\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n<li>\u786e\u4fdd\u5728\u4e0d\u518d\u9700\u8981\u65f6\u91ca\u653e\u9501\uff0c\u4ee5\u907f\u514d\u6b7b\u9501\u3002<\/li>\n<li>\u4e86\u89e3\u4e0d\u540c\u9501\u7c7b\u578b\u7684\u4f18\u7f3a\u70b9\uff0c\u5e76\u5728\u5408\u9002\u7684\u60c5\u51b5\u4e0b\u9009\u62e9\u5408\u9002\u7684\u9501\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u9501\u673a\u5236\u600e\u4e48\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql \u9501\u673a\u5236\u7528\u4e8e\u63a7\u5236\u5bf9\u6570\u636e\u7684\u5e76\u53d1\u8bbf\u95ee\uff0c\u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u3002\u5e38\u89c1\u7684\u9501\u7c7b\u578b\u5305\u62ec\uff1a\u8868\u9501\uff1a\u9501\u5b9a\u6574\u4e2a\u8868\u3002\u884c\u9501\uff1a\u9501\u5b9a\u7279\u5b9a\u6570\u636e\u884c\u3002\u95f4\u9699\u9501\uff1a\u9501\u5b9a\u6307\u5b9a\u8303\u56f4\u7684\u6f5c\u5728\u884c\u3002\u4f7f\u7528 mysql \u9501\u9700\u4f7f\u7528 lock \u5173\u952e\u5b57\uff0c\u5982\uff1alock table table_name [mode] [option]\u5176\u4e2d [mode] \u6307\u5b9a\u9501\u7c7b\u578b\uff0c\u5982 read \u6216 write\uff1b[option] \u6307\u5b9a\u989d\u5916\u9501\u9009\u9879\uff0c\u5982 nowait\u3002 MySQL \u9501\u673a\u5236\u7684\u7528\u6cd5 \u4ec0\u4e48\u662f MySQL \u9501\uff1fMySQL \u9501\u662f\u4e00\u79cd\u6570\u636e\u5e93\u673a\u5236\uff0c\u7528\u4e8e\u63a7\u5236\u5bf9\u6570\u636e\u7684\uff0c\u9632\u6b62\u591a\u4e2a\u7528\u6237\u540c\u65f6\u66f4\u6539\u76f8\u540c\u7684\u6570\u636e\uff0c\u4ece\u800c\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002 MySQL \u5e38\u89c1\u7684\u9501\u7c7b\u578b\uff1a \u8868\u9501 (Table Lock)\uff1a\u9501\u5b9a\u6574\u4e2a\u8868\uff0c\u5305\u62ec\u8868\u7684\u6240\u6709\u884c\u548c\u5217\u3002 \u884c\u9501 (Row Lock)\uff1a\u53ea\u9501\u5b9a\u7279\u5b9a\u7684\u6570\u636e\u884c\uff0c\u5176\u4ed6\u884c\u4e0d\u53d7\u5f71\u54cd\u3002 \u95f4\u9699\u9501 (Gap Lock)\uff1a\u9501\u5b9a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6f5c\u5728\u884c\uff08\u5373\u4f7f\u5b83\u4eec\u4e0d\u5b58\u5728\uff09\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u9501\uff1f \u4f7f\u7528 MySQL \u9501\u9700\u8981\u5728 SQL \u8bed\u53e5\u4e2d\u4f7f\u7528 LOCK \u5173\u952e\u5b57\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a LOCK [TABLE | ROW] table_name [AS alias] [mode] [option] \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5794","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5794"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5794\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}