{"id":441778,"date":"2025-06-04T09:43:04","date_gmt":"2025-06-04T01:43:04","guid":{"rendered":"https:\/\/www.idc.net\/help\/?p=441778"},"modified":"2025-09-16T10:54:59","modified_gmt":"2025-09-16T02:54:59","slug":"mysql-%e5%b8%b8%e8%a7%81%e6%95%85%e9%9a%9c%e6%8e%92%e6%9f%a5%e4%b8%8e%e7%94%9f%e4%ba%a7%e7%8e%af%e5%a2%83%e4%bc%98%e5%8c%96%e5%85%a8%e6%94%bb%e7%95%a5","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/441778\/","title":{"rendered":"\u5728\u540e\u6d6a\u4e91VPS\u4e0a\u4f18\u5316MySQL\u6027\u80fd\u4e0e\u6545\u969c\u6392\u67e5"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>MySQL\u662f\u4f01\u4e1a\u7ea7\u5e94\u7528\u7684\u9996\u9009\u6570\u636e\u5e93\u4e4b\u4e00\uff0c\u4f46\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u6027\u80fd\u74f6\u9888\u548c\u6545\u969c\u53ef\u80fd\u5f71\u54cd\u670d\u52a1\u7a33\u5b9a\u6027\u3002\u638c\u63e1\u6545\u969c\u6392\u67e5\u548c\u4f18\u5316\u6280\u5de7\u5bf9\u4e8e\u7ef4\u6301\u9ad8\u6548\u6570\u636e\u5e93\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u4ee5<a href=\"https:\/\/idc.net\/cloud-hk\">\u540e\u6d6a\u4e91VPS<\/a>\u4e3a\u4f8b\uff0c\u63a2\u8ba8MySQL\u5e38\u89c1\u6545\u969c\u7684\u8bca\u65ad\u4e0e\u4f18\u5316\u7b56\u7565\uff0c\u7ed3\u5408\u6280\u672f\u539f\u7406\u548c\u5b9e\u8df5\u6848\u4f8b\uff0c\u4e3a\u5f00\u53d1\u8005\u548c\u7cfb\u7edf\u7ba1\u7406\u5458\u63d0\u4f9b\u5b9e\u7528\u6307\u5357\u3002<\/p>\n<h2>\u6848\u4f8b\u5206\u6790\uff1a\u540e\u6d6a\u4e91\u4e0a\u7684\u793e\u4ea4\u5e73\u53f0\u6570\u636e\u5e93<\/h2>\n<p>\u5047\u8bbe\u6211\u4eec\u5728<a href=\"https:\/\/idc.net\/cloud-hk\">\u540e\u6d6a\u4e91HK-1H2G VPS<\/a>\uff081\u6838CPU\u30012GB DDR4\u5185\u5b58\u300130GB SSD\u30011Mbps\u5e26\u5bbd\uff0c\u6708\u4ed8\u00a530\uff09\u4e0a\u90e8\u7f72\u4e00\u4e2a\u793e\u4ea4\u5e73\u53f0\u7684MySQL\u6570\u636e\u5e93\u3002\u8fd1\u671f\u7528\u6237\u53cd\u9988\u67e5\u8be2\u5ef6\u8fdf\uff0c\u68c0\u67e5\u53d1\u73b0\u6162\u67e5\u8be2\u548c\u8fde\u63a5\u6570\u8fc7\u9ad8\u3002\u901a\u8fc7\u4f18\u5316\u914d\u7f6e\u548c\u7d22\u5f15\uff0c\u6211\u4eec\u5c06\u67e5\u8be2\u54cd\u5e94\u65f6\u95f4\u4ece1.5\u79d2\u964d\u81f3300\u6beb\u79d2\u3002<\/p>\n<h2>\u6280\u672f\u539f\u7406\uff1aMySQL\u6027\u80fd\u4e0e\u6545\u969c\u673a\u5236<\/h2>\n<h3>\u6545\u969c\u7c7b\u578b<\/h3>\n<ul>\n<li><strong>\u670d\u52a1\u6545\u969c<\/strong>\uff1aMySQL\u65e0\u6cd5\u542f\u52a8\u6216\u5d29\u6e83\uff0c\u901a\u5e38\u7531\u914d\u7f6e\u9519\u8bef\u6216\u8d44\u6e90\u4e0d\u8db3\u5f15\u8d77\u3002<\/li>\n<li><strong>\u8fde\u63a5\u95ee\u9898<\/strong>\uff1a\u5ba2\u6237\u7aef\u8fde\u63a5\u5931\u8d25\uff0c\u5e38\u89c1\u4e8e\u6743\u9650\u914d\u7f6e\u6216\u8fde\u63a5\u6570\u8d85\u9650\u3002<\/li>\n<li><strong>\u67e5\u8be2\u6027\u80fd<\/strong>\uff1a\u6162\u67e5\u8be2\u6216\u8868\u635f\u574f\u5bfc\u81f4\u54cd\u5e94\u5ef6\u8fdf\u3002<\/li>\n<li><strong>\u590d\u5236\u95ee\u9898<\/strong>\uff1a\u4e3b\u4ece\u540c\u6b65\u4e2d\u65ad\uff0c\u5f71\u54cd\u9ad8\u53ef\u7528\u6027\u3002<\/li>\n<\/ul>\n<h3>\u4f18\u5316\u5173\u952e<\/h3>\n<ul>\n<li><strong>\u7f13\u51b2\u6c60<\/strong>\uff1a<code>innodb_buffer_pool_size<\/code>\u51b3\u5b9a\u70ed\u70b9\u6570\u636e\u7f13\u5b58\u6548\u7387\u3002<\/li>\n<li><strong>\u7d22\u5f15<\/strong>\uff1a\u4f18\u5316\u67e5\u8be2\u6267\u884c\u8ba1\u5212\uff0c\u51cf\u5c11\u5168\u8868\u626b\u63cf\u3002<\/li>\n<li><strong>\u76d1\u63a7<\/strong>\uff1a\u6162\u67e5\u8be2\u65e5\u5fd7\u548c\u6027\u80fd\u6307\u6807\u5e2e\u52a9\u5b9a\u4f4d\u74f6\u9888\u3002<\/li>\n<\/ul>\n<h2>\u5b9e\u8df5\u6307\u5357\uff1a\u5728\u540e\u6d6a\u4e91VPS\u4e0a\u4f18\u5316MySQL<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5728Ubuntu 20.04\u4e0a\u6392\u67e5\u548c\u4f18\u5316MySQL\u7684\u6b65\u9aa4\uff0c\u9002\u7528\u4e8e\u540e\u6d6a\u4e91VPS\u3002<\/p>\n<h3>\u6b65\u9aa41\uff1a\u68c0\u67e5\u670d\u52a1\u72b6\u6001<\/h3>\n<p>\u9a8c\u8bc1MySQL\u8fd0\u884c\u72b6\u6001\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u68c0\u67e5\u670d\u52a1\u72b6\u6001\r\nsudo systemctl status mysql\r\n<\/code><\/pre>\n<p>\u67e5\u770b\u9519\u8bef\u65e5\u5fd7\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u67e5\u770b\u65e5\u5fd7\u8def\u5f84\r\nmysql -e \"SHOW VARIABLES LIKE 'log_error';\"\r\n\r\n# \u68c0\u67e5\u65e5\u5fd7\r\nsudo tail -f \/var\/log\/mysql\/error.log\r\n<\/code><\/pre>\n<h3>\u6b65\u9aa42\uff1a\u8bca\u65ad\u8fde\u63a5\u95ee\u9898<\/h3>\n<p>\u68c0\u67e5\u6d3b\u8dc3\u8fde\u63a5\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u67e5\u770b\u7ebf\u7a0b\u72b6\u6001\r\nSHOW PROCESSLIST\\G;\r\n<\/code><\/pre>\n<p>\u82e5<code>Threads_connected<\/code>\u63a5\u8fd1<code>max_connections<\/code>\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u67e5\u770b\u5f53\u524d\u8fde\u63a5\u6570\r\nSHOW STATUS LIKE 'Threads_connected';\r\n\r\n-- \u67e5\u770b\u6700\u5927\u8fde\u63a5\u6570\r\nSHOW VARIABLES LIKE 'max_connections';\r\n\r\n-- \u4e34\u65f6\u8c03\u6574\r\nSET GLOBAL max_connections = 500;\r\nSET GLOBAL wait_timeout = 60;\r\n<\/code><\/pre>\n<p>\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff08<code>\/etc\/mysql\/my.cnf<\/code>\uff09\uff1a<\/p>\n<pre><code class=\"language-ini\">[mysqld]\r\nmax_connections = 500\r\nwait_timeout = 60\r\n<\/code><\/pre>\n<h3>\u6b65\u9aa43\uff1a\u4fee\u590d\u8868\u635f\u574f<\/h3>\n<p>\u5bf9\u4e8eMyISAM\u8868\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u4fee\u590d\u635f\u574f\u8868\r\nmyisamchk --force \/var\/lib\/mysql\/db_name\/table_name.MYI\r\n<\/code><\/pre>\n<p>\u5bf9\u4e8eInnoDB\u8868\uff1a<\/p>\n<pre><code class=\"language-ini\"># \u7f16\u8f91my.cnf\u542f\u7528\u6062\u590d\u6a21\u5f0f\r\n[mysqld]\r\ninnodb_force_recovery = 1\r\n<\/code><\/pre>\n<p>\u91cd\u542f\u5e76\u5bfc\u51fa\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo systemctl restart mysql\r\nmysqldump -u root -p db_name &gt; backup.sql\r\n<\/code><\/pre>\n<h3>\u6b65\u9aa44\uff1a\u4f18\u5316\u914d\u7f6e<\/h3>\n<p>\u7f16\u8f91<code>\/etc\/mysql\/my.cnf<\/code>\uff1a<\/p>\n<pre><code class=\"language-ini\">[mysqld]\r\ninnodb_buffer_pool_size = 1G  # \u5360\u5185\u5b5860%-70%\r\ninnodb_log_file_size = 512M\r\nquery_cache_type = 0\r\nquery_cache_size = 0\r\nslow_query_log = ON\r\nlong_query_time = 1\r\nslow_query_log_file = \/var\/log\/mysql\/slow.log\r\n<\/code><\/pre>\n<p>\u91cd\u542fMySQL\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo systemctl restart mysql\r\n<\/code><\/pre>\n<h3>\u6b65\u9aa45\uff1a\u7d22\u5f15\u4e0e\u67e5\u8be2\u4f18\u5316<\/h3>\n<p>\u542f\u7528\u6162\u67e5\u8be2\u65e5\u5fd7\uff1a<\/p>\n<pre><code class=\"language-sql\">SET GLOBAL slow_query_log = ON;\r\nSET GLOBAL long_query_time = 1;\r\n<\/code><\/pre>\n<p>\u5206\u6790\u6162\u67e5\u8be2\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u4f7f\u7528mysqldumpslow\r\nmysqldumpslow \/var\/log\/mysql\/slow.log\r\n<\/code><\/pre>\n<p>\u4f18\u5316\u67e5\u8be2\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u68c0\u67e5\u6267\u884c\u8ba1\u5212\r\nEXPLAIN SELECT * FROM posts WHERE user_id = 100;\r\n\r\n-- \u6dfb\u52a0\u8986\u76d6\u7d22\u5f15\r\nALTER TABLE posts ADD INDEX idx_user_time (user_id, created_at);\r\n<\/code><\/pre>\n<h3>\u6b65\u9aa46\uff1a\u90e8\u7f72\u76d1\u63a7\u5de5\u5177<\/h3>\n<p>\u5b89\u88c5Prometheus\u548cMySQL Exporter\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u5b89\u88c5Prometheus\r\nwget https:\/\/github.com\/prometheus\/prometheus\/releases\/download\/v2.45.0\/prometheus-2.45.0.linux-amd64.tar.gz\r\ntar -xzf prometheus-2.45.0.linux-amd64.tar.gz\r\nsudo mv prometheus-2.45.0.linux-amd64 \/opt\/prometheus\r\n\r\n# \u5b89\u88c5MySQL Exporter\r\nwget https:\/\/github.com\/prometheus\/mysqld_exporter\/releases\/download\/v0.14.0\/mysqld_exporter-0.14.0.linux-amd64.tar.gz\r\ntar -xzf mysqld_exporter-0.14.0.linux-amd64.tar.gz\r\nsudo mv mysqld_exporter-0.14.0.linux-amd64 \/opt\/mysqld_exporter\r\n<\/code><\/pre>\n<p>\u914d\u7f6ePrometheus\uff08<code>\/opt\/prometheus\/prometheus.yml<\/code>\uff09\uff1a<\/p>\n<pre><code class=\"language-yaml\">scrape_configs:\r\n  - job_name: 'mysql'\r\n    static_configs:\r\n      - targets: ['localhost:9104']\r\n<\/code><\/pre>\n<p>\u542f\u52a8\u670d\u52a1\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u8fd0\u884cPrometheus\r\nsudo \/opt\/prometheus\/prometheus --config.file=\/opt\/prometheus\/prometheus.yml &amp;\r\n\r\n# \u8fd0\u884cMySQL Exporter\r\nsudo \/opt\/mysqld_exporter\/mysqld_exporter &amp;\r\n<\/code><\/pre>\n<p>\u8bbf\u95ee<code>http:\/\/&lt;vps-ip&gt;:9090<\/code>\u67e5\u770b\u6307\u6807\u3002<\/p>\n<h2>\u6280\u672f\u5bf9\u6bd4\uff1aMySQL vs. PostgreSQL<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u7279\u6027<\/th>\n<th>MySQL<\/th>\n<th>PostgreSQL<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u5e76\u53d1\u6027\u80fd<\/strong><\/td>\n<td>\u9ad8\uff0c\u9002\u5408\u8bfb\u5199\u5206\u79bb<\/td>\n<td>\u5f3a\u5927\uff0c\u652f\u6301\u590d\u6742\u4e8b\u52a1<\/td>\n<\/tr>\n<tr>\n<td><strong>\u5b58\u50a8\u5f15\u64ce<\/strong><\/td>\n<td>InnoDB\/MyISAM<\/td>\n<td>\u5355\u5f15\u64ce\uff0c\u529f\u80fd\u4e30\u5bcc<\/td>\n<\/tr>\n<tr>\n<td><strong>\u9002\u7528\u573a\u666f<\/strong><\/td>\n<td>Web\u5e94\u7528\u3001\u7535\u5546<\/td>\n<td>\u590d\u6742\u5206\u6790\u3001GIS<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>MySQL<\/strong>\uff1a\u9002\u5408\u540e\u6d6a\u4e91VPS\uff08\u5982<a href=\"https:\/\/idc.net\/cloud-hk\">HK-1H2G<\/a>\uff09\u7684\u8f7b\u91cfWeb\u5e94\u7528\u3002<\/li>\n<li><strong>PostgreSQL<\/strong>\uff1a\u9002\u5408\u6570\u636e\u5206\u6790\u573a\u666f\u3002<\/li>\n<\/ul>\n<p>\u5728\u8be5\u6848\u4f8b\u7814\u7a76\u4e2d\uff0cMySQL \u7684\u7b80\u6d01\u6027\u6ee1\u8db3\u4e86\u793e\u4ea4\u5e73\u53f0\u7684\u9700\u6c42\u3002<\/p>\n<h2>\u6280\u672f\u7ecf\u9a8c\u5206\u4eab<\/h2>\n<p>\u5728\u540e\u6d6a\u4e91VPS \u4e0a\u5bf9 MySQL \u8fdb\u884c\u4f18\u5316\u63d0\u5347\u4e86\u6211\u4eec\u5e73\u53f0\u7684\u6027\u80fd:<\/p>\n<ul>\n<li>\u56fa\u6001\u786c\u76d8\u6027\u80fd\uff1aHK-1H2G \u5957\u4ef6\u4e2d\u7684\u56fa\u6001\u786c\u76d8\u964d\u4f4e\u4e86 I\/O \u5ef6\u8fdf.<\/li>\n<li>\u4f4e\u5ef6\u8fdf\u7f51\u7edc\uff1a\u9999\u6e2f\u6570\u636e\u4e2d\u5fc3\u786e\u4fdd\u4e86\u5feb\u901f\u7684\u67e5\u8be2\u54cd\u5e94\u65f6\u95f4.<\/li>\n<li>\u76d1\u6d4b\uff1aPrometheus\u7cfb\u7edf\u80fd\u591f\u63d0\u4f9b\u5b9e\u65f6\u7684\u74f6\u9888\u5206\u6790\u4fe1\u606f.<\/li>\n<li>\u53ef\u6269\u5c55\u6027\uff1a\u80fd\u591f\u8f7b\u677e\u5347\u7ea7\u81f3\u66f4\u9ad8\u5c42\u7ea7\u7684\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668\uff0c\u4ee5\u5e94\u5bf9\u589e\u52a0\u7684\u8d1f\u8f7d\u3002.<\/li>\n<\/ul>\n<p>\u66f4\u591a\u8d44\u6e90\uff1aMySQL \u6587\u6863 \u6216\u00a0<a href=\"https:\/\/idc.net\/\">\u540e\u6d6a\u4e91 Support<\/a>.<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u719f\u7ec3\u638c\u63e1 MySQL \u7684\u6545\u969c\u6392\u67e5\u548c\u4f18\u5316\u6280\u672f\uff0c\u6211\u4eec\u5728\u201c\u540e\u6d6a\u4e91\u201dVPS \u4e0a\u5b9e\u73b0\u4e86\u9ad8\u6027\u80fd\u3002\u501f\u52a9\u201c\u540e\u6d6a\u4e91\u201d\u7684\u56fa\u6001\u786c\u76d8\u5b58\u50a8\u548c\u4f4e\u5ef6\u8fdf\u7f51\u7edc\uff0c\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u4e3a\u9ad8\u8981\u6c42\u7684\u5e94\u7528\u7a0b\u5e8f\u6784\u5efa\u5f3a\u5927\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 MySQL\u662f\u4f01\u4e1a\u7ea7\u5e94\u7528\u7684\u9996\u9009\u6570\u636e\u5e93\u4e4b\u4e00\uff0c\u4f46\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u6027\u80fd\u74f6\u9888\u548c\u6545\u969c\u53ef\u80fd\u5f71\u54cd\u670d\u52a1\u7a33\u5b9a\u6027\u3002\u638c\u63e1\u6545\u969c\u6392\u67e5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7038],"tags":[],"class_list":["post-441778","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=441778"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441778\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=441778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=441778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=441778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}