{"id":3834,"date":"2025-07-06T14:48:08","date_gmt":"2025-07-06T06:48:08","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3834"},"modified":"2025-07-06T14:48:08","modified_gmt":"2025-07-06T06:48:08","slug":"%e5%9c%a8-mysql-%e4%b8%ad%e9%85%8d%e7%bd%ae%e8%bf%9e%e6%8e%a5%e9%99%90%e5%88%b6%e5%92%8c%e8%b6%85%e6%97%b6%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/06\/%e5%9c%a8-mysql-%e4%b8%ad%e9%85%8d%e7%bd%ae%e8%bf%9e%e6%8e%a5%e9%99%90%e5%88%b6%e5%92%8c%e8%b6%85%e6%97%b6%e8%ae%be%e7%bd%ae\/","title":{"rendered":"\u5728 MySQL \u4e2d\u914d\u7f6e\u8fde\u63a5\u9650\u5236\u548c\u8d85\u65f6\u8bbe\u7f6e"},"content":{"rendered":"\n<p>\u5728 MySQL \u4e2d\u914d\u7f6e\u8fde\u63a5\u9650\u5236\u548c\u8d85\u65f6\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u8d44\u6e90\u6ee5\u7528\u3001\u907f\u514d\u8fde\u63a5\u8fc7\u591a\u5bfc\u81f4\u670d\u52a1\u5d29\u6e83\u3002\u4e0b\u9762\u4ece\u53c2\u6570\u8bf4\u660e\u3001\u914d\u7f6e\u65b9\u6cd5\u3001\u5b9e\u65f6\u67e5\u8be2\u4e0e\u8c03\u4f18\u5efa\u8bae\u56db\u4e2a\u65b9\u9762\uff0c\u4e3a\u4f60\u5168\u9762\u8be6\u89e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5173\u952e\u53c2\u6570\u6982\u89c8<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u53c2\u6570\u540d<\/th><th>\u9ed8\u8ba4\u503c\uff08MySQL 8.0\uff09<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>max_connections<\/code><\/td><td>151<\/td><td>\u6700\u5927\u5141\u8bb8\u540c\u65f6\u8fde\u63a5\u6570\uff08\u5305\u542b\u7cfb\u7edf\u7ebf\u7a0b\uff09<\/td><\/tr><tr><td><code>max_user_connections<\/code><\/td><td>0<\/td><td>\u6bcf\u4e2a\u7528\u6237\u5141\u8bb8\u7684\u6700\u5927\u8fde\u63a5\u6570\uff080=\u65e0\u9650\uff09<\/td><\/tr><tr><td><code>wait_timeout<\/code><\/td><td>28800\uff088\u5c0f\u65f6\uff09<\/td><td>\u975e\u4ea4\u4e92\u8fde\u63a5\u7a7a\u95f2\u591a\u4e45\u540e\u65ad\u5f00<\/td><\/tr><tr><td><code>interactive_timeout<\/code><\/td><td>28800<\/td><td>\u4ea4\u4e92\u5f0f\u8fde\u63a5\uff08\u5982MySQL CLI\uff09\u7a7a\u95f2\u591a\u4e45\u540e\u65ad\u5f00<\/td><\/tr><tr><td><code>connect_timeout<\/code><\/td><td>10<\/td><td>\u5ba2\u6237\u7aef\u8fde\u63a5\u8d85\u65f6\u79d2\u6570<\/td><\/tr><tr><td><code>net_read_timeout<\/code><\/td><td>30<\/td><td>\u5ba2\u6237\u7aef\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4<\/td><\/tr><tr><td><code>net_write_timeout<\/code><\/td><td>60<\/td><td>\u5ba2\u6237\u7aef\u5199\u5165\u8d85\u65f6\u65f6\u95f4<\/td><\/tr><tr><td><code>thread_cache_size<\/code><\/td><td>9<\/td><td>\u7a7a\u95f2\u7ebf\u7a0b\u7f13\u5b58\u6570\uff08\u907f\u514d\u9891\u7e41\u521b\u5efa\u7ebf\u7a0b\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udccc \u6ce8\uff1a\u5b9e\u9645\u8fde\u63a5\u8d85\u65f6\u53ef\u80fd\u53d7\u5ba2\u6237\u7aef\u5e93\uff08\u5982 JDBC\/PHP PDO\uff09\u8bbe\u7f6e\u5f71\u54cd\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4fee\u6539\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4e34\u65f6\u4fee\u6539\uff08\u7acb\u5373\u751f\u6548\u3001\u91cd\u542f\u5931\u6548\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6700\u5927\u8fde\u63a5\u6570\nSET GLOBAL max_connections = 300;\n\n-- \u6bcf\u7528\u6237\u6700\u5927\u8fde\u63a5\u6570\nSET GLOBAL max_user_connections = 50;\n\n-- \u975e\u4ea4\u4e92\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\nSET GLOBAL wait_timeout = 600;\n\n-- \u4ea4\u4e92\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\nSET GLOBAL interactive_timeout = 600;\n\n-- \u5ba2\u6237\u7aef\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\nSET GLOBAL connect_timeout = 10;<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u67e5\u770b\u5f53\u524d\u503c\uff1a<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW VARIABLES LIKE 'max_connections';\nSHOW VARIABLES LIKE 'wait_timeout';<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6c38\u4e45\u4fee\u6539\uff08\u63a8\u8350\uff0c\u9700\u91cd\u542f MySQL\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u6cd5\u4e00\uff1a\u4fee\u6539 <code>my.cnf<\/code> \u914d\u7f6e\u6587\u4ef6<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]\nmax_connections = 300\nmax_user_connections = 50\nwait_timeout = 600\ninteractive_timeout = 600\nconnect_timeout = 10<\/code><\/pre>\n\n\n\n<p>\u91cd\u542f MySQL\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart mysql<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u6cd5\u4e8c\uff1aDocker \u4e2d\u8bbe\u7f6e<\/h4>\n\n\n\n<p>\u5982\u679c\u4f60\u7528 Docker \u8fd0\u884c MySQL\uff0c\u53ef\u901a\u8fc7 command \u8986\u76d6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>services:\n  mysql:\n    image: mysql:8.0\n    command: &#91;\n      \"--max_connections=300\",\n      \"--wait_timeout=600\"\n    ]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5b9e\u65f6\u76d1\u63a7\u8fde\u63a5\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5f53\u524d\u6d3b\u8dc3\u8fde\u63a5\u6570\uff08\u542b sleep \u8fde\u63a5\uff09\nSHOW STATUS LIKE 'Threads_connected';\n\n-- \u5df2\u521b\u5efa\u7684\u8fde\u63a5\u603b\u6570\nSHOW STATUS LIKE 'Connections';\n\n-- \u68c0\u67e5\u8fde\u63a5\u6765\u6e90\uff08IP\uff09\nSELECT host, COUNT(*) FROM information_schema.processlist GROUP BY host;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u8c03\u8bd5\u4e0e\u6545\u969c\u6392\u67e5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u95ee\u9898<\/th><th>\u8bca\u65ad\u65b9\u6cd5<\/th><th>\u5efa\u8bae<\/th><\/tr><\/thead><tbody><tr><td><code>Too many connections<\/code><\/td><td>\u67e5\u770b <code>SHOW STATUS LIKE 'Max_used_connections';<\/code><\/td><td>\u589e\u5927 <code>max_connections<\/code> \u6216\u4f18\u5316\u8fde\u63a5\u6c60<\/td><\/tr><tr><td>\u8fde\u63a5\u957f\u65f6\u95f4\u6302\u8d77<\/td><td><code>SHOW PROCESSLIST<\/code><\/td><td>\u8bc6\u522b\u6162 SQL\uff0c\u907f\u514d\u957f\u4e8b\u52a1<\/td><\/tr><tr><td>\u8fde\u63a5\u88ab\u81ea\u52a8\u65ad\u5f00<\/td><td>\u68c0\u67e5 <code>wait_timeout<\/code> \u662f\u5426\u8fc7\u4f4e<\/td><td>\u8bbe\u7f6e\u5408\u7406 timeout \u5e76\u8c03\u4f18\u5ba2\u6237\u7aef<\/td><\/tr><tr><td>\u5e94\u7528\u9891\u7e41\u65ad\u8fde<\/td><td>\u68c0\u67e5 <code>net_write_timeout<\/code>, <code>net_read_timeout<\/code><\/td><td>\u8c03\u9ad8\u4e3a <code>60~120<\/code> \u79d2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u5efa\u8bae\u8bbe\u7f6e<\/th><\/tr><\/thead><tbody><tr><td>Web \u5e94\u7528\u8fde\u63a5\u6c60\uff08\u5982 PHP\u3001Node.js\uff09<\/td><td>\u4f7f\u7528 <code>wait_timeout=60~120<\/code>\uff0c\u907f\u514d\u957f\u8fde\u63a5 idle<\/td><\/tr><tr><td>\u6570\u636e\u5bfc\u5165\u6216\u5927\u4e8b\u52a1\u6267\u884c<\/td><td>\u8c03\u9ad8 <code>net_write_timeout=180<\/code><\/td><\/tr><tr><td>\u591a\u7528\u6237\u73af\u5883<\/td><td>\u8bbe\u7f6e <code>max_user_connections=50<\/code> \u9650\u5236\u6ee5\u7528<\/td><\/tr><tr><td>\u9ad8\u5e76\u53d1\u8bfb\u53d6\uff08\u53ea\u8bfb\u4ece\u5e93\uff09<\/td><td>\u53ef\u9002\u5f53\u63d0\u9ad8 <code>max_connections=500~1000<\/code><\/td><\/tr><tr><td>\u5e94\u7528\u4e3b\u52a8\u4fdd\u6301\u8fde\u63a5<\/td><td>\u4f7f\u7528 <code>select 1<\/code> \u5b9a\u65f6\u5fc3\u8df3\u4fdd\u6301\u8fde\u63a5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcda \u5b98\u65b9\u53c2\u8003\u6587\u6863<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd17 <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/server-system-variables.html\">MySQL \u5b98\u65b9\u624b\u518c\uff1a\u8fde\u63a5\u63a7\u5236\u53c2\u6570<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/server-status-variables.html\">MySQL Performance Schema &amp; Status Monitoring<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/www.percona.com\/blog\/2016\/05\/16\/mysql-connection-handling-and-scalability\/\">MySQL \u8fde\u63a5\u7ba1\u7406\u5b9e\u6218\uff08Percona\uff09<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u793a\u4f8b\u603b\u7ed3<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u63a8\u8350\u8bbe\u7f6e\uff08\u9002\u5408\u666e\u901a Web \u5e94\u7528\uff09\nSET GLOBAL max_connections = 300;\nSET GLOBAL max_user_connections = 50;\nSET GLOBAL wait_timeout = 90;\nSET GLOBAL interactive_timeout = 90;\nSET GLOBAL connect_timeout = 10;\nSET GLOBAL net_read_timeout = 30;\nSET GLOBAL net_write_timeout = 60;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MySQL \u4e2d\u914d\u7f6e\u8fde\u63a5\u9650\u5236\u548c\u8d85\u65f6\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u8d44\u6e90\u6ee5\u7528\u3001\u907f\u514d\u8fde\u63a5\u8fc7\u591a\u5bfc\u81f4\u670d\u52a1\u5d29\u6e83\u3002\u4e0b\u9762\u4ece\u53c2\u6570\u8bf4\u660e\u3001\u914d\u7f6e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3836,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-3834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3834"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3834\/revisions"}],"predecessor-version":[{"id":3837,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3834\/revisions\/3837"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3836"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}