{"id":3813,"date":"2025-07-06T14:08:37","date_gmt":"2025-07-06T06:08:37","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3813"},"modified":"2025-07-06T14:08:37","modified_gmt":"2025-07-06T06:08:37","slug":"mysql-%e8%a1%a8%e6%8d%9f%e5%9d%8f%e8%87%aa%e5%8a%a8%e6%a3%80%e6%b5%8b-%e9%82%ae%e4%bb%b6%e6%8a%a5%e8%ad%a6%e8%84%9a%e6%9c%ac%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/06\/mysql-%e8%a1%a8%e6%8d%9f%e5%9d%8f%e8%87%aa%e5%8a%a8%e6%a3%80%e6%b5%8b-%e9%82%ae%e4%bb%b6%e6%8a%a5%e8%ad%a6%e8%84%9a%e6%9c%ac%e6%96%b9%e6%a1%88\/","title":{"rendered":"MySQL \u8868\u635f\u574f\u81ea\u52a8\u68c0\u6d4b + \u90ae\u4ef6\u62a5\u8b66\u811a\u672c\u65b9\u6848"},"content":{"rendered":"\n<p>\u4e0b\u9762\u662f\u4e00\u5957<strong>MySQL \u8868\u635f\u574f\u81ea\u52a8\u68c0\u6d4b + \u90ae\u4ef6\u62a5\u8b66\u811a\u672c\u65b9\u6848<\/strong>\uff0c\u9002\u7528\u4e8e Linux \u73af\u5883\u4e2d\u5b9a\u671f\u8fd0\u884c <code>CHECK TABLE<\/code> \u68c0\u67e5\u6570\u636e\u5e93\u4e2d\u662f\u5426\u6709\u635f\u574f\u8868\uff0c\u4e00\u65e6\u53d1\u73b0\u7acb\u5373\u90ae\u4ef6\u901a\u77e5\u3002\u9002\u914d <strong>MyISAM + InnoDB<\/strong> \u8868\uff0c\u652f\u6301 <strong>Cron \u5b9a\u65f6\u8c03\u5ea6<\/strong> \u548c <strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\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\u811a\u672c\u529f\u80fd\u7b80\u4ecb<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u626b\u63cf\u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u8868<\/li>\n\n\n\n<li>\u6267\u884c <code>CHECK TABLE<\/code> \u68c0\u67e5\u7ed3\u6784\u5b8c\u6574\u6027<\/li>\n\n\n\n<li>\u53d1\u73b0\u635f\u574f\u8868\u81ea\u52a8\u8bb0\u5f55\u65e5\u5fd7\u5e76\u53d1\u9001\u90ae\u4ef6<\/li>\n\n\n\n<li>\u652f\u6301\u6309\u5929\u5f52\u6863\u68c0\u6d4b\u65e5\u5fd7<\/li>\n\n\n\n<li>\u652f\u6301\u81ea\u5b9a\u4e49\u591a\u4e2a\u6570\u636e\u5e93 \/ \u6392\u9664\u7cfb\u7edf\u5e93<\/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\">\u4e8c\u3001Shell \u811a\u672c\u793a\u4f8b\uff1a<code>mysql_table_check.sh<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# === \u914d\u7f6e\u9879 ===\nMYSQL_USER=\"root\"\nMYSQL_PWD=\"your_password\"\nMYSQL_HOST=\"127.0.0.1\"\nMYSQL_PORT=3306\n\nEMAIL_TO=\"admin@example.com\"\nEMAIL_SUBJECT=\"\ud83d\udd34 MySQL\u8868\u635f\u574f\u9884\u8b66\"\nTMP_LOG=\"\/tmp\/mysql_table_check_$(date +%F).log\"\nERROR_LOG=\"\/tmp\/mysql_table_error_$(date +%F).log\"\n\n# \u6392\u9664\u6570\u636e\u5e93\nEXCLUDE_DB=\"'mysql','performance_schema','information_schema','sys'\"\n\n# === \u6267\u884c\u8868\u68c0\u6d4b ===\necho \"&#91;INFO] \u5f00\u59cb MySQL \u8868\u7ed3\u6784\u68c0\u67e5 $(date)\" &gt; \"$TMP_LOG\"\n\n# \u83b7\u53d6\u7528\u6237\u6570\u636e\u5e93\u5217\u8868\nDBS=$(mysql -u\"$MYSQL_USER\" -p\"$MYSQL_PWD\" -h\"$MYSQL_HOST\" -P\"$MYSQL_PORT\" -Nse \"SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ($EXCLUDE_DB);\")\n\nfor DB in $DBS; do\n    echo \"&#91;INFO] \u68c0\u67e5\u6570\u636e\u5e93\uff1a$DB\" &gt;&gt; \"$TMP_LOG\"\n    TABLES=$(mysql -u\"$MYSQL_USER\" -p\"$MYSQL_PWD\" -h\"$MYSQL_HOST\" -P\"$MYSQL_PORT\" -Nse \"SHOW TABLES FROM $DB;\")\n\n    for TB in $TABLES; do\n        CHECK_RESULT=$(mysql -u\"$MYSQL_USER\" -p\"$MYSQL_PWD\" -h\"$MYSQL_HOST\" -P\"$MYSQL_PORT\" -e \"CHECK TABLE \\`$DB\\`.\\`$TB\\`;\" 2&gt;&amp;1 | grep -i -E \"error|crash|corrupt\")\n\n        if &#91;&#91; -n \"$CHECK_RESULT\" ]]; then\n            echo \"&#91;ERROR] $DB.$TB \u68c0\u6d4b\u5f02\u5e38\uff1a\" &gt;&gt; \"$ERROR_LOG\"\n            echo \"$CHECK_RESULT\" &gt;&gt; \"$ERROR_LOG\"\n        fi\n    done\ndone\n\n# === \u90ae\u4ef6\u901a\u77e5 ===\nif &#91; -f \"$ERROR_LOG\" ]; then\n    cat \"$ERROR_LOG\" | mail -s \"$EMAIL_SUBJECT\" \"$EMAIL_TO\"\n    echo \"&#91;ALERT] \u8868\u635f\u574f\u95ee\u9898\u5df2\u53d1\u9001\u90ae\u4ef6\u901a\u77e5\" &gt;&gt; \"$TMP_LOG\"\nelse\n    echo \"&#91;OK] \u6240\u6709\u8868\u7ed3\u6784\u6b63\u5e38\u3002\" &gt;&gt; \"$TMP_LOG\"\nfi\n\n# \u53ef\u9009\uff1a\u6e05\u7406\u65e7\u65e5\u5fd7\uff08\u4fdd\u5b587\u5929\uff09\nfind \/tmp\/ -name \"mysql_table_*.log\" -mtime +7 -delete<\/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\u914d\u7f6e Cron \u5b9a\u65f6\u4efb\u52a1<\/h2>\n\n\n\n<p>\u7f16\u8f91\u5b9a\u65f6\u4efb\u52a1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e<\/code><\/pre>\n\n\n\n<p>\u6dfb\u52a0\u6bcf\u65e5\u51cc\u6668 3 \u70b9\u68c0\u67e5\u4efb\u52a1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 3 * * * \/bin\/bash \/path\/to\/mysql_table_check.sh &gt; \/dev\/null 2&gt;&amp;1<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2714 \u53ef\u6539\u4e3a\u6bcf\u5c0f\u65f6\/\u6bcf\u5468\u4e00\u6b21\uff0c\u6839\u636e\u4e1a\u52a1\u91cd\u8981\u6027\u8bbe\u7f6e\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\">\u56db\u3001\u542f\u7528\u90ae\u4ef6\u53d1\u9001<\/h2>\n\n\n\n<p>\u786e\u4fdd\u670d\u52a1\u5668\u5b89\u88c5\u4e86\u90ae\u4ef6\u5de5\u5177\uff08\u4efb\u9009\u4e00\u79cd\uff09\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <code>mailx<\/code>\uff08\u63a8\u8350\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install mailutils   # Ubuntu\nsudo yum install mailx       # CentOS<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e <code>\/etc\/mail.rc<\/code> \u6216\u4f7f\u7528 Gmail SMTP\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set smtp=smtp:\/\/smtp.gmail.com:587\nset smtp-auth-user=your@gmail.com\nset smtp-auth-password=your_password\nset smtp-auth=login<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u8f93\u51fa\u793a\u4f8b\uff08\u6536\u5230\u7684\u90ae\u4ef6\u5185\u5bb9\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;ERROR] production.orders \u68c0\u6d4b\u5f02\u5e38\uff1a\n| production.orders | check | error | Table is marked as crashed and should be repaired |\n\n&#91;ERROR] logs.audit_log \u68c0\u6d4b\u5f02\u5e38\uff1a\n| logs.audit_log | check | warning | Table upgrade required. Please do \"REPAIR TABLE `audit_log`\" |<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5ef6\u4f38\u63a8\u8350<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5de5\u5177<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.zabbix.com\">Zabbix MySQL Table Health Check<\/a><\/td><td>\u53ef\u901a\u8fc7\u81ea\u5b9a\u4e49 Item + Trigger \u5b9e\u73b0\u544a\u8b66<\/td><\/tr><tr><td>Percona Toolkit <code>pt-table-checksum<\/code><\/td><td>\u7528\u4e8e\u4e3b\u4ece\u4e00\u81f4\u6027\u6821\u9a8c<\/td><\/tr><tr><td>Prometheus + mysqld_exporter<\/td><td>\u53ef\u52a0\u8868\u72b6\u6001\u76d1\u63a7\u6307\u6807<\/td><\/tr><tr><td>\u81ea\u5efa\u72b6\u6001\u9875<\/td><td>\u4f7f\u7528 Grafana \u6216 Uptime Kuma \u5c55\u793a MySQL \u5065\u5eb7\u72b6\u51b5<\/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\">\u4e03\u3001\u5b89\u5168\u52a0\u56fa\u5efa\u8bae<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u5728\u811a\u672c\u4e2d\u660e\u6587\u5199\u5bc6\u7801 \u27a4 \u4f7f\u7528 <code>~\/.my.cnf<\/code> + <code>chmod 600<\/code><\/li>\n\n\n\n<li>\u65e5\u5fd7\u6587\u4ef6\u6bcf\u65e5\u6e05\u7406\uff0c\u9632\u6b62\u7a7a\u95f4\u6491\u7206<\/li>\n\n\n\n<li>\u68c0\u67e5\u7ed3\u679c\u5b58\u5165\u65e5\u5fd7\u7cfb\u7edf\uff08\u5982 ELK\u3001Graylog\uff09<\/li>\n\n\n\n<li>\u90ae\u4ef6\u5185\u5bb9\u9644\u5e26 SQL \u4fee\u590d\u5efa\u8bae\uff08\u53ef\u5b9a\u5236\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u4e00\u5957MySQL \u8868\u635f\u574f\u81ea\u52a8\u68c0\u6d4b + \u90ae\u4ef6\u62a5\u8b66\u811a\u672c\u65b9\u6848\uff0c\u9002\u7528\u4e8e Linux \u73af\u5883\u4e2d\u5b9a\u671f\u8fd0\u884c CHECK T [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3814,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-3813","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\/3813","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=3813"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3813\/revisions"}],"predecessor-version":[{"id":3815,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3813\/revisions\/3815"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3814"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}