{"id":15568,"date":"2017-12-05T15:47:57","date_gmt":"2017-12-05T06:47:57","guid":{"rendered":"https:\/\/sys-guard.com\/?p=15568"},"modified":"2017-12-05T20:33:48","modified_gmt":"2017-12-05T11:33:48","slug":"%ef%bc%91%e3%81%a4%e3%81%ae%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%a7%e3%83%87%e3%83%83%e3%83%89%e3%83%ad%e3%83%83%e3%82%af%e3%81%95%e3%81%9b%e3%82%8b","status":"publish","type":"post","link":"https:\/\/sys-guard.com\/post-15568\/","title":{"rendered":"MySQL \uff11\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u3055\u305b\u308b"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15558img-responsive\" src=\"https:\/\/sys-guard.com\/contents\/wp-content\/uploads\/2017\/12\/deadlock_20171204_1.jpg\" alt=\"\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\" width=\"680\" height=\"450\" \/><\/p>\n<p>\u3069\u3046\u3082\uff01\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u64b2\u6ec5\u59d4\u54e1\u4f1a\u4f1a\u9577\u306e\u512a\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\uff11\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u3082\u767a\u751f\u3057\u3066\u3057\u307e\u3046\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306e\u3054\u7d39\u4ecb\u3067\u3059\uff01<\/p>\n<p>&nbsp;<\/p>\n<h2>\u3053\u3093\u306a\u30b1\u30fc\u30b9<\/h2>\n<p>\u300e\u602a\u3057\u3044\u306e\u306f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3057\u3066\u3044\u308b\u7b87\u6240\u3060\u3002\u3067\u30822\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u64cd\u4f5c\u3057\u3066\u3044\u308b\u308f\u3051\u3067\u3082\u306a\u3044\u306e\u3067\u3001\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306f\u8d77\u3053\u308a\u3048\u306a\u3044\u306f\u305a\u3002\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u3063\u3066\u3053\u3068\u306f\u306a\u3055\u305d\u3046\u3060\u3051\u3069\uff1f\uff1f\u3055\u3063\u3071\u308a\u539f\u56e0\u304c\u4e0d\u660e\u3060&#8230;\u3002\u30b5\u30fc\u30d0\u30b9\u30da\u30c3\u30af\u3042\u3052\u305f\u3089\u89e3\u6c7a\u3059\u308b\u304b\u306a\uff1f\uff1f\uff1f\u300f<\/p>\n<p>&nbsp;<\/p>\n<p>\u305d\u3093\u306a\u6642\u3001<strong>\u305d\u308c\u306f\u3084\u306f\u308a\u30c7\u30c3\u30c9\u30ed\u30c3\u30af<\/strong>\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>\u30d0\u30c3\u30af\u30ca\u30f3\u30d0\u30fc<\/h3>\n<ul>\n<li><a href=\"https:\/\/sys-guard.com\/post-15552\/\">MySQL\u306e\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u5bfe\u51e6 \u304a\u307e\u3051\u3067\u30ae\u30e3\u30c3\u30d7\u30ed\u30c3\u30af<\/a><\/li>\n<li><a href=\"https:\/\/sys-guard.com\/post-15568\/\">\uff11\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u3055\u305b\u308b<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4>\uff11\u30c6\u30fc\u30d6\u30eb\u3067\u3082\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306f\u767a\u751f\u3059\u308b<\/h4>\n<pre class=\"lang:default decode:true \">CREATE TABLE product(\r\n\tid         INT UNSIGNED PRIMARY KEY,\r\n    name       VARCHAR(255),\r\n    INDEX      idx_a_id(id)\r\n)ENGINE=InnoDB DEFAULT CHARSET=utf8;\r\n\r\n\r\n\r\nINSERT INTO product(id, name) VALUES\r\n\t(1, \"\u30b1\u30fc\u30ad\u98a8\u3070\u306a\u306a\"),\r\n\t(2, \"\u9ad8\u3081\u306e\u3042\u3093\u3071\u3093\");<\/pre>\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306b\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15570img-responsive\" src=\"https:\/\/sys-guard.com\/contents\/wp-content\/uploads\/2017\/12\/single_table_Lock_1_r2.jpg\" alt=\"\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\" width=\"680\" height=\"443\" \/><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">\u25cfA\r\n\r\n\r\nBEGIN;\r\n\r\nUPDATE product SET name = \"\u307e\u308b\u3054\u3068\u30d0\u30ca\u30ca\" WHERE id = 1;\r\n\r\n\r\n\u25cfB\r\n\r\nBEGIN;\r\n\r\nUPDATE product SET name = \"\u9ad8\u7d1a\u3042\u3093\u3071\u3093\" WHERE id = 2;\r\n\r\n\r\n\r\n\u25cfA\r\n\r\nUPDATE product SET name = \"\u9ad8\u7d1a\u3042\u3093\u3071\u3093\" WHERE id = 2;\r\n\r\n\u56fa\u307e\u308b\r\n\r\n\r\n\r\n\r\n\u25cfB\r\n\r\nUPDATE product SET name = \"\u307e\u308b\u3054\u3068\u30d0\u30ca\u30ca\" WHERE id = 1;\r\n\r\n\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\uff01<\/pre>\n<p>&nbsp;<\/p>\n<p>\u30ec\u30b3\u30fc\u30c9\u306e\u66f4\u65b0\u9806\u306b\u3088\u3063\u3066\u306f\u3001<strong>\uff11\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u3082\u305f\u3059\u304d\u304c\u3051\u3068\u306a\u308a\u30c7\u30c3\u30c9\u30ed\u30c3\u30af<\/strong>\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<p><strong>\u8907\u6570\u30ec\u30b3\u30fc\u30c9\u3092\u65e5\u6642\u306a\u3069\u306e\u7bc4\u56f2\u6307\u5b9a\u3067\u3044\u3063\u304d\u306b\u66f4\u65b0\u3059\u308b\u6642\u306a\u3069<\/strong>\u767a\u751f\u3057\u304c\u3061\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u89e3\u6c7a\u306b\u306f\uff1f<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15571img-responsive\" src=\"https:\/\/sys-guard.com\/contents\/wp-content\/uploads\/2017\/12\/single_table_Lock_2.jpg\" alt=\"\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\" width=\"680\" height=\"443\" \/><\/p>\n<pre class=\"lang:default decode:true \">\u25cfA\r\n\r\n\r\nBEGIN;\r\n\r\nSELECT * FROM product WHERE id = 1 FOR UPDATE;\r\nSELECT * FROM product WHERE id = 2 FOR UPDATE;\r\nUPDATE product SET name = \"\u307e\u308b\u3054\u3068\u30d0\u30ca\u30ca\" WHERE id = 1;\r\n\r\n\r\n\u25cfB\r\n\r\nBEGIN;\r\n\r\nSELECT * FROM product WHERE id = 2 FOR UPDATE;\r\n\r\n\u56fa\u307e\u308b\u3002\u30ed\u30c3\u30af\u89e3\u653e\u5f85\u3061\u3002\r\n\r\n\r\n\r\n\r\n\r\n\u25cfA\r\n\r\nUPDATE product SET name = \"\u9ad8\u7d1a\u3042\u3093\u3071\u3093\" WHERE id = 2;\r\n\r\nCOMMIT;\r\n\r\n\r\n\r\n\r\n\u25cfB\r\n\r\n\u30ed\u30c3\u30af\u89e3\u653e\u3055\u308c\u308b\r\n\r\nSELECT * FROM product WHERE id = 1 FOR UPDATE;\r\nUPDATE product SET name = \"\u9ad8\u7d1a\u3042\u3093\u3071\u3093\" WHERE id = 2;\r\nUPDATE product SET name = \"\u307e\u308b\u3054\u3068\u30d0\u30ca\u30ca\" WHERE id = 1;\r\n\r\nCOMMIT;<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>FOR UPDATE\u3067\u6392\u4ed6\u30ed\u30c3\u30af<\/strong>\u3092\u304b\u3051\u3066\u3042\u3052\u308c\u3070OK\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4>\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u3092\u30ed\u30b0\u306b\u51fa\u3057\u305f\u3044<\/h4>\n<p>innodb_print_all_deadlocks\u3092ON\u306b\u3059\u308c\u3070\u30a8\u30e9\u30fc\u30ed\u30b0\u3068\u3057\u3066\u30ed\u30b0\u306b\u51fa\u529b\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u73fe\u5728\u306e\u8a2d\u5b9a\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">mysql&gt; show variables like 'innodb_print_all_deadlocks';\r\n\r\n+----------------------------+-------+\r\n| Variable_name              | Value |\r\n+----------------------------+-------+\r\n| innodb_print_all_deadlocks | OFF   |\r\n+----------------------------+-------+\r\n1 row in set (0.00 sec)<\/pre>\n<p>OFF\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u306d\u3002<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">[mysqld]\r\n# join_buffer_size = 128M\r\n# sort_buffer_size = 2M\r\n# read_rnd_buffer_size = 2M\r\ndatadir=\/var\/lib\/mysql\r\nsocket=\/var\/lib\/mysql\/mysql.sock\r\n\r\ninnodb_print_all_deadlocks = ON \u2190\u8ffd\u52a0<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \"># systemctl restart mysqld<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306e\u30ed\u30b0\u3092\u898b\u3066\u307f\u3088\u3046\uff01<\/h3>\n<pre class=\"lang:default decode:true\"># tail -n 50 \/var\/log\/mysqld.log\r\n\r\n\r\n\r\n*** (1) TRANSACTION:\r\n\r\nTRANSACTION 3092, ACTIVE 16 sec starting index read\r\nmysql tables in use 1, locked 1\r\nLOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1\r\nMySQL thread id 3, OS thread handle 140717827069696, query id 23 localhost root updating\r\nUPDATE product SET name = \"\u9ad8\u7d1a\u3042\u3093\u3071\u3093\" WHERE id = 2\r\n2017-12-05T06:12:58.818434Z 4 [Note] InnoDB: *** (1) WAITING FOR THIS LOCK TO BE GRANTED:\r\n\r\nRECORD LOCKS space id 88 page no 3 n bits 72 index PRIMARY of table `testdb`.`product` trx id 3092 lock_mode X locks rec but not gap waiting\r\nRecord lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 0\r\n 0: len 4; hex 00000002; asc     ;;\r\n 1: len 6; hex 000000000c15; asc       ;;\r\n 2: len 7; hex 300000013e16ff; asc 0   &gt;  ;;\r\n 3: len 18; hex e9ab98e7b49ae38182e38293e381b1e38293; asc                   ;;\r\n\r\n2017-12-05T06:12:58.818522Z 4 [Note] InnoDB: *** (2) TRANSACTION:\r\n\r\nTRANSACTION 3093, ACTIVE 11 sec starting index read\r\nmysql tables in use 1, locked 1\r\n3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1\r\nMySQL thread id 4, OS thread handle 140717826803456, query id 24 localhost root updating\r\nUPDATE product SET name = \"\u307e\u308b\u3054\u3068\u30d0\u30ca\u30ca\" WHERE id = 1\r\n2017-12-05T06:12:58.818535Z 4 [Note] InnoDB: *** (2) HOLDS THE LOCK(S):\r\n\r\nRECORD LOCKS space id 88 page no 3 n bits 72 index PRIMARY of table `testdb`.`product` trx id 3093 lock_mode X locks rec but not gap\r\nRecord lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 0\r\n 0: len 4; hex 00000002; asc     ;;\r\n 1: len 6; hex 000000000c15; asc       ;;\r\n 2: len 7; hex 300000013e16ff; asc 0   &gt;  ;;\r\n 3: len 18; hex e9ab98e7b49ae38182e38293e381b1e38293; asc                   ;;\r\n\r\n2017-12-05T06:12:58.818603Z 4 [Note] InnoDB: *** (2) WAITING FOR THIS LOCK TO BE GRANTED:\r\n\r\nRECORD LOCKS space id 88 page no 3 n bits 72 index PRIMARY of table `testdb`.`product` trx id 3093 lock_mode X locks rec but not gap waiting\r\nRecord lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0\r\n 0: len 4; hex 00000001; asc     ;;\r\n 1: len 6; hex 000000000c14; asc       ;;\r\n 2: len 7; hex 2f0000013d0faa; asc \/   =  ;;\r\n 3: len 21; hex e381bee3828be38194e381a8e38390e3838ae3838a; asc                      ;;\r\n\r\n2017-12-05T06:12:58.818675Z 4 [Note] InnoDB: *** WE ROLL BACK TRANSACTION (2)<\/pre>\n<p>\u5b9f\u306f\u4e8b\u524d\u306b\u8a2d\u5b9a\u3042\u308b\u306e\u3067\u3001\u3053\u3093\u306a\u98a8\u306b\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306e\u7b87\u6240\u3092\u78ba\u8a8d\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u304a\u75b2\u308c\u69d8\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3069\u3046\u3082\uff01\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u64b2\u6ec5\u59d4\u54e1\u4f1a\u4f1a\u9577\u306e\u512a\u3067\u3059\u3002 \u4eca\u56de\u306f\uff11\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u3082\u767a\u751f\u3057\u3066\u3057\u307e\u3046\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306e\u3054\u7d39\u4ecb\u3067\u3059\uff01 &nbsp; \u3053\u3093\u306a\u30b1\u30fc\u30b9 \u300e\u602a\u3057\u3044\u306e\u306f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3057\u3066\u3044\u308b\u7b87\u6240\u3060\u3002\u3067\u30822\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u64cd\u4f5c\u3057\u3066\u3044\u308b\u308f\u3051\u3067\u3082\u306a\u3044\u306e\u3067\u3001\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306f\u8d77 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":15573,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,26],"tags":[205,224],"_links":{"self":[{"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/posts\/15568"}],"collection":[{"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/comments?post=15568"}],"version-history":[{"count":0,"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/posts\/15568\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/media\/15573"}],"wp:attachment":[{"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/media?parent=15568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/categories?post=15568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sys-guard.com\/wp-json\/wp\/v2\/tags?post=15568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}