{"id":5394,"date":"2020-07-30T00:16:25","date_gmt":"2020-07-29T16:16:25","guid":{"rendered":"http:\/\/39.97.231.10\/?p=5394"},"modified":"2020-07-30T00:16:25","modified_gmt":"2020-07-29T16:16:25","slug":"mysql%e5%9c%a8%e7%ba%bf%e6%94%b9%e8%a1%a8%e5%b7%a5%e5%85%b7gh-ost%e6%ba%90%e7%a0%81%e7%bc%96%e8%af%91%e4%b8%8e%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=5394","title":{"rendered":"MySQL\u5728\u7ebf\u6539\u8868\u5de5\u5177gh-ost\u6e90\u7801\u7f16\u8bd1\u4e0e\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<div>\n<div>\n<div>\ngh-ost \u662f github\u5f00\u6e90\u7684MySQL\u5728\u7ebf\u6539\u8868\u5de5\u5177\uff0c\u4f7f\u7528go\u8bed\u8a00\u5f00\u53d1\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528\u89e6\u53d1\u5668\uff0c\u91c7\u7528binlog\u540c\u6b65\u589e\u91cf\u6570\u636e\uff0c\u6027\u80fd\u635f\u8017\u8f83\u5c0f\uff0c\u540c\u65f6\u4e5f\u907f\u514d\u4e86\u4e0e\u4e1a\u52a1SQL\u5e76\u53d1\u6267\u884c\u53ef\u80fd\u5bfc\u81f4\u7684\u6b7b\u9501\u3002\u672c\u6587\u7b80\u5355\u4ecb\u7ecdgh-ost\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\u4e0e\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<h4>\u4e00\u3001gh-ost\u7f16\u8bd1\u5b89\u88c5<\/h4>\n<p>1. \u524d\u63d0\u6761\u4ef6\uff1a<br \/>\ngo\u5f00\u53d1\u73af\u5883\u7248\u672c\uff1a1.12\u53ca\u4ee5\u4e0a<br \/>\ngo\u7248\u672c\u5347\u7ea7\uff0c\u53c2\u8003\u6587\u7ae0\uff1a<a href=\\\"https:\/\/mytecdb.com\/blogDetail.php?id=125\\\">Linux\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\u9ad8\u7248\u672cgo\u8bed\u8a00\u5f00\u53d1\u73af\u5883<\/a><br \/>\n2. \u4e0b\u8f7dgh-ost\u6e90\u7801\uff1a<br \/>\ngit clone https:\/\/github.com\/github\/gh-ost.git<br \/>\n3. \u7f16\u8bd1\uff1a<br \/>\ncd \/data\/git\/gh-ost<br \/>\n.\/script\/build<br \/>\n\u7f16\u8bd1\u6210\u529f\u540e\uff0c\u751f\u6210\u7684 gh-ost \u53ef\u6267\u884c\u6587\u4ef6\u4f4d\u4e8e\u76ee\u5f55\uff1a<br \/>\n\/data\/git\/gh-ost\/bin\/<\/p>\n<h4>\u4e8c\u3001gh-ost\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<pre>gh-ost\n-host=127.0.0.1\n-user=\\\"user\\\"\n-password=\\\"passwd\\\"\n-database=\\\" dbname\\\"\n-table=\\\"tablename\\\"\n-alter=\\\"add column c1 varchar(100)\\\"\n-allow-on-master\n-ok-to-drop-table\n-initially-drop-ghost-table\n-execute\n<\/pre>\n<h4>\u4e09\u3001gh-ost\u5e38\u7528\u53c2\u6570<\/h4>\n<ul>\n<li>-host\uff0cMySQL\u673a\u5668IP\uff0c\u901a\u5e38\u662f\u4e00\u4e2a\u4ece\u5e93\uff0c\u6307\u5b9a-allow-on-master\u53c2\u6570\u540e\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e3b\u5e93\u3002<\/li>\n<li>-port\uff0cMySQL\u7aef\u53e3\u53f7<\/li>\n<li>-user\uff0cMySQL\u7528\u6237\u540d<\/li>\n<li>-password\uff0cMySQL\u5bc6\u7801<\/li>\n<li>-database\uff0c\u6570\u636e\u5e93\u540d\u79f0<\/li>\n<li>-table\uff0c\u8868\u540d\u79f0<\/li>\n<li>-alter\uff0c\u6539\u8868\u7684SQL\u8bed\u53e5\uff0c\u6bd4\u5982 add column age int default 0<\/li>\n<li>-allow-on-master\uff0c\u5141\u8bb8\u8fde\u63a5\u4e3b\u5e93\u8bfb\u53d6binlog<\/li>\n<li>-ok-to-drop-table\uff0c\u6539\u5b8c\u8868\u4e4b\u540e\uff0c\u662f\u5426\u5220\u9664\u65e7\u8868<\/li>\n<li>-execute\uff0c\u6267\u884c\u6539\u8868<\/li>\n<li>-chunk-size\uff0c\u62f7\u8d1d\u539f\u8868\u6570\u636e\u65f6\uff0c\u4e00\u6b21\u62f7\u8d1d\u7684\u884c\u6570<\/li>\n<li>-initially-drop-ghost-table\uff0c\u5728\u6539\u8868\u6267\u884c\u4e4b\u524d\uff0c\u5220\u9664\u4e4b\u524d\u6267\u884c\u5931\u8d25\u7559\u4e0b\u6765\u7684gho\u8868\u3002<\/li>\n<li>-initially-drop-old-table\uff0c\u5728\u6539\u8868\u6267\u884c\u4e4b\u524d\uff0c\u5220\u9664\u4e4b\u524d\u7559\u4e0b\u6765\u7684\u65e7\u8868<\/li>\n<li>-max-lag-millis\uff0c\u6700\u5927\u590d\u5236\u5ef6\u8fdf\uff0c\u5927\u4e8e\u8be5\u503c\u65f6\uff0cgh-ost\u6682\u505c\u6267\u884c<\/li>\n<li>-cut-over-lock-timeout-seconds\uff0c\u6539\u8868\u7684cut-over\u9636\u6bb5\u65f6\uff0c\u83b7\u53d6\u9501\u7684\u8d85\u65f6\u65f6\u95f4<\/li>\n<\/ul>\n<h4>\u56db\u3001gh-ost\u539f\u7406<\/h4>\n<ol>\n<li>gh-ost\u5148\u8fde\u63a5\u5230\u4e3b\u5e93\u4e0a\uff0c\u521b\u5efa\u4e34\u65f6\u7684ghost\u8868\uff0c\u8868\u540d\u4ee5_gho\u7ed3\u5c3e\uff0c\u6839\u636ealter\u8bed\u53e5\u4fee\u6539ghost\u8868\u3002<\/li>\n<li>gh-ost\u521b\u5efa\u65b0\u7684\u8fde\u63a5\uff0c\u8fde\u63a5\u5230\u4e3b\u5e93\u6216\u8005\u67d0\u4e00\u4e2a\u5907\u5e93\uff0c\u9ed8\u8ba4\u6761\u4ef6\u4e0b\u662f\u8fde\u63a5\u5907\u5e93\uff0c\u540c\u65f6\u5c06\u81ea\u5df1\u6a21\u62df\u6210\u4e00\u4e2a\u5907\u5e93\uff0c\u4e00\u8fb9\u5728\u4e3b\u5e93\u4e0a\u4ece\u6e90\u8868\u62f7\u8d1d\u5df2\u6709\u7684\u6570\u636e\u5230\u65b0\u8868(ghost\u8868\uff09\uff0c\u4e00\u8fb9\u4ece\u4e3b\u5e93\uff08\u6216\u8005\u67d0\u4e00\u4e2a\u5907\u5e93\uff09\u4e0a\u62c9\u53d6\u589e\u91cf\u6570\u636e\u7684binlog\uff0c\u7136\u540e\u4e0d\u65ad\u7684\u628a binlog \u65e5\u5fd7\u4e2d\u5173\u4e8e\u6e90\u8868\u7684\u4fee\u6539\u90e8\u5206\u89e3\u6790\u6210SQL\uff0c\u5e94\u7528\u5230\u4e3b\u5e93\u7684ghost\u8868\u3002<\/li>\n<li>cut-over\u662f\u6700\u540e\u4e00\u6b65\uff0c\u9501\u4f4f\u4e3b\u5e93\u7684\u6e90\u8868\uff0c\u7b49\u5f85binlog \u5e94\u7528\u5b8c\u6bd5\uff0c\u7136\u540e\u5c06ghost\u8868\u548c\u6e90\u8868\u8fdb\u884c\u66ff\u6362\u3002<\/li>\n<\/ol>\n<h5>4.1 \u6e90\u8868\u6570\u636e\u5982\u4f55\u62f7\u8d1d\u5230ghost\u8868\uff1f<\/h5>\n<p>gh-ost \u4f7f\u7528 insert ignore \u8bed\u6cd5\u5f80\u65b0\u8868\u8fc1\u79fb\u6570\u636e\uff0c\u56e0\u4e3a\u8fc1\u79fb\u6570\u636e\u4e0e\u5e94\u7528binlog\u5e76\u53d1\u8fdb\u884c\uff0c\u5982\u679cbinlog\u5148\u5e94\u7528\uff0c\u90a3\u4e48\u8fc1\u79fb\u6570\u636einsert ignore\u5c31\u4f1a\u5ffd\u7565\u8be5\u6570\u636e\u3002\u5177\u4f53\u7684SQL\u8bed\u53e5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>insert \/* gh-ost `sysbench`.`sbtest1` *\/ ignore into `sysbench`.`_sbtest1_gho` (`id`, `k`, `c\n`, `pad`, `age`, `age1`, `age2`, `age3`, `age4`, `age5`)\n      (select `id`, `k`, `c`, `pad`, `age`, `age1`, `age2`, `age3`, `age4`, `age5` from `sysbench`.`sbtest1` force index (`PRIMARY`)\n        where (((`id` &gt; _binary\\'203685\\')) and ((`id` &lt; _binary\\'204685\\') or ((`id` = _binary\\'204685\\')))) lock in share mode\n      )\n<\/pre>\n<h5>4.2 binlog\u65e5\u5fd7\u5982\u4f55\u89e3\u6790\u6210\u56de\u653eSQL\uff1f<\/h5>\n<p>gh-ost\u4f7f\u7528\u7b2c\u4e09\u65b9binlog\u65e5\u5fd7\u89e3\u6790\u5e93 siddontang\/go-mysql \u8fdb\u884cbinlog\u65e5\u5fd7\u7684\u89e3\u6790\u3002<\/p>\n<ul>\n<li>insert\u8bed\u53e5\u89e3\u6790\uff0c\u8f6c\u6362\u4e3areplace into\u8bed\u53e5\u3002<\/li>\n<li>update\u8bed\u53e5\u89e3\u6790\u4e3aupdate\u8bed\u53e5\u3002<\/li>\n<li>delete\u8bed\u53e5\u89e3\u6790\u4e3adelete\u8bed\u53e5 \u3002<\/li>\n<\/ul>\n<p>\u6e90\u8868\u5f80\u65b0\u8868\u8fc1\u79fb\u6570\u636e\uff0c\u4e1a\u52a1DML\u64cd\u4f5c\u6e90\u8868\uff0c\u89e3\u6790binlog\u751f\u6210SQL\u5e94\u7528\u5230\u65b0\u8868\uff0cbinlog\u7531DML\u64cd\u4f5c\u4ea7\u751f\uff0c\u8fd9\u4e09\u79cd\u64cd\u4f5c\u540c\u65f6\u8fdb\u884c\uff0c\u65e0\u8bba\u8c01\u5148\u8c01\u540e\uff0c\u5728\u6570\u636e\u8fc1\u79fb\u5b8c\u6210\u65f6\uff0c\u52a0\u9501\uff0c\u7b49\u5f85binlog\u5e94\u7528\u5b8c\u6210\uff0c\u6700\u7ec8\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u3002<\/p>\n<h5>4.3 cut-over\u8fc7\u7a0b<\/h5>\n<p>gh-ost cut-over\u8bbe\u8ba1\u7684\u5f88\u5de7\u5999\uff0c\u4e5f\u6709\u4e00\u70b9\u590d\u6742\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u4fdd\u8bc1\u5728cut-over\u9636\u6bb5\uff0c\u4e0d\u7ba1\u53d1\u751f\u4ec0\u4e48\u5f02\u5e38\u60c5\u51b5\uff0c\u90fd\u80fd\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<br \/>\n\u4e0b\u9762\u8be6\u7ec6\u5206\u6790\uff0cC1&#8230;C9\uff0cC10\u7b49\u7b49\u8868\u793a\u4e0d\u540c\u7684\u8fde\u63a5\u3002<\/p>\n<ol>\n<li>C1..C9\uff1a\u5bf9 tbl \u6267\u884c\u6b63\u5e38\u7684DML: INSERT, UPDATE, DELETE<\/li>\n<li>C10\uff1aCREATE TABLE tbl_old (id int primary key) COMMENT=\\&#8217;magic-be-here\\&#8217;<\/li>\n<li>C10\uff1aLOCK TABLES tbl WRITE, tbl_old WRITE<\/li>\n<li>C11..C19\uff1a\u65b0\u6765\u7684\u8fde\u63a5\uff0c\u5bf9tbl\u7684dml\uff0c\u7531\u4e8eC10\u7684LOCK\u88ab\u963b\u585e<\/li>\n<li>C20\uff1aRENAME TABLE tbl TO tbl_old, ghost TO tbl \u540c\u6837\u88abC10\u7684LOCK\u963b\u585e, \u4f46\u662f\u4f18\u5148\u7ea7\u9ad8\u4e8e C11..C19 \u548c C1..C9 \u548c\u4efb\u4f55\u4f01\u56fe\u5728tbl\u4e0a\u6267\u884c DML\u7684\u4f1a\u8bdd<\/li>\n<li>C21..C29\uff1a\u65b0\u6765\u7684\u8fde\u63a5\uff0c\u5728 tbl\u4e0a\u6267\u884cdml\uff0c\u4f46\u662f\u88abC10\u7684 LOCK \u548cC20\u7684 RENAME\u963b\u585e<\/li>\n<li>C10\uff1a\u68c0\u67e5 C20 \u7684 RENAME \u662f\u5426\u4ecd\u5728\u7b49\u5f85 (\u5728processlist\u4e2d\u68c0\u67e5 RENAME )<\/li>\n<li>C10\uff1aDROP TABLE tbl_old\uff0c\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f\uff0ctbl \u4ecd\u7136\u662f locked\uff0c\u5176\u4ed6\u4f1a\u8bdd\u4e5f\u4ecd\u7136\u88ab\u963b\u585e<\/li>\n<li>C10\uff1aUNLOCK TABLES<\/li>\n<li>unlock\u540e\u9996\u5148\u6267\u884cC20\u7684RENAME , ghost \u4e0e tbl\u4e92\u6362, \u7136\u540e C1..C9, C11..C19, C21..C29 \u90fd\u5728\u65b0\u7684 tbl\u8868\u4e0a\u6267\u884c<\/li>\n<\/ol>\n<p>\u6ce8\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa tbl_old \u9632\u6b62\u8fc7\u65e9\u4ea4\u6362\u8868<\/li>\n<li>\u4e00\u4e2a\u4f1a\u8bdd\u5728\u62e5\u6709\u8868\u7684write lock\u540e\u4ecd\u53ef\u4ee5\u6267\u884c drop table\uff0c\u4f46\u662f\u4e0d\u80fd\u5bf9\u8be5\u8868\u6267\u884crename\u64cd\u4f5c<\/li>\n<li>\u88ab\u963b\u585e\u7684 rename \u4f18\u5148\u7ea7\u6c38\u8fdc\u9ad8\u4e8e\u88ab\u963b\u585e\u7684 insert\/update\/delete\uff0c\u65e0\u8bba\u8c01\u5148\u5f00\u59cb\u7684\uff0c\u5728table\u7684lock\u91ca\u653e\u540e\uff0crename\u4f18\u5148\u6267\u884c<\/li>\n<\/ul>\n<p>cut-over\u8fc7\u7a0b\u4e2d\u4efb\u4e00\u9636\u6bb5\u5931\u8d25\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/p>\n<ul>\n<li>C10 \u5728 CREATE \u65f6\u5931\u8d25\uff0c\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u53d1\u751f<\/li>\n<li>C10 \u5728 LOCK \u65f6\u5931\u8d25, \u8868\u4e0d\u4f1a\u88ab\u9501\uff0c\u4e1a\u52a1\u6b63\u5e38\u6267\u884c\u5176\u4ed6\u8bed\u53e5<\/li>\n<li>C10\u5728C20\u5373\u5c06 RENAME \u65f6\u5f02\u5e38\u65ad\u5f00\u8fde\u63a5\uff0c\u91ca\u653e\u9501\uff0c C1..C9, C11..C19 \u7acb\u5373\u5728 tbl\u4e0a\u6267\u884c\u3002C20\u7684 RENAME \u7acb\u5373\u5931\u8d25\uff0c\u56e0\u4e3a tbl_old \u5b58\u5728\u3002\u5f71\u54cd\u4ec5\u4e3aC1..C9, C11..C19\u7684\u64cd\u4f5c\u88ab\u9501\u4e86\u4e00\u6bb5\u65f6\u95f4<\/li>\n<li>C10 \u5728 C20\u7684RENAME\u64cd\u4f5c\u88ab\u963b\u585e\u540e\u5f02\u5e38\u65ad\u5f00\u8fde\u63a5\uff0c\u4e0e\u4e0a\u9762\u7684\u60c5\u51b5\u7c7b\u4f3c\uff0c\u91ca\u653e\u9501, C20\u7684 RENAME \u7acb\u5373\u5931\u8d25\uff0c\u56e0\u4e3a tbl_old \u5b58\u5728\u3002\u5f71\u54cd\u4ec5\u4e3aDML\u64cd\u4f5c\u88ab\u9501\u4e86\u4e00\u6bb5\u65f6\u95f4<\/li>\n<li>C20 \u5728 C10 drops table\u4e4b\u524d\u5f02\u5e38, C10 \u6267\u884c DROP, UNLOCK\uff0cDML\u6062\u590d\u6b63\u5e38<\/li>\n<li>C20 \u5728 C10 DROP table \u4e4b\u540e UNLOCK \u4e4b\u524d\u5f02\u5e38\uff0cC10 \u6267\u884cUNLOCK\uff0cDML\u6062\u590d\u6b63\u5e38<\/li>\n<li>\u5982\u679cC10\u548cC20\u90fd\u5931\u8d25\uff0c\u4e5f\u6ca1\u6709\u95ee\u9898\uff0cLOCK\uff0cRENAME \u64cd\u4f5c\u90fd\u88ab\u91ca\u653e\uff0cC1..C9, C11..C19, C21..C29 \u53ef\u6b63\u5e38\u64cd\u4f5c<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c\u65e0\u8bba\u5728cut-over\u65f6\u53d1\u751f\u4ec0\u4e48\u5f02\u5e38\uff0c\u6574\u4e2acut-over\u90fd\u662f\u539f\u5b50\u5316\u7684\uff0c\u4e0d\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u548c\u4e0d\u4e00\u81f4\u3002\n<\/p><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>gh-ost \u662f github\u5f00\u6e90\u7684MySQL\u5728\u7ebf\u6539\u8868\u5de5\u5177\uff0c\u4f7f\u7528go\u8bed\u8a00\u5f00\u53d1\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528\u89e6\u53d1\u5668\uff0c\u91c7\u7528binl &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=5394\"> <span class=\"screen-reader-text\">MySQL\u5728\u7ebf\u6539\u8868\u5de5\u5177gh-ost\u6e90\u7801\u7f16\u8bd1\u4e0e\u4f7f\u7528\u65b9\u6cd5<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":7,"uagb_excerpt":"gh-ost \u662f github\u5f00\u6e90\u7684MySQL\u5728\u7ebf\u6539\u8868\u5de5\u5177\uff0c\u4f7f\u7528go\u8bed\u8a00\u5f00\u53d1\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528\u89e6\u53d1\u5668\uff0c\u91c7\u7528binl&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5394"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5394"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5394\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5394"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}