{"id":1147,"date":"2011-12-20T22:08:00","date_gmt":"2011-12-20T14:08:00","guid":{"rendered":"\/?p=1147"},"modified":"2012-01-11T23:25:26","modified_gmt":"2012-01-11T15:25:26","slug":"mysql-unuse-swap-2","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/mysql\/mysql-unuse-swap-2.html","title":{"rendered":"MySQL\u5982\u4f55\u907f\u514d\u4f7f\u7528swap"},"content":{"rendered":"
Linux\u6709\u5f88\u591a\u5f88\u597d\u7684\u5185\u5b58\u3001IO\u8c03\u5ea6\u673a\u5236\uff0c\u4f46\u662f\u5e76\u4e0d\u4f1a\u9002\u7528\u4e8e\u6240\u6709\u573a\u666f\u3002\u5bf9\u4e8eDBA\u6765\u8bf4Linux\u6bd4\u8f83\u8ba9\u4eba\u5934\u75bc\u7684\u4e00\u4e2a\u5730\u65b9\u662f\uff0c\u5b83\u4e0d\u4f1a\u56e0\u4e3aMySQL\u5f88\u91cd\u8981\u5c31\u907f\u514d\u5c06\u5206\u914d\u7ed9MySQL\u7684\u5730\u5740\u7a7a\u95f4\u6620\u5c04\u5230swap\u4e0a\u3002\u5bf9\u4e8e\u9891\u7e41\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u7684\u7cfb\u7edf\u800c\u8a00\uff0c\u6570\u636e\u770b\u4f3c\u5728\u5185\u5b58\u800c\u5b9e\u9645\u4e0a\u5728\u78c1\u76d8\u662f\u975e\u5e38\u7cdf\u7cd5\u7684\uff0c\u54cd\u5e94\u65f6\u95f4\u7684\u589e\u957f\u5f88\u53ef\u80fd\u76f4\u63a5\u62d6\u57ae\u6574\u4e2a\u7cfb\u7edf\u3002\u8fd9\u7bc7blog\u4e3b\u8981\u8bb2\u8bb2\u6211\u4eec\u4f5c\u4e3aDBA\uff0c\u600e\u6837\u5c3d\u91cf\u907f\u514dMySQL\u60e8\u906dswap\u7684\u6bd2\u624b\u3002<\/p>\n
\u9996\u5148\u6211\u4eec\u8981\u4e86\u89e3\u70b9\u57fa\u7840\u7684\u4e1c\u897f\uff0c\u6bd4\u5982\u8bf4\u4e3a\u4ec0\u4e48\u4f1a\u4ea7\u751fswap\u3002\u5047\u8bbe\u6211\u4eec\u7684\u7269\u7406\u5185\u5b58\u662f16G\uff0cswap\u662f4G\u3002\u5982\u679cMySQL\u672c\u8eab\u5df2\u7ecf\u5360\u7528\u4e8612G\u7269\u7406\u5185\u5b58\uff0c\u800c\u540c\u65f6\u5176\u4ed6\u7a0b\u5e8f\u6216\u8005\u7cfb\u7edf\u6a21\u5757\u53c8\u9700\u89816G\u5185\u5b58\uff0c\u8fd9\u65f6\u5019\u64cd\u4f5c\u7cfb\u7edf\u5c31\u53ef\u80fd\u628aMySQL\u6240\u62e5\u6709\u7684\u4e00\u90e8\u5206\u5730\u5740\u7a7a\u95f4\u6620\u5c04\u5230swap\u4e0a\u53bb\u3002<\/p>\n
cp\u4e00\u4e2a\u5927\u6587\u4ef6\uff0c\u6216\u7528mysqldump\u5bfc\u51fa\u4e00\u4e2a\u5f88\u5927\u7684\u6570\u636e\u5e93\u7684\u65f6\u5019\uff0c\u6587\u4ef6\u7cfb\u7edf\u5f80\u5f80\u4f1a\u5411Linux\u7533\u8bf7\u5927\u91cf\u7684\u5185\u5b58\u4f5c\u4e3acache\uff0c\u4e00\u4e0d\u5c0f\u5fc3\u5c31\u4f1a\u5bfc\u81f4L\u4f7f\u7528swap\u3002\u8fd9\u4e2a\u60c5\u666f\u6bd4\u8f83\u5e38\u89c1\uff0c\u4ee5\u4e0b\u662f\u6700\u7b80\u5355\u7684\u4e09\u4e2a\u8c03\u6574\u65b9\u6cd5\uff1a
\n1\u3001\/proc\/sys\/vm\/swappiness\u7684\u5185\u5bb9\u6539\u62100\uff08\u4e34\u65f6\uff09\uff0c\/etc\/sysctl.conf\u4e0a\u6dfb\u52a0vm.swappiness=0\uff08\u6c38\u4e45\uff09
\n\u8fd9\u4e2a\u53c2\u6570\u51b3\u5b9a\u4e86Linux\u662f\u503e\u5411\u4e8e\u4f7f\u7528swap\uff0c\u8fd8\u662f\u503e\u5411\u4e8e\u91ca\u653e\u6587\u4ef6\u7cfb\u7edfcache\u3002\u5728\u5185\u5b58\u7d27\u5f20\u7684\u60c5\u51b5\u4e0b\uff0c\u6570\u503c\u8d8a\u4f4e\u8d8a\u503e\u5411\u4e8e\u91ca\u653e\u6587\u4ef6\u7cfb\u7edfcache\u3002
\n\u5f53\u7136\uff0c\u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u51cf\u5c11\u4f7f\u7528swap\u7684\u6982\u7387\uff0c\u5e76\u4e0d\u80fd\u907f\u514dLinux\u4f7f\u7528swap\u3002
\n2\u3001\u4fee\u6539MySQL\u7684\u914d\u7f6e\u53c2\u6570innodb_flush_method\uff0c\u5f00\u542fO_DIRECT\u6a21\u5f0f\u3002
\n\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cInnoDB\u7684buffer pool\u4f1a\u76f4\u63a5\u7ed5\u8fc7\u6587\u4ef6\u7cfb\u7edfcache\u6765\u8bbf\u95ee\u78c1\u76d8\uff0c\u4f46\u662fredo log\u4f9d\u65e7\u4f1a\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edfcache\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cRedo log\u662f\u8986\u5199\u6a21\u5f0f\u7684\uff0c\u5373\u4f7f\u4f7f\u7528\u4e86\u6587\u4ef6\u7cfb\u7edf\u7684cache\uff0c\u4e5f\u4e0d\u4f1a\u5360\u7528\u592a\u591a\u3002
\n3\u3001\u6dfb\u52a0MySQL\u7684\u914d\u7f6e\u53c2\u6570memlock
\n\u8fd9\u4e2a\u53c2\u6570\u4f1a\u5f3a\u8febmysqld\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u4e00\u76f4\u88ab\u9501\u5b9a\u5728\u7269\u7406\u5185\u5b58\u4e0a\uff0c\u5bf9\u4e8eos\u6765\u8bf4\u662f\u975e\u5e38\u9738\u9053\u7684\u4e00\u4e2a\u8981\u6c42\u3002\u5fc5\u987b\u8981\u7528root\u5e10\u53f7\u6765\u542f\u52a8MySQL\u624d\u80fd\u751f\u6548\u3002<\/p>\n
\u8fd8\u6709\u4e00\u4e2a\u6bd4\u8f83\u590d\u6742\u7684\u65b9\u6cd5\uff0c\u6307\u5b9aMySQL\u4f7f\u7528\u5927\u9875\u5185\u5b58\uff08Large Page\uff09\u3002Linux\u4e0a\u7684\u5927\u9875\u5185\u5b58\u662f\u4e0d\u4f1a\u88ab\u6362\u51fa\u7269\u7406\u5185\u5b58\u7684\uff0c\u548cmemlock\u6709\u5f02\u66f2\u540c\u5de5\u4e4b\u5999\u3002
\n\u5177\u4f53\u7684\u914d\u7f6e\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003\uff1ahttp:\/\/harrison-fisk.blogspot.com\/2009\/01\/enabling-innodb-large-pages-on-linux.html<\/p>\n
\u8fd9\u91cc\u9700\u8981\u8865\u5145\u4e00\u4e0b\u4e0a\u97624\u79cd\u65b9\u6cd5\u539f\u7406\u548c\u5b9e\u73b0\u673a\u5236\uff0c\u5bf9\u4e8eLinux api\u4e0d\u611f\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u76f4\u63a5\u8df3\u8fc7\u3002<\/p>\n
\u4e00\u3001\u64cd\u4f5c\u7cfb\u7edf\u8bbe\u7f6eswap\u7684\u76ee\u7684<\/strong> \u4e8c\u3001Linux\u63d0\u4f9b\u7684\u51e0\u4e2aapi<\/strong> 2\u3001shmget()\u548cshmat()<\/strong> 3\u3001O_DIRECT\u548cposix_memalign()<\/strong> 4\u3001madvise()\u548cfadvise()<\/strong> \u4e09\u3001MySQL\u5185\u5b58\u4f7f\u7528\u76f8\u5173\u7684\u4e00\u4e9b\u4ee3\u7801<\/strong> 2\u3001large-pages<\/strong> 3\u3001innodb_flush_method<\/strong> \u4ee5\u4e0a\u7684\u5206\u6790\u57fa\u4e8e5.0.85\u7248\u672c\u7684\u539f\u7248MySQL\uff0cInnoDB\u662fInnobase\u3002 \u53c2\u8003\u6587\u732e\uff1a<\/strong> Linux\u6709\u5f88\u591a\u5f88\u597d\u7684\u5185\u5b58\u3001IO\u8c03\u5ea6\u673a\u5236\uff0c\u4f46\u662f\u5e76\u4e0d\u4f1a\u9002\u7528\u4e8e\u6240\u6709\u573a\u666f\u3002\u5bf9\u4e8eDBA\u6765\u8bf4Linux\u6bd4\u8f83\u8ba9\u4eba\u5934\u75bc\u7684\u4e00\u4e2a\u5730\u65b9\u662f\uff0c\u5b83\u4e0d\u4f1a\u56e0\u4e3aMySQL\u5f88\u91cd\u8981\u5c31\u907f\u514d\u5c06\u5206\u914d\u7ed9MySQL\u7684\u5730\u5740\u7a7a\u95f4\u6620\u5c04\u5230swap\u4e0a\u3002\u5bf9\u4e8e\u9891\u7e41\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u7684\u7cfb\u7edf\u800c\u8a00\uff0c\u6570\u636e\u770b\u4f3c\u5728\u5185\u5b58\u800c\u5b9e\u9645\u4e0a\u5728\u78c1\u76d8\u662f\u975e\u5e38\u7cdf\u7cd5\u7684\uff0c\u54cd\u5e94\u65f6\u95f4\u7684\u589e\u957f\u5f88\u53ef\u80fd\u76f4\u63a5\u62d6\u57ae\u6574\u4e2a\u7cfb\u7edf\u3002\u8fd9\u7bc7blog\u4e3b\u8981\u8bb2\u8bb2\u6211\u4eec\u4f5c\u4e3aDBA\uff0c\u600e\u6837\u5c3d\u91cf\u907f\u514dMySQL\u60e8\u906dswap\u7684\u6bd2\u624b\u3002
\n\u7a0b\u5e8f\u8fd0\u884c\u7684\u4e00\u4e2a\u5fc5\u8981\u6761\u4ef6\u5c31\u662f\u8db3\u591f\u7684\u5185\u5b58\uff0c\u800c\u5185\u5b58\u5f80\u5f80\u662f\u7cfb\u7edf\u91cc\u9762\u6bd4\u8f83\u7d27\u5f20\u7684\u4e00\u79cd\u8d44\u6e90\u3002\u4e3a\u4e86\u6ee1\u8db3\u66f4\u591a\u7a0b\u5e8f\u7684\u8981\u6c42\uff0c\u64cd\u4f5c\u7cfb\u7edf\u865a\u62df\u4e86\u4e00\u90e8\u5206\u5185\u5b58\u5730\u5740\uff0c\u5e76\u5c06\u4e4b\u6620\u5c04\u5230swap\u4e0a\u3002\u5bf9\u4e8e\u7a0b\u5e8f\u6765\u8bf4\uff0c\u5b83\u53ea\u77e5\u9053\u64cd\u4f5c\u7cfb\u7edf\u7ed9\u81ea\u5df1\u5206\u914d\u4e86\u5185\u5b58\u5730\u5740\uff0c\u4f46\u5e76\u4e0d\u6e05\u695a\u8fd9\u4e9b\u5185\u5b58\u5730\u5740\u5230\u5e95\u6620\u5c04\u5230\u7269\u7406\u5185\u5b58\u8fd8\u662fswap\u3002
\n\u7269\u7406\u5185\u5b58\u548cswap\u5728\u529f\u80fd\u4e0a\u662f\u4e00\u6837\u7684\uff0c\u53ea\u662f\u56e0\u4e3a\u7269\u7406\u5b58\u50a8\u5143\u4ef6\u7684\u4e0d\u540c\uff08\u5185\u5b58\u548c\u78c1\u76d8\uff09\uff0c\u6027\u80fd\u4e0a\u6709\u5f88\u5927\u7684\u5dee\u522b\u3002\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u6839\u636e\u7a0b\u5e8f\u4f7f\u7528\u5185\u5b58\u7684\u7279\u70b9\u8fdb\u884c\u6362\u5165\u548c\u6362\u51fa\uff0c\u5c3d\u53ef\u80fd\u5730\u628a\u7269\u7406\u5185\u5b58\u7559\u7ed9\u6700\u9700\u8981\u5b83\u7684\u7a0b\u5e8f\u3002\u4f46\u662f\u8fd9\u79cd\u8c03\u5ea6\u662f\u6309\u7167\u9884\u5148\u8bbe\u5b9a\u7684\u67d0\u79cd\u89c4\u5219\u7684\uff0c\u5e76\u4e0d\u80fd\u5b8c\u5168\u7b26\u5408\u7a0b\u5e8f\u7684\u9700\u8981\u3002\u4e00\u4e9b\u7279\u6b8a\u7684\u7a0b\u5e8f\uff08\u6bd4\u5982MySQL\uff09\u5e0c\u671b\u81ea\u5df1\u7684\u6570\u636e\u6c38\u8fdc\u5bc4\u5b58\u5728\u7269\u7406\u5185\u5b58\u91cc\uff0c\u4ee5\u4fbf\u63d0\u4f9b\u66f4\u9ad8\u7684\u6027\u80fd\u3002\u4e8e\u662f\u64cd\u4f5c\u7cfb\u7edf\u5c31\u8bbe\u7f6e\u4e86\u51e0\u4e2aapi\uff0c\u4ee5\u4fbf\u4e3a\u8c03\u7528\u8005\u63d0\u4f9b\u201c\u7279\u6b8a\u670d\u52a1\u201d\u3002<\/p>\n
\n1\u3001mlockall()\u548cmunlockall()<\/strong>
\n\u8fd9\u4e00\u5bf9\u51fd\u6570\uff0c\u53ef\u4ee5\u8ba9\u8c03\u7528\u8005\u7684\u5730\u5740\u7a7a\u95f4\u5e38\u9a7b\u7269\u7406\u5185\u5b58\uff0c\u4e5f\u53ef\u4ee5\u5728\u9700\u8981\u7684\u65f6\u5019\u5c06\u6b64\u7279\u6743\u53d6\u6d88\u3002mlockall()\u7684flag\u4f4d\u53ef\u4ee5\u662fMCL_CURRENT\u548cMCL_FUTURE\u7684\u4efb\u610f\u7ec4\u5408\uff0c\u5206\u522b\u4ee3\u8868\u4e86\u201c\u4fdd\u6301\u5df2\u5206\u914d\u7684\u5730\u5740\u7a7a\u95f4\u5e38\u9a7b\u7269\u7406\u5185\u5b58\u201d\u548c\u201c\u4fdd\u6301\u672a\u6765\u5206\u914d\u7684\u5730\u5740\u7a7a\u95f4\u5e38\u9a7b\u7269\u7406\u5185\u5b58\u201d\u3002\u5bf9\u4e8eLinux\u6765\u8bf4\uff0c\u8fd9\u5bf9\u51fd\u6570\u662f\u975e\u5e38\u9738\u9053\u7684\uff0c\u53ea\u6709root\u7528\u6237\u624d\u6709\u6743\u9650\u8c03\u7528\u3002<\/p>\n
\n\u8fd9\u4e00\u5bf9\u51fd\u6570\uff0c\u53ef\u4ee5\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u4f7f\u7528\u5927\u9875\u5185\u5b58\uff08Large Page\uff09\u3002\u5927\u9875\u5185\u5b58\u7684\u7279\u70b9\u662f\u9884\u5206\u914d\u548c\u6c38\u9a7b\u7269\u7406\u5185\u5b58\uff0c\u56e0\u4e3a\u4f7f\u7528\u4e86\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u65b9\u5f0f\uff0cpage table\u6709\u53ef\u80fd\u4f1a\u6bd4\u4f20\u7edf\u7684\u5c0f\u9875\u5206\u914d\u65b9\u5f0f\u66f4\u5c0f\u3002\u5bf9\u4e8e\u591a\u8fdb\u7a0b\u5171\u4eab\u5185\u5b58\u7684\u7a0b\u5e8f\uff08\u6bd4\u5982ORACLE\uff09\uff0c\u5927\u9875\u5185\u5b58\u80fd\u591f\u8282\u7701\u5f88\u591apage table\u5f00\u9500\uff1b\u800c\u5bf9\u4e8eMySQL\u6765\u8bf4\uff0c\u6027\u80fd\u548c\u8d44\u6e90\u5f00\u9500\u90fd\u6ca1\u6709\u663e\u8457\u53d8\u5316\uff0c\u597d\u5904\u5c31\u5728\u4e8e\u51cf\u5c11\u4e86\u5185\u5b58\u5730\u5740\u88ab\u6620\u5c04\u5230swap\u4e0a\u7684\u53ef\u80fd\u3002\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u662f\u51cf\u5c11\uff0c\u800c\u4e0d\u662f\u5b8c\u5168\u907f\u514d\uff0c\u4e4b\u540e\u518d\u8bb2\u89e3\u3002<\/p>\n
\n\u4ee5\u4e0a\u4e24\u4e2a\u65b9\u6cd5\u90fd\u4e0d\u4f1a\u51cf\u5c11\u5185\u5b58\u7684\u4f7f\u7528\u91cf\uff0c\u8c03\u7528\u8005\u7684\u672c\u610f\u662f\u83b7\u53d6\u66f4\u9ad8\u7684\u7cfb\u7edf\u7279\u6743\uff0c\u800c\u4e0d\u662f\u8282\u7ea6\u7cfb\u7edf\u8d44\u6e90\u3002O_DIRECT\u662f\u4e00\u79cd\u66f4\u52a0\u7406\u60f3\u5316\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u907f\u514ddouble buffer\uff0c\u8282\u7701\u4e86\u6587\u4ef6\u7cfb\u7edfcache\u7684\u5f00\u9500\uff0c\u6700\u7ec8\u51cf\u5c11swap\u7684\u4f7f\u7528\u7387\u3002O_DIRECT\u662fLinux IO\u8c03\u5ea6\u76f8\u5173\u7684\u6807\u5fd7\uff0c\u5728open\u51fd\u6570\u91cc\u9762\u8c03\u7528\u3002\u901a\u8fc7O_DIRECT\u6807\u5fd7\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u8bfb\u5199\u90fd\u4e0d\u4f1a\u7528\u5230\u6587\u4ef6\u7cfb\u7edf\u7684cache\u3002\u4f20\u7edf\u7684\u6570\u636e\u5e93\uff08ORACLE\u3001MySQL\uff09\u57fa\u672c\u90fd\u6709O_DIRECT\u76f8\u5173\u7684\u5f00\u5173\uff0c\u5728\u63d0\u9ad8\u6027\u80fd\u7684\u540c\u65f6\uff0c\u4e5f\u51cf\u5c11\u4e86\u5185\u5b58\u7684\u4f7f\u7528\u3002\u81f3\u4e8eposix_memalign()\uff0c\u662f\u7528\u6765\u7533\u8bf7\u5bf9\u9f50\u7684\u5185\u5b58\u5730\u5740\u7684\u3002\u53ea\u6709\u7528posix_memalign()\u7533\u8bf7\u7684\u5185\u5b58\u5730\u5740\uff0c\u624d\u80fd\u7528\u6765\u8bfb\u5199O_DIRECT\u6a21\u5f0f\u4e0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n
\n\u8fd9\u5bf9\u51fd\u6570\u4e5f\u662f\u6bd4\u8f83\u6e29\u548c\u7684\uff0c\u53ef\u4ee5\u5c06\u8c03\u7528\u8005\u5bf9\u6570\u636e\u8bbf\u95ee\u6a21\u5f0f\u7684\u9884\u671f\u4f20\u9012\u7ed9Linux\uff0c\u4ee5\u671f\u5f97\u5230\u66f4\u597d\u7684\u6027\u80fd\u3002
\n\u6211\u4eec\u6bd4\u8f83\u611f\u5174\u8da3\u7684\u662fMADV_DONTNEED\u548cFADV_NOREUSE\u8fd9\u4e24\u4e2aflag\u3002\u524d\u8005\u4f1a\u5efa\u8baeLinux\u91ca\u653e\u6307\u5b9a\u7684\u5185\u5b58\u533a\u57df\uff0c\u800c\u540e\u8005\u4f1a\u5efa\u8bae\u6587\u4ef6\u7cfb\u7edf\u91ca\u653e\u6307\u5b9a\u6587\u4ef6\u6240\u5360\u7528\u7684cache\u3002<\/p>\n
\n1\u3001memlock<\/strong>
\n\u5728MySQL\u7684\u6e90\u7801\u76ee\u5f55\u91cc\u9762\u67e5\u8be2memlock\uff0c\u53ef\u4ee5\u77e5\u9053\u8fd9\u4e2a\u53c2\u6570\u7684\u4f5c\u7528\u662f\u4f7fMySQL\u8c03\u7528mlockall()\u3002\u5728\u6e90\u7801\u91cc\u9762\u5339\u914d\u53ef\u4ee5\u5f97\u77e5NDB\u3001MyISAM\u548cmysqld\u90fd\u8c03\u7528\u4e86mlockall()\u3002NDB\u662f\u53ef\u4ee5\u72ec\u7acb\u4e8eMySQL\u800c\u5b58\u5728\u7684\u5b58\u50a8\u5f15\u64ce\uff0c\u6b64\u5904\u6309\u4e0b\u4e0d\u8868\u3002mysqld\u8c03\u7528mlockall()\u7684\u65b9\u5f0f\u6709\u70b9\u51fa\u4e4e\u610f\u6599\uff0c\u5728init_server_components()\u51fd\u6570\u91cc\u4f20\u7ed9mlockall()\u7684flag\u662fMCL_CURRENT\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e4b\u540e\u7533\u8bf7\u7684\u5185\u5b58\u4e00\u6982\u4e0d\u7528\u9501\u4f4f\u3002\u518d\u770b\u770bMyISAM\u7684\u8c03\u7528\u987a\u5e8f\u662f\uff1amlockall() <- lock_memory() <- mi_repair()\uff0cMyISAM\u53ea\u6709\u4fee\u590d\u7684\u65f6\u5019\u4f1a\u8c03\u7528mlockall()\u51fd\u6570\u3002<\/p>\n
\n\u6839\u636eLinux\u7684\u5185\u6838\u6587\u6863\uff0c\u5927\u9875\u5185\u5b58\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u5230\uff1a\u4e00\u79cd\u662f\u521b\u5efahugetlb\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5b83mmap\u5230\u7a0b\u5e8f\u7684\u5185\u5b58\u5730\u5740\u91cc\u9762\uff0c\u7136\u540e\u8fdb\u884c\u6b63\u5e38\u7684\u8bfb\u5199\u64cd\u4f5c\u3002\u53e6\u5916\u4e00\u79cd\u662f\u4e4b\u524d\u8bf4\u5230\u7684shmget()+shmat()\uff0c\u4e5f\u6b63\u662fMySQL\u91c7\u7528\u7684\u65b9\u5f0f\u3002\u5728MySQL\u7684\u6e90\u7801\u76ee\u5f55\u91cc\u9762\u5339\u914dshmget\uff0c\u53ef\u4ee5\u53d1\u73b0BDB\u3001NDB\u3001InnoDB\u3001MyISAM\u90fd\u8c03\u7528\u4e86\u8fd9\u4e2a\u51fd\u6570\u3002\u63a5\u7740\u770b\u4e00\u4e0b\u6bd4\u8f83\u5e38\u7528\u7684InnoDB\u548cMyISAM\u5f15\u64ce\u3002
\n\u5728InnoDB\u91cc\u9762\u53ef\u4ee5\u627e\u5230os_mem_alloc_large()\u8c03\u7528\u4e86shmget()\uff0c\u800c\u8c03\u7528os_mem_alloc_large()\u7684\u51fd\u6570\u53ea\u6709buf_pool_init()\u2015\u2015InnoDB Buffer Pool\u7684\u521d\u59cb\u5316\u51fd\u6570\u3002\u6839\u636e\u89c2\u5bdf\u5f97\u5230\u7684\u7ed3\u8bba\u662f\uff0cInnoDB\u4f1a\u6839\u636e\u914d\u7f6e\u53c2\u6570\u5728Buffer Pool\u91cc\u9762\u4f7f\u7528\u5927\u9875\u5185\u5b58\uff0cRedo log\u8c8c\u4f3c\u5c31\u6ca1\u6709\u8fd9\u4e2a\u5f85\u9047\u4e86\u3002
\n\u5bf9\u4e8eMyISAM\uff0c\u5728storage\u5c42\u7ea7\u7684\u4ee3\u7801\u91cc\u9762\u627e\u4e0d\u5230\u5bf9shmget()\u7684\u76f4\u63a5\u8c03\u7528\u3002\u8fd9\u662f\u56e0\u4e3aMyISAM\u662fMySQL\u7684\u539f\u751f\u5b58\u50a8\u5f15\u64ce\uff0c\u5f88\u591a\u51fd\u6570\u5b58\u653e\u5728\u4e0a\u4e00\u5c42\u7684mysys\u76ee\u5f55\u91cc\u9762\u3002\u901a\u8fc7\u641c\u7d22shmget()\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230MyISAM\u7684\u8c03\u7528\u987a\u5e8f\u662f\u8fd9\u6837\u7684\uff1ashmget() <- my_large_malloc_int() <- my_large_malloc() <- init_key_cache()\u3002\u4e5f\u5c31\u662f\u8bf4MyISAM\u53ea\u6709\u7d22\u5f15\u7f13\u5b58\u7528\u5230\u4e86\u5927\u9875\u5185\u5b58\uff0c\u8fd9\u662f\u5f88\u5bb9\u6613\u7406\u89e3\uff0c\u56e0\u4e3aMyISAM\u7684\u6570\u636e\u662f\u76f4\u63a5\u6254\u7ed9\u6587\u4ef6\u7cfb\u7edf\u505a\u7f13\u5b58\u7684\uff0c\u6ca1\u6cd5\u4f7f\u7528\u5927\u9875\u5185\u5b58\u3002<\/p>\n
\nO_DIRECT\u662fBDB\u3001NDB\u3001InnoDB\u7279\u6709\u7684\u53c2\u6570\uff0c\u5728\u8fd9\u91cc\u53ea\u8ba8\u8bbaInnoDB\u8fd9\u4e2a\u6bd4\u8f83\u5e38\u89c1\u7684\u5f15\u64ce\u3002\u5728InnoDB\u7684\u6e90\u7801\u76ee\u5f55\u91cc\u9762\u5339\u914dO_DIRECT\uff0c\u5f88\u5bb9\u6613\u627e\u5230\u4e00\u4e2a\u53eb\u505aos_file_set_nocache()\u7684\u51fd\u6570\uff0c\u800c\u8fd9\u4e2a\u51fd\u6570\u4f5c\u7528\u662f\u5c06\u6587\u4ef6\u7684\u6253\u5f00\u65b9\u5f0f\u6539\u4e3aO_DIRECT\u6a21\u5f0f\u3002\u518d\u8ddf\u8e2a\u4e00\u4e0b\uff0c\u4f1a\u53d1\u73b0\u53ea\u6709os_file_create()\u51fd\u6570\u8c03\u7528\u4e86os_file_set_nocache()\u3002\u867d\u7136\u51fd\u6570\u540d\u91cc\u9762\u8fd8\u6709create\uff0c\u5b9e\u9645\u4e0aos_file_create()\u4f1a\u6839\u636e\u4f20\u5165\u53c2\u6570\u7684\u4e0d\u540c\uff0c\u9009\u62e9\u6253\u5f00\u6216\u8005\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u3002\u540c\u65f6os_file_create()\u8fd8\u4f1a\u6839\u636eMySQL\u7684\u914d\u7f6e\uff0c\u6765\u8c03\u7528os_file_set_nocache()\u5173\u95ed\u6587\u4ef6\u7cfb\u7edf\u7684\u76f8\u5e94cache\u3002\u5728os_file_create()\u51fd\u6570\u91cc\u9762\u6709\u5982\u4e0b\u4e00\u6bb5\u4ee3\u7801\uff1a
\n\/* We disable OS caching (O_DIRECT) only on data files *\/
\nif (type != OS_LOG_FILE &&
\nsrv_unix_file_flush_method == SRV_UNIX_O_DIRECT)
\n{
\nos_file_set_nocache(file, name, mode_str);
\n}
\n\u8fd9\u6bb5\u4ee3\u7801\u7684\u610f\u601d\u662f\uff0c\u53ea\u6709InnoDB\u7684\u6570\u636e\u6587\u4ef6\u6709\u8d44\u683c\u4f7f\u7528O_DIRECT\u6a21\u5f0f\uff0cRedo log\u662f\u4e0d\u80fd\u4f7f\u7528\u7684\u3002<\/p>\n
\n\u7248\u672c\u4e0d\u540c\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u51fa\u5165\uff0c\u6b22\u8fce\u53c2\u4e0e\u8ba8\u8bba\u3002<\/p>\n
\nVirtual memory@wiki<\/span><\/a>
\nAll about Linux swap space<\/span><\/a>
\nHugeTLB C Large Page Support in the Linux Kernel<\/span><\/a>
\nPage table@wiki<\/span><\/a>
\n\u539f\u6587\u00a0<\/strong><\/span>http:\/\/www.taobaodba.com\/html\/552_mysql_avoid_swap.html<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"
<\/span>
\u9996\u5148\u6211\u4eec\u8981\u4e86\u89e3\u70b9\u57fa\u7840\u7684\u4e1c\u897f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1147"}],"collection":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/comments?post=1147"}],"version-history":[{"count":1,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1147\/revisions"}],"predecessor-version":[{"id":1293,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1147\/revisions\/1293"}],"wp:attachment":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/media?parent=1147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/categories?post=1147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/tags?post=1147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}