{"id":21443,"date":"2024-01-02T09:28:24","date_gmt":"2024-01-02T09:28:24","guid":{"rendered":"https:\/\/server.hk\/blog\/21443\/"},"modified":"2024-11-14T10:34:17","modified_gmt":"2024-11-14T02:34:17","slug":"how-to-fix-postgresql-error-code-53300-too_many_connections","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21443\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a53300 &#8211; too_many_connections"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-53300-too_many_connections-UGMOiTFfvT\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a53300 &#8211; too_many_connections<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5168\u7403\u7684\u4f01\u696d\u548c\u7d44\u7e54\u3002\u7136\u800c\uff0c\u50cf\u5176\u4ed6\u4efb\u4f55\u8edf\u4ef6\u4e00\u6a23\uff0cPostgreSQL \u4e5f\u53ef\u80fd\u6703\u9047\u5230\u6703\u5e72\u64fe\u5176\u6b63\u5e38\u904b\u4f5c\u7684\u932f\u8aa4\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a53300 &#8211; too_many_connections\u3002<\/p>\n<h2 id=\"understanding-the-error-UGMOiTFfvT\">\u7406\u89e3\u932f\u8aa4<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a53300 &#8211; too_many_connections \u767c\u751f\u5728\u9054\u5230\u6578\u64da\u5eab\u7684\u6700\u5927\u540c\u6642\u9023\u63a5\u6578\u6642\u3002\u6b64\u932f\u8aa4\u901a\u5e38\u8868\u793a\u6578\u64da\u5eab\u4f3a\u670d\u5668\u88ab\u9023\u63a5\u8acb\u6c42\u6df9\u6c92\uff0c\u7121\u6cd5\u5728\u8a72\u6642\u523b\u8655\u7406\u66f4\u591a\u7684\u9023\u63a5\u3002<\/p>\n<p>\u7576\u6b64\u932f\u8aa4\u767c\u751f\u6642\uff0c\u7528\u6236\u53ef\u80fd\u6703\u9047\u5230\u97ff\u61c9\u6642\u9593\u8b8a\u6162\u3001\u8d85\u6642\uff0c\u751a\u81f3\u7121\u6cd5\u5b8c\u5168\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u53ca\u6642\u89e3\u6c7a\u6b64\u554f\u984c\u5c0d\u78ba\u4fdd\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\u5e73\u7a69\u904b\u884c\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2 id=\"identifying-the-cause-UGMOiTFfvT\">\u8b58\u5225\u539f\u56e0<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a53300 &#8211; too_many_connections \u53ef\u80fd\u6709\u5e7e\u500b\u6f5b\u5728\u539f\u56e0\uff1a<\/p>\n<ul>\n<li><strong>\u9023\u63a5\u6c60\u4e0d\u8db3\uff1a<\/strong>\u5982\u679c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u672a\u6709\u6548\u4f7f\u7528\u9023\u63a5\u6c60\uff0c\u53ef\u80fd\u6703\u8fc5\u901f\u8017\u76e1\u53ef\u7528\u9023\u63a5\u4e26\u89f8\u767c\u6b64\u932f\u8aa4\u3002<\/li>\n<li><strong>\u4f3a\u670d\u5668\u8cc7\u6e90\u4e0d\u8db3\uff1a<\/strong>\u5982\u679c\u60a8\u7684\u4f3a\u670d\u5668\u6c92\u6709\u8db3\u5920\u7684\u8cc7\u6e90\uff08CPU\u3001\u5167\u5b58\u7b49\uff09\u4f86\u8655\u7406\u9032\u4f86\u7684\u9023\u63a5\u8acb\u6c42\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4 too_many_connections \u932f\u8aa4\u3002<\/li>\n<li><strong>\u9023\u63a5\u6301\u7e8c\u6642\u9593\u904e\u9577\uff1a<\/strong>\u5982\u679c\u5728\u4f7f\u7528\u5f8c\u672a\u6b63\u78ba\u95dc\u9589\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\uff0c\u9019\u4e9b\u9023\u63a5\u53ef\u80fd\u6703\u4fdd\u6301\u958b\u555f\u4e26\u4f54\u7528\u5bf6\u8cb4\u7684\u8cc7\u6e90\uff0c\u6700\u7d42\u5c0e\u81f4\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2 id=\"resolving-the-error-UGMOiTFfvT\">\u89e3\u6c7a\u932f\u8aa4<\/h2>\n<p>\u8981\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a53300 &#8211; too_many_connections\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\uff1a<\/p>\n<h3 id=\"1-increase-the-maximum-connections-UGMOiTFfvT\">1. \u589e\u52a0\u6700\u5927\u9023\u63a5\u6578<\/h3>\n<p>\u9ed8\u8a8d\u60c5\u6cc1\u4e0b\uff0cPostgreSQL \u5141\u8a31\u6709\u9650\u7684\u540c\u6642\u9023\u63a5\u6578\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u4fee\u6539 PostgreSQL \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 <code>max_connections<\/code> \u914d\u7f6e\u53c3\u6578\u4f86\u589e\u52a0\u6b64\u9650\u5236\uff08<code>postgresql.conf<\/code>\uff09\u3002\u6839\u64da\u4f3a\u670d\u5668\u7684\u80fd\u529b\u548c\u9810\u671f\u7684\u9023\u63a5\u6578\u8a2d\u7f6e\u66f4\u9ad8\u7684 <code>max_connections<\/code> \u503c\u3002<\/p>\n<pre><code># \u6253\u958b PostgreSQL \u914d\u7f6e\u6587\u4ef6\r\nsudo nano \/etc\/postgresql\/{version}\/main\/postgresql.conf\r\n\r\n# \u627e\u5230 max_connections \u53c3\u6578\u4e26\u4fee\u6539\r\nmax_connections = 200\r\n\r\n# \u4fdd\u5b58\u66f4\u6539\u4e26\u91cd\u555f PostgreSQL\r\nsudo service postgresql restart\r\n<\/code><\/pre>\n<h3 id=\"2-optimize-connection-pooling-UGMOiTFfvT\">2. \u512a\u5316\u9023\u63a5\u6c60<\/h3>\n<p>\u5982\u679c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u672a\u6709\u6548\u4f7f\u7528\u9023\u63a5\u6c60\uff0c\u8003\u616e\u5be6\u65bd\u9023\u63a5\u6c60\u6a5f\u5236\u3002\u9023\u63a5\u6c60\u5141\u8a31\u91cd\u7528\u6578\u64da\u5eab\u9023\u63a5\uff0c\u6e1b\u5c11\u65b0\u9023\u63a5\u7684\u6578\u91cf\uff0c\u9632\u6b62 too_many_connections \u932f\u8aa4\u3002\u6d41\u884c\u7684 PostgreSQL \u9023\u63a5\u6c60\u5eab\u5305\u62ec PgBouncer \u548c Pgpool-II\u3002<\/p>\n<h3 id=\"3-close-idle-connections-UGMOiTFfvT\">3. \u95dc\u9589\u9592\u7f6e\u9023\u63a5<\/h3>\n<p>\u78ba\u4fdd\u5728\u4f7f\u7528\u5f8c\u6b63\u78ba\u95dc\u9589\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u672a\u95dc\u9589\u7684\u9592\u7f6e\u9023\u63a5\u53ef\u80fd\u6703\u96a8\u8457\u6642\u9593\u7684\u63a8\u79fb\u800c\u7a4d\u7d2f\uff0c\u8017\u76e1\u53ef\u7528\u9023\u63a5\u3002\u6aa2\u67e5\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u4ee3\u78bc\uff0c\u78ba\u4fdd\u5728\u4e0d\u518d\u9700\u8981\u6642\u91cb\u653e\u9023\u63a5\u3002<\/p>\n<h3 id=\"4-monitor-and-optimize-server-resources-UGMOiTFfvT\">4. \u76e3\u63a7\u548c\u512a\u5316\u4f3a\u670d\u5668\u8cc7\u6e90<\/h3>\n<p>\u5b9a\u671f\u76e3\u63a7\u4f3a\u670d\u5668\u7684\u8cc7\u6e90\u4f7f\u7528\u60c5\u6cc1\uff0c\u5305\u62ec CPU\u3001\u5167\u5b58\u548c\u78c1\u789f I\/O\u3002\u5982\u679c\u60a8\u7684\u4f3a\u670d\u5668\u6301\u7e8c\u904b\u884c\u5728\u9ad8\u5229\u7528\u7387\u4e0b\uff0c\u8003\u616e\u5347\u7d1a\u786c\u4ef6\u6216\u512a\u5316\u67e5\u8a62\u548c\u6578\u64da\u5eab\u8a2d\u8a08\u4ee5\u6e1b\u5c11\u8cc7\u6e90\u6d88\u8017\u3002<\/p>\n<h2 id=\"summary-UGMOiTFfvT\">\u7e3d\u7d50<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a53300 &#8211; too_many_connections \u53ef\u80fd\u662f\u4e00\u500b\u4ee4\u4eba\u6cae\u55aa\u7684\u554f\u984c\uff0c\u5f71\u97ff\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u53ef\u7528\u6027\u3002\u901a\u904e\u589e\u52a0\u6700\u5927\u9023\u63a5\u6578\u3001\u512a\u5316\u9023\u63a5\u6c60\u3001\u95dc\u9589\u9592\u7f6e\u9023\u63a5\u4ee5\u53ca\u76e3\u63a7\u4f3a\u670d\u5668\u8cc7\u6e90\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u89e3\u6c7a\u6b64\u932f\u8aa4\uff0c\u78ba\u4fdd\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\u9806\u5229\u904b\u884c\u3002<\/p>\n<p>\u5c0d\u65bc\u53ef\u9760\u4e14\u9ad8\u6027\u80fd\u7684 VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u901a\u904e\u6211\u5011\u4e00\u6d41\u7684 VPS \u8a17\u7ba1\u670d\u52d9\uff0c\u60a8\u53ef\u4ee5\u9ad4\u9a57\u5230\u865b\u64ec\u79c1\u4eba\u4f3a\u670d\u5668\u7684\u5f37\u5927\u548c\u9748\u6d3b\u6027\uff0c\u4ee5\u6eff\u8db3\u60a8\u7684 PostgreSQL \u548c\u5176\u4ed6\u6578\u64da\u5eab\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4fee\u5fa9 PostgreSQL &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-21443","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=21443"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21443\/revisions"}],"predecessor-version":[{"id":24040,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21443\/revisions\/24040"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}