{"id":2591,"date":"2010-10-21T23:19:27","date_gmt":"2010-10-21T15:19:27","guid":{"rendered":"http:\/\/www.orczhou.com\/?p=2591"},"modified":"2010-11-08T19:35:05","modified_gmt":"2010-11-08T11:35:05","slug":"mysql-open-file-limit","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2010\/10\/mysql-open-file-limit\/","title":{"rendered":"MySQL\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236"},"content":{"rendered":"<p>\u5982\u679c\u9047\u5230\u5982\u4e0b\u9519\u8bef:<\/p>\n<div class=\"mycode\">Can&#8217;t open file: &#8216;.\\test\\mytable.frm&#8217; (errno: 24)<br \/>\nshell> perror 24<br \/>\nOS error code  24:  Too many open files\n<\/div>\n<p>\u8fd9\u5c31\u662fMySQL\u7684\u6587\u4ef6\u63cf\u8ff0\u4e0d\u591f\u7528\u4e86\u3002\u5148\u8bf4\u89e3\u51b3\u529e\u6cd5\uff0c\u518d\u8bf4\u80cc\u540e\u7684\u539f\u56e0\u5427\u3002<\/p>\n<div class=\"myt1\">1. \u5982\u4f55\u89e3\u51b3<\/div>\n<p><span style=\"color:blue\">\u7b2c\u4e00\u6b65\uff1a<\/span>\u8bbe\u7f6eOS\u53c2\u6570\uff08\u5982\u679c\u4f60\u6709\u6743\u9650\u7684\u8bdd\uff09\uff1a<\/p>\n<p>\u6587\u4ef6\/etc\/security\/limits.conf\u65b0\u589e\u5982\u4e0b\u884c\uff1a<\/p>\n<div class=\"mycode\">mysql   soft    nofile  65535<br \/>\nmysql   hard    nofile  65535<\/div>\n<p>\u4e0a\u9762\u7684\u914d\u7f6e\uff0c\u662fOS\u9650\u5236\u5404\u4e2a\u7528\u6237\u80fd\u591f\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\uff08hard soft\u533a\u522b\u53c2\u770bman ulimit\uff09\uff0c\u65b0\u589e\u4e0a\u9762\u4e24\u884c\uff0c\u8868\u793amysql\u7528\u6237\u80fd\u591f\u6253\u5f0065535\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08\u53ef\u4ee5\u4f7f\u7528lsof -u mysql|wc -l\u67e5\u770b\u5f53\u524d\u6253\u5f00\u4e86\u591a\u5c11\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff09\u3002<\/p>\n<p><span style=\"color:blue\">\u7b2c\u4e8c\u6b65\uff1a<\/span>\u4fee\u6539MySQL\u53c2\u6570\uff1a<br \/>\n\u5728MySQL\u914d\u7f6e\u6587\u4ef6my.cnf\u4e2d\u65b0\u589e\u4e0b\u9762\u7684\u884c<\/p>\n<div class=\"mycode\">open_files_limit = 65535<br \/>\ninnodb_open_files=65535\n<\/div>\n<p>\u7136\u540e\u91cd\u542f\u4f60\u7684MySQL\u4e00\u822c\u95ee\u9898\u5c31\u89e3\u51b3\u4e86\u3002<\/p>\n<div class=\"myt1\">2. \u80cc\u540e\u7684\u95ee\u9898<\/div>\n<p><!--more--><br \/>\n\u4e0a\u9762\u7684\u529e\u6cd5\u4e00\u822c\u5c31\u80fd\u89e3\u51b3\u95ee\u9898\u4e86\u3002<strong>\u4e0d\u8fc7\u5728\u5b9e\u8df5\u4e2d\u53d1\u73b0\uff0c\u5728my.cnf\u4e2d\u8bbe\u7f6e\u7684\u53c2\u6570open_files_limit\u503c\u662f\u65e0\u6548\u7684<\/strong>\uff0c\u5373MySQL\u542f\u52a8\u540eopen_files_limit\u59cb\u7ec8\u4ee5OS\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u51c6\u3002\uff08\u7248\u672cMySQL5.1.45 RHEL5.4\uff09<\/p>\n<pre><div class=\"mycode\">root@(none) 11:27:07>show global variables like \"%open_files_limit%\";\r\n+------------------+-------+\r\n| Variable_name    | Value |\r\n+------------------+-------+\r\n| open_files_limit | 20000 |\r\n+------------------+-------+\r\n1 row in set (0.01 sec)\r\n\r\nroot@(none) 11:27:09>system ulimit -n\r\n20000<\/div><\/pre>\n<p><strong>\u90a3my.cnf\u53c2\u6570open_files_limit\u662f\u5426\u771f\u7684\u662f\u6ca1\u7528\u5462\uff1f<\/strong>\u63a5\u4e0b\u6765\u4f1a\u662f\u4e00\u7bc7\u5f88\u957f\u3001\u5f88\u86cb\u75bc\u7684\u5173\u4e8e\u8be5\u95ee\u9898\u7684\u7814\u7a76\uff0c\u5982\u679c\u4e0d\u662f\u5f88\u6709\u65f6\u95f4\uff0c\u4e0d\u5efa\u8bae\u770b\u4e0b\u53bb\u3002<\/p>\n<div class=\"myt1\">3. \u6e90\u4ee3\u7801\u4e2d\u5982\u4f55\u8bbe\u7f6eopen_files_limit<\/div>\n<div class=\"myt2\">3.1 \u5b9e\u9a8c\u9a8c\u8bc1<\/div>\n<p>\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\uff1aopen_files_limit = 10000\uff1b$ulimit -n 20000\uff1b\u542f\u52a8\u6570\u636e\u5e93\uff0c\u89c2\u5bdf\uff1a<\/p>\n<pre><div class=\"mycode\">root@(none) 11:48:57>show global variables like \"%open_files_limit%\";\r\n+------------------+-------+\r\n| Variable_name    | Value |\r\n+------------------+-------+\r\n| open_files_limit | 20000 |\r\n+------------------+-------+<\/div><\/pre>\n<p>\u770b\u5230\u53c2\u6570open_files_limit\u786e\u5b9e\u6ca1\u6709\u4f5c\u7528\uff08\u5df2\u7ecf\u5b9e\u9a8c\u4e86\u5f88\u591a\u6b21\u4e86\uff09\u3002<\/p>\n<div class=\"myt2\">3.1 \u4ee3\u7801\u8ffd\u8e2awatch<\/div>\n<p>\u9996\u5148\u4f7f\u7528gdb\/watch\u89c2\u5bdfopen_files_limit\u53d8\u91cf\u503c\uff0c<\/p>\n<pre><div class=\"mycode\">Old value = 0\r\nNew value = 10000\r\n0x000000000086a6f2 in setval ()\r\n\r\nOld value = 10000\r\nNew value = 20000\r\n0x00000000005ba159 in init_common_variables ()<\/div><\/pre>\n<p>\u770b\u5230\uff0c\u914d\u7f6e\u6587\u4ef6my.cnf\u4e2d\u768410000\uff0c\u53c8\u6ca1\u6709\u751f\u6548\u3002<\/p>\n<div class=\"myt2\">3.2 \u68a6\u5883\uff1a\u66f4\u591a\u7684\u4ee3\u7801\u7ec6\u8282<\/div>\n<pre><div class=\"mycode\">    wanted_files= 10+max_connections+table_cache_size*2;\r\n    max_open_files= max(max(wanted_files, max_connections*5),\r\n                        open_files_limit);\r\n    files= my_set_max_open_files(max_open_files);<\/div><\/pre>\n<p>\u8fd9\u4e00\u6bb5\u53ef\u4ee5\u770b\u5230\uff0c\u9996\u5148\u4f1a\u6bd4\u8f83open_files_limit\uff0cmax_connections*5\u548c10+max_connections+table_cache_size*2\u4e2d\u6700\u5927\u503c\uff0c\u5e76\u8c03\u7528\u51fd\u6570my_set_max_open_files\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\u3002<\/p>\n<p>\u7ee7\u7eed\u8ddf\u8e2a\u51fd\u6570my_set_max_open_files\uff0c\u5728\u6587\u4ef6mysys\/my_file.c\u4e2d\uff1a<\/p>\n<pre><div class=\"mycode\">uint my_set_max_open_files(uint files)\r\n{\r\n  struct st_my_file_info *tmp;\r\n  DBUG_ENTER(\"my_set_max_open_files\");\r\n  DBUG_PRINT(\"enter\",(\"files: %u  my_file_limit: %u\", files, my_file_limit));\r\n\r\n  files= set_max_open_files(min(files, OS_FILE_LIMIT));     ## #define OS_FILE_LIMIT\t65535 ##<\/div><\/pre>\n<p>\u8fd9\u91cc\u7ee7\u7eed\uff0c\u8c03\u7528\u4e86set_max_open_files\uff0c\u4ecd\u7136\u5728\u6587\u4ef6mysys\/my_file.c\uff1a<\/p>\n<pre><div class=\"mycode\">static uint set_max_open_files(uint max_file_limit)\r\n{\r\n  struct rlimit rlimit;\r\n  uint old_cur;\r\n  DBUG_ENTER(\"set_max_open_files\");\r\n  DBUG_PRINT(\"enter\",(\"files: %u\", max_file_limit));\r\n\r\n  if (!getrlimit(RLIMIT_NOFILE,&rlimit))\r\n  {\r\n    old_cur= (uint) rlimit.rlim_cur;\r\n    DBUG_PRINT(\"info\", (\"rlim_cur: %u  rlim_max: %u\",\r\n                        (uint) rlimit.rlim_cur,\r\n                        (uint) rlimit.rlim_max));\r\n    if (rlimit.rlim_cur == RLIM_INFINITY)\r\n      rlimit.rlim_cur = max_file_limit;\r\n    if (rlimit.rlim_cur >= max_file_limit)\r\n      DBUG_RETURN(rlimit.rlim_cur);             \/* purecov: inspected *\/\r\n    rlimit.rlim_cur= rlimit.rlim_max= max_file_limit;\r\n    if (setrlimit(RLIMIT_NOFILE, &rlimit))\r\n      max_file_limit= old_cur;                  \/* Use original value *\/\r\n    else\r\n    {\r\n      rlimit.rlim_cur= 0;                       \/* Safety if next call fails *\/\r\n      (void) getrlimit(RLIMIT_NOFILE,&rlimit);\r\n      DBUG_PRINT(\"info\", (\"rlim_cur: %u\", (uint) rlimit.rlim_cur));\r\n      if (rlimit.rlim_cur)                      \/* If call didn't fail *\/\r\n        max_file_limit= (uint) rlimit.rlim_cur;\r\n    }\r\n  }\r\n  DBUG_PRINT(\"exit\",(\"max_file_limit: %u\", max_file_limit));\r\n  DBUG_RETURN(max_file_limit);\r\n}<div><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff1a<\/p>\n<pre><div class=\"mycode\">    if (rlimit.rlim_cur >= max_file_limit)\r\n      DBUG_RETURN(rlimit.rlim_cur);             \/* purecov: inspected *\/<\/div><\/pre>\n<p>\u770b\u5230\uff0c\u5982\u679c\u5f53\u524d\u7684OS\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08rlimit.rlim_cur\uff09\u9650\u5236\u5927\u4e8e\u914d\u7f6e\u6587\u4ef6\u7684\u53c2\u6570max_file_limit\uff0c\u5219\u51fd\u6570\u8fd4\u56de\u4e0d\u518d\u5bf9\u4f7f\u7528\u63cf\u8ff0\u7b26\u505a\u4efb\u4f55\u9650\u5236\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5f53\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\u7684open_files_limit\u503c\u5c0f\u4e8eOS\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\u65f6\uff0c\u5c06\u4e0d\u4f1a\u8c03\u7528setrlimit\uff0c\u5373\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\u662fOS\u7684\u9650\u5236\u503c\u3002<\/p>\n<p>\u8fd9\u91cc\u4ee3\u7801\u8fd8\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679copen_files_limit\u7684\u503c\u5927\u4e8eOS\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\u65f6\uff0c\u5c06\u4f1a<strong>\u5c1d\u8bd5<\/strong>\u8c03\u7528setrlimit(RLIMIT_NOFILE, &#038;rlimit)\u5c06\u63cf\u8ff0\u7b26\u8bbe\u7f6eopen_files_limit\uff0c\u4e8b\u5b9e\u4e0a\uff0csetrlimit\u4f1a\u8c03\u7528\u5931\u8d25\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5f53\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\u7684open_files_limit\u503c\u5927\u4e8eOS\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\u65f6\uff0c\u4f1a\u8c03\u7528setrlimit\uff0c\u4f46\u662f\u4f1a\u5931\u8d25\uff0c\u5373\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\u4ecd\u7136\u662fOS\u7684\u9650\u5236\u503c\u3002<\/p>\n<p><strong>\u5373\uff0c\u65e0\u8bba\u5982\u4f55\uff0cMySQL\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\u90fd\u662fOS\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\uff0c\u548c\u914d\u7f6e\u6587\u4ef6\u4e2dopen_files_limit\u7684\u8bbe\u7f6e\u6ca1\u6709\u5173\u7cfb\u3002<\/strong><\/p>\n<p>\uff08\u5982\u679c\u770b\u5230\u8fd9\uff0c\u8bf4\u660e\u4f60\u5df2\u7ecf\u6210\u529f\u8fdb\u5165<a href=\"http:\/\/movie.douban.com\/subject\/3541415\/\">\u7b2c\u4e8c\u5c42\u68a6\u5883<\/a>\u4e86\uff1amy_set_max_open_files\u548cset_max_open_files\uff09<\/p>\n<div class=\"myt1\">4 \u4ee3\u7801\u5916\u7684\u4ee3\u7801<\/div>\n<p>\u4f30\u8ba1\u770b\u5230\u8fd9\uff0c\u770b\u5ba2\u5df2\u7ecf\u6655\u4e86\u5427\u3002\u56e0\u4e3a\u8fd9\u4e2a\u95ee\u9898\u5e76\u6ca1\u6709\u90a3\u4e48\u91cd\u8981\uff0c\u89e3\u51b3\u529e\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u6240\u4ee5\u786e\u5b9e\u4e5f\u4e0d\u7528\u8ffd\u7a76\u7684\u8fd9\u4e48\u7ec6\u3002\u4e0d\u8fc7\u90fd\u5230\u8fd9\u513f\u4e86\uff0c\u6211\u6253\u7b97\u7ee7\u7eed\u3002<\/p>\n<p>\u5728\u5982\u679c\u60f3\u8bbe\u7f6eopen-files-limit\u4e5f\u6709\u6210\u529f\u7684\u65f6\u5019\uff0c\u90a3\u5c31\u662f\u4f7f\u7528root\u5e10\u53f7\uff0c\u8fd0\u884cmysqld_safe\u811a\u672c\u542f\u52a8MySQL\uff08\u6216\u8005\u4f7f\u7528mysql.server\u542f\u52a8\uff09\uff0c\u5982\u679c\u4f7f\u7528&#8211;open-files-limit\u662f\u53ef\u4ee5\u6210\u529f\u8bbe\u7f6e\u7684\uff1a<\/p>\n<pre><div class=\"mycode\">.\/mysqld_safe --open-files-limit=25000 &\r\nroot@(none) 02:50:54>show variables like \"%open_files_limit%\";\r\n+------------------+-------+\r\n| Variable_name    | Value |\r\n+------------------+-------+\r\n| open_files_limit | 25000 |\r\n+------------------+-------+\r\n<\/div><\/pre>\n<p>\u8fd9\u662f\u56e0\u4e3a\u5728mysqld_safe\u4e2d\u505a\u4e86\u5982\u4e0b\u5904\u7406\uff1a<\/p>\n<pre><div class=\"mycode\">\r\nif test -w \/ -o \"$USER\" = \"root\"\r\nthen\r\n  ......\r\n  if test -n \"$open_files\"\r\n  then\r\n    ulimit -n $open_files\r\n    append_arg_to_args \"--open-files-limit=$open_files\"\r\n  fi  \r\nfi   \r\n<\/div><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u76f4\u63a5\u6216\u95f4\u63a5\u4f7f\u7528mysqld_safe\u542f\u52a8MySQL\u65f6\uff0c\u5176\u5b9e\u662f\u5728\u542f\u52a8mysqld\u7a0b\u5e8f\u4e4b\u524d\uff0c\u8c03\u7528\u4e86ulimit -n $open_files\u6765\u5b9e\u73b0\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u9650\u5236\u3002<\/p>\n<p>\u53c2\u8003\uff1a<\/p>\n<p>1. <a href=\"http:\/\/dev.mysql.com\/downloads\/mysql\/5.1.html\">MySQL Source Code<\/a><\/p>\n<p>2. <a href=\"http:\/\/imysql.cn\/2008_04_18_how_mysql_calculate_open_files\">mysql\u5982\u4f55\u8ba1\u7b97\u6253\u5f00\u6587\u4ef6\u6570<\/a><\/p>\n<p>3. <a href=\"http:\/\/dev.mysql.com\/doc\/\">MySQL Manual<\/a><\/p>\n<p>\u5e7f\u544a\u65f6\u95f4\uff1a<a href=\"http:\/\/www.orczhou.com\/index.php\/projects\/we-are-hunting-mysql-hacker\/\">\u5de5\u4f5c\u673a\u4f1a&#8211;MySQL Hacker<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u9047\u5230\u5982\u4e0b\u9519\u8bef: Can&#8217;t open file: &#8216;.\\test\\mytable.frm&#8217; (errno: 24) shell> perror 24 OS error code 24: Too many open files \u8fd9\u5c31\u662fMySQL\u7684\u6587\u4ef6\u63cf\u8ff0\u4e0d\u591f\u7528\u4e86\u3002\u5148\u8bf4\u89e3\u51b3\u529e\u6cd5\uff0c\u518d\u8bf4\u80cc\u540e\u7684\u539f\u56e0\u5427\u3002 1. \u5982\u4f55\u89e3\u51b3 \u7b2c\u4e00\u6b65\uff1a\u8bbe\u7f6eOS\u53c2\u6570\uff08\u5982\u679c\u4f60\u6709\u6743\u9650\u7684\u8bdd\uff09\uff1a \u6587\u4ef6\/etc\/security\/limits.conf\u65b0\u589e\u5982\u4e0b\u884c\uff1a mysql soft nofile 65535 mysql hard nofile 65535 \u4e0a\u9762\u7684\u914d\u7f6e\uff0c\u662fOS\u9650\u5236\u5404\u4e2a\u7528\u6237\u80fd\u591f\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236\uff08hard soft\u533a\u522b\u53c2\u770bman ulimit\uff09\uff0c\u65b0\u589e\u4e0a\u9762\u4e24\u884c\uff0c\u8868\u793amysql\u7528\u6237\u80fd\u591f\u6253\u5f0065535\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08\u53ef\u4ee5\u4f7f\u7528lsof -u mysql|wc -l\u67e5\u770b\u5f53\u524d\u6253\u5f00\u4e86\u591a\u5c11\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff09\u3002 \u7b2c\u4e8c\u6b65\uff1a\u4fee\u6539MySQL\u53c2\u6570\uff1a \u5728MySQL\u914d\u7f6e\u6587\u4ef6my.cnf\u4e2d\u65b0\u589e\u4e0b\u9762\u7684\u884c open_files_limit = 65535 innodb_open_files=65535 \u7136\u540e\u91cd\u542f\u4f60\u7684MySQL\u4e00\u822c\u95ee\u9898\u5c31\u89e3\u51b3\u4e86\u3002 2. \u80cc\u540e\u7684\u95ee\u9898<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-2591","post","type-post","status-publish","format-standard","hentry","category-mysql","category-code-staff"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/2591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=2591"}],"version-history":[{"count":38,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/2591\/revisions"}],"predecessor-version":[{"id":2647,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/2591\/revisions\/2647"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=2591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=2591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=2591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}