 
		 
		{"id":175,"date":"2017-12-02T22:38:58","date_gmt":"2017-12-02T14:38:58","guid":{"rendered":"http:\/\/www.softeng.cn\/?p=175"},"modified":"2017-12-03T17:36:41","modified_gmt":"2017-12-03T09:36:41","slug":"%e5%9c%a8centos%e7%b3%bb%e7%bb%9f%e4%b8%8b%e5%ae%9e%e7%8e%b0mysql%e8%87%aa%e5%8a%a8%e5%85%a8%e9%87%8f%e5%a4%87%e4%bb%bd","status":"publish","type":"post","link":"https:\/\/www.softeng.cn\/?p=175","title":{"rendered":"\u5728CentOS\u7cfb\u7edf\u4e0b\u5b9e\u73b0MySQL\u81ea\u52a8\u5168\u91cf\u5907\u4efd"},"content":{"rendered":"<h1>\u524d\u8a00<\/h1>\n<p>\u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b9e\u73b0MySQL\u7684\u81ea\u52a8\u5907\u4efd\uff0c\u4e3b\u8981\u9700\u8981\u4e24\u4e2a\u8d44\u6599\uff0c\u4e00\u4e2a\u662fMySQL\u6570\u636e\u5e93\u5907\u4efd\u6240\u9700\u8981\u7684\u811a\u672c\uff0c\u4e00\u4e2a\u662fcrontab\u8fd9\u6837\u7684\u5b9a\u65f6\u4efb\u52a1\u6267\u884c\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u811a\u672c\u53ef\u4ee5\u662f\u4efb\u4f55\u8bed\u8a00\u7684\uff0c\u53ea\u8981\u6700\u7ec8\u80fd\u591f\u5c06MySQL\u6570\u636e\u5e93\uff0c\u5bfc\u51fa\u6210SQL\u6587\u4ef6\u6216\u8005\u5176\u4ed6\u4f60\u9700\u8981\u7684\u6587\u4ef6\u5373\u53ef\uff0c\u800c\u5b9a\u65f6\u4efb\u52a1\u6267\u884c\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u662fLinux\u4e0b\u5e38\u7528\u7684crontab\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u5b9a\u65f6\u4efb\u52a1\u7a0b\u5e8f\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u672c\u6587\u662f\u4ee5shell\u811a\u672c\u53cacrontab\u5b9a\u65f6\u4efb\u52a1\u4e3a\u57fa\u7840\uff0c\u5b9e\u73b0\u7684MySQL\u6570\u636e\u5e93\u5b9a\u65f6\u81ea\u52a8\u5168\u91cf\u5907\u4efd\u3002\u5176\u529f\u80fd\u5177\u4f53\u63cf\u8ff0\u4e3a\uff1a<br \/>\n+ \u5b9e\u73b0\u5f53\u524dLinux\u670d\u52a1\u5668\u4e0aMySQL\u670d\u52a1\u5668\u91cc\u7684\u6240\u6709\u5e93\u7684\u5168\u91cf\u6570\u636e+\u7ed3\u6784\u5907\u4efd\uff1b<br \/>\n+ \u5b9e\u73b0\u5907\u4efd\u811a\u672c\u53ea\u4fdd\u7559\u8fd115\u5929\uff0c15\u5929\u4ee5\u524d\u7684\u5907\u4efd\u811a\u672c\u81ea\u52a8\u5220\u9664\uff1b<br \/>\n+ \u5b9e\u73b0\u6bcf\u5929\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u5f00\u59cb\u8fdb\u884c\u5907\u4efd\u5de5\u4f5c\u3002<\/p>\n<h1>\u51c6\u5907\u5de5\u4f5c<\/h1>\n<p>\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u5b89\u88c5crontab\uff08\u5df2\u5b89\u88c5\u7684\u53ef\u4ee5\u8df3\u8fc7\uff09<\/p>\n<blockquote><p>yum install -y vixie-cron crontabs<\/p><\/blockquote>\n<p>\u521b\u5efamysql\u5907\u4efd\u6587\u4ef6\u7684\u5b58\u653e\u8def\u5f84<\/p>\n<blockquote><p>mkdir -p \/alidata\/backup\/databases<br \/>\nmkdir -p \/alidata\/backup\/databases\/mysql<\/p><\/blockquote>\n<p>\u8fd9\u91cc\u7684\u8def\u5f84\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\uff0c\u4f46\u662f\u5982\u679c\u4fee\u6539\u4e86\u8fd9\u91cc\u7684\u8def\u5f84\uff0c\u4e5f\u9700\u8981\u4fee\u6539\u4e0b\u9762\u5907\u4efd\u811a\u672c\u91cc\u7684\u8def\u5f84\u3002\u8fd9\u91cc\u7684\u8def\u5f84\u53ea\u9700\u8981\u5177\u6709root\u6743\u9650\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<h1>MySQL\u5907\u4efd\u811a\u672c<\/h1>\n<p>\u4e0b\u9762\u5148\u8d34\u51fa\u672c\u6587\u6240\u9700\u8981\u4f7f\u7528\u7684MySQL\u5907\u4efd\u811a\u672c<\/p>\n<blockquote><p>#!\/bin\/bash<br \/>\nPATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/sbin:\/bin:\/usr\/sbin:\/usr\/bin:\/alidata\/server\/mysql\/bin:\/root\/bin:~\/bin<br \/>\nexport PATH<\/p>\n<p>#current datetime<br \/>\nDATE=<span class=\"katex math inline\">(date -d &#8216;+0 days&#8217; +%Y%m%d%H%M)<\/p>\n<p>#backup directory<br \/>\nBACKUP_ROOT=\/alidata\/backup\/databases<br \/>\nBACKUP_DIR=mysql<br \/>\nDATA_DIR=mysql_<\/span>DATE<\/p>\n<p>#get all database name<br \/>\nSQL_STRING=&#8221;SELECT SCHEMA_NAME AS db FROM information_schema.SCHEMATA WHERE SCHEMA_NAME NOT IN (&#8216;mysql&#8217;, &#8216;information_schema&#8217;, &#8216;performance_schema&#8217;);&#8221;<\/p>\n<p>DBS=<span class=\"katex math inline\">(echo<\/span>SQL_STRING | mysql -Bs)<\/p>\n<p>#package name<br \/>\nZIP_NAME=&#8221;mysql_&#8221;<span class=\"katex math inline\">DATE&#8221;.tar.gz&#8221;<\/p>\n<p>#remove backup of the database before 15 days<br \/>\nfind<\/span>BACKUP_ROOT\/<span class=\"katex math inline\">BACKUP_DIR -name &#8220;mysql_*.tar.gz&#8221; -type f -mtime +15 -exec rm {} \\;<\/p>\n<p>for DBNAME in<\/span>DBS<br \/>\ndo<br \/>\necho &#8220;backup &#8220;<span class=\"katex math inline\">DBNAME;<br \/>\nif [ ! -d<\/span>{BACKUP_ROOT}\/<span class=\"katex math inline\">{DATA_DIR} ]; then<br \/>\nmkdir -p<\/span>{BACKUP_ROOT}\/<span class=\"katex math inline\">{DATA_DIR}<br \/>\nfi<br \/>\nSQL_FILE=<\/span>DBNAME-<span class=\"katex math inline\">DATE&#8221;.sql&#8221;<br \/>\n\/alidata\/server\/mysql-5.6.21\/bin\/mysqldump &#8211;default-character-set=utf8<\/span>DBNAME &amp;gt; <span class=\"katex math inline\">BACKUP_ROOT\/<\/span>DATA_DIR\/<span class=\"katex math inline\">SQL_FILE<br \/>\ndone<\/p>\n<p>#Use tar to package all sql files<br \/>\ncd<\/span>BACKUP_ROOT<br \/>\ntar -czvPf <span class=\"katex math inline\">BACKUP_DIR\/<\/span>ZIP_NAME <span class=\"katex math inline\">DATA_DIR<\/p>\n<p>#After the success of the package to delete sql file<br \/>\nif [<\/span>? = 0 ]; then<br \/>\nrm -rf <span class=\"katex math inline\">BACKUP_ROOT\/<\/span>DATA_DIR<br \/>\nfi<\/p>\n<p>echo &#8220;mysql backup finished!&#8221;<\/p><\/blockquote>\n<p>\u4e0b\u9762\u5bf9\u4e0a\u9762\u7684\u5907\u4efd\u7528\u811a\u672c\u8fdb\u884c\u89e3\u91ca\u3002<br \/>\n\u9996\u5148\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u811a\u672c\u5f00\u59cb\u65f6\uff0c\u5bf9\u7cfb\u7edf\u53d8\u91cfPATH\u8fdb\u884c\u4e86\u91cd\u65b0\u7684\u590d\u5236\u548c\u5bfc\u5165\uff0c\u8fd9\u662f\u56e0\u4e3a\u4f7f\u7528crontab\u6267\u884c\u811a\u672c\u65f6\uff0c\u4e0d\u50cf\u662f\u5e73\u65f6\u4eba\u5de5\u4f7f\u7528ssh\u8fde\u63a5\u767b\u5f55CentOS\u7cfb\u7edf\u90a3\u6837\uff0c\u56e0\u4e3a\u6ca1\u6709\u767b\u5f55\u52a8\u4f5c\uff0c\u6240\u4ee5\u6ca1\u6709\u7528\u6237\uff0c\u6240\u4ee5\u5c31\u6ca1\u6709\u4e86\u7cfb\u7edf\u53d8\u91cf\uff0c\u8fd9\u6837\u5728\u547d\u4ee4\u4e2d\u6d89\u53ca\u5230\u7684\u4e00\u4e2a\u547d\u4ee4\uff0c\u53ef\u80fd\u4f1a\u53d8\u4e3a\u672a\u5b9a\u4e49\u7684\u547d\u4ee4\uff0c\u65e0\u6cd5\u6267\u884c\uff0c\u6240\u4ee5\uff0c\u8fd9\u91cc\u5148\u5b9a\u4e49\u73af\u5883\u53d8\u91cf\u3002\u8fd9\u91cc\u9700\u8981\u8bf4\u660e\uff0c\u6bcf\u53f0\u7535\u8111\u7684\u73af\u5883\u53d8\u91cf\u90fd\u4e0d\u5c3d\u76f8\u540c\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528<\/p>\n<blockquote><p>echo $PATH<\/p><\/blockquote>\n<p>\u547d\u4ee4\u8fdb\u884c\u67e5\u770b\uff0c\u7136\u540e\u5c06\u5176\u7ed3\u679c\uff0c\u590d\u5236\u5230\u8fd9\u4e2a\u4f4d\u7f6e\u4e0a\uff0c\u66ff\u6362\u8fd9\u4e2a\u811a\u672c\u4e2d\u7684\u5185\u5bb9\uff1b<br \/>\n\u7b2c\u4e8c\uff0c\u53ef\u4ee5\u770b\u5230\u6574\u4e2a\u811a\u672c\u4e2d\uff0c\u5e76\u672a\u51fa\u73b0MySQL\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u8fd9\u662f\u56e0\u4e3a\u672c\u6587\u6240\u63cf\u8ff0\u7684\u60c5\u51b5\u4e2d\uff0c\u4f7f\u7528\u7684\u662fMySQL 5.6\u7684\u7248\u672c\uff0c\u57285.6\u7684\u7248\u672c\u4e2d\uff0c\u82e5\u5c06MySQL\u7684\u7528\u6237\u540d\u3001\u5bc6\u7801\u5728\u811a\u672c\u4e2d\u8f93\u5165\u548c\u6267\u884c\uff0c\u4f1a\u88ab\u8b66\u544a\u4e3a\u4e0d\u5b89\u5168\u7684\u505a\u6cd5\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u5907\u4efd\u6240\u9700\u8981\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u5b58\u653e\u5728MySQL\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5373my.cnf\u6587\u4ef6\u4e2d\uff0c\u5177\u4f53\u5f62\u5f0f\u5982\u4e0b<\/p>\n<blockquote><p>[mysql]<br \/>\nuser=root<br \/>\npassword=123456<\/p>\n<p>[mysqldump]<br \/>\nuser=root<br \/>\npassword=123456<\/p><\/blockquote>\n<p>\u5176\u4e2d\uff0cmysql\u7684\u90e8\u5206\u662fmysql\u547d\u4ee4\u6240\u4f7f\u7528\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0cmysqldump\u7684\u90e8\u5206\u662fmysqldump\u547d\u4ee4\u6240\u4f7f\u7528\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u8fd9\u91cc\u4e00\u5b9a\u8981\u8fd9\u6837\u5206\u5f00\u5199\u6e05\u695a\uff0c\u5426\u5219\u4f1a\u88ab\u63d0\u793a\u4e3a\u6ca1\u6709\u6743\u9650\uff1b<br \/>\n\u7b2c\u4e09\uff0c\u8fd9\u4e2a\u811a\u672c\u7684\u5927\u81f4\u6d41\u7a0b\u662f\uff0c\u67e5\u8be2\u5230\u6240\u6709\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0c\u7136\u540e\u4f7f\u7528dump\u547d\u4ee4\u5bfc\u51fa\u6bcf\u4e2a\u6570\u636e\u5e93\u7684\u7ed3\u6784\u548c\u6570\u636e\uff0c\u5c06\u5176\u653e\u5728\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\u5939\u4e2d\uff08\u4e34\u65f6\u6587\u4ef6\u5939\u7684\u540d\u5b57\u4e3a\uff1amysql_+\u5e74\u6708\u65e5\u65f6\u5206\u79d2\uff09\uff0c\u5f53\u6240\u6709\u7684\u6570\u636e\u5e93\u90fd\u5bfc\u51fa\u5b8c\u6bd5\u540e\uff0c\u5c06\u8fd9\u4e2a\u4e34\u65f6\u6587\u4ef6\u6253\u5305\u5e76\u653e\u5165\u9884\u5148\u51c6\u5907\u597d\u7684\u6587\u4ef6\u5939\u4e2d\u3002\u5728\u4e0a\u8ff0\u8fc7\u7a0b\u4e2d\uff0c\u8fd8\u6709\u4e00\u4e2a\u6f0f\u6389\u7684\u903b\u8f91\u5c31\u662f\uff0c\u627e\u5230\u4fee\u6539\u65f6\u95f4\u5df2\u7ecf\u8d85\u8fc715\u5929\u7684\u538b\u7f29\u5305\uff0c\u5e76\u5c06\u5176\u5220\u9664\u3002<br \/>\n\u603b\u4f53\u6765\u8bf4\uff0c\u8fd9\u4e2a\u811a\u672c\u8fd8\u662f\u975e\u5e38\u597d\u7406\u89e3\u7684\u3002<\/p>\n<h1>\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1<\/h1>\n<p>\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u505a\u5907\u4efd\u7684\u662f\u4e00\u53f0\u6b63\u5728\u4e0a\u7ebf\u4f7f\u7528\u7684\u670d\u52a1\u5668\uff0c\u6211\u4eec\u901a\u8fc7\u5206\u6790\u670d\u52a1\u5668\u7684\u8bbf\u95ee\u60c5\u51b5\u540e\uff0c\u5f97\u5230\u6bcf\u5929\u51cc\u6668\u76842:30\u81f33:30\u4e4b\u95f4\u662f\u8bbf\u95ee\u91cf\u6700\u4f4e\u7684\u65f6\u95f4\u6bb5\uff0c\u90a3\u6211\u4eec\u5c31\u9700\u8981\u5728\u8fd9\u6bb5\u65f6\u95f4\u5185\u5b8c\u6210\u6570\u636e\u5e93\u7684\u5907\u4efd\u3002\u63a5\u4e0b\u6765\u4f7f\u7528crontab\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u9996\u5148\u4f7f\u7528<\/p>\n<blockquote><p>crontab -e<\/p><\/blockquote>\n<p>\u547d\u4ee4\u6253\u5f00\u5b9a\u65f6\u4efb\u52a1\u5217\u8868\uff0c\u7c7b\u4f3c\u4e8e\u4f7f\u7528vim\u7f16\u8f91\u6587\u672c\uff0c\u6211\u4eec\u8bbe\u7f6e\u6bcf\u5929\u76842:35\u5f00\u59cb\u8fdb\u884c\u6570\u636e\u5e93\u5907\u4efd\uff0c\u5219\u5177\u4f53\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>SHELL=\/bin\/bash<br \/>\nPATH=\/sbin:\/bin:\/usr\/sbin:\/usr\/bin<br \/>\nMAILTO=root<br \/>\nHOME=\/<\/p>\n<p># For details see man 4 crontabs<\/p>\n<p># Example of job definition:<br \/>\n# .&#8212;&#8212;&#8212;&#8212;&#8212;- minute (0 &#8211; 59)<br \/>\n# | .&#8212;&#8212;&#8212;&#8212;- hour (0 &#8211; 23)<br \/>\n# | | .&#8212;&#8212;&#8212;- day of month (1 &#8211; 31)<br \/>\n# | | | .&#8212;&#8212;- month (1 &#8211; 12) OR jan,feb,mar,apr &#8230;<br \/>\n# | | | | .&#8212;- day of week (0 &#8211; 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,f ri,sat<br \/>\n# | | | | |<br \/>\n# * * * * * user-name command to be executed<br \/>\n35 2 * * * \/root\/backup_mysql.sh<\/p><\/blockquote>\n<p>\u5176\u4e2d35\u4ee3\u8868\u6267\u884c\u7684\u5206\u949f\uff0c2\u4ee3\u8868\u6267\u884c\u7684\u5c0f\u65f6\uff0c\u540e\u9762\u7684*\u4ee3\u8868\u6bcf\u5929\u6bcf\u6708\u6bcf\u5468\uff0c\u540e\u9762\u7684\u5185\u5bb9\u662f\u8981\u6267\u884c\u7684\u811a\u672c\u6240\u5b58\u653e\u7684\u8def\u5f84\uff0c\u8fd9\u91cc\u662f\u5c06\u811a\u672c\u5b58\u653e\u5728root\u7528\u6237\u7684home\u76ee\u5f55\u4e0b\uff0c\u5373\/root\u76ee\u5f55\uff0c\u8fd9\u91cc\u5177\u4f53\u53ef\u4ee5\u67e5\u770bcrontab\u76f8\u5173\u7684\u8d44\u6599\u6587\u6863\u3002<\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5b8c\u6210\u4e86\uff0c\u6bcf\u5929\u51cc\u66682:35\u5206\u81ea\u52a8\u5907\u4efd\u5f53\u524d\u670d\u52a1\u5668\u4e0a\u9664mysql\u3001information_schema\u3001performance_schema\u4ee5\u5916\uff0c\u6240\u6709MySQL\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002<\/p>\n<h1>\u603b\u7ed3<\/h1>\n<p>\u672c\u6587\u7684\u91cd\u70b9\u8fd8\u662f\u81ea\u52a8\u5907\u4efd\u811a\u672c\uff0c\u5176\u4e2d\u9700\u8981\u6ce8\u610f\u5982\u4f55\u83b7\u53d6\u5f53\u524dMySQL\u670d\u52a1\u5668\u4e2d\u7684\u6240\u6709\u6570\u636e\u5e93\uff1b\u5982\u4f55\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6emysql\u547d\u4ee4\u53camysqldump\u547d\u4ee4\u6240\u4f7f\u7528\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\uff1b\u5982\u4f55\u5b9e\u73b0\u5220\u966415\u5929\u4e4b\u524d\u7684\u538b\u7f29\u5305\u7b49\u51e0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u89e3\u51b3\u4e86\u8fd9\u4e9b\u95ee\u9898\uff0c\u81ea\u52a8\u5316\u5907\u4efd\u5c31\u53d8\u7684\u7b80\u5355\u7684\u591a\u4e86\u3002<\/p>\n  \n  \n  ","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b9e\u73b0MySQL\u7684\u81ea\u52a8\u5907\u4efd\uff0c\u4e3b\u8981\u9700\u8981\u4e24\u4e2a\u8d44\u6599\uff0c\u4e00\u4e2a\u662fMySQL\u6570\u636e\u5e93\u5907\u4efd\u6240\u9700\u8981\u7684\u811a\u672c&hellip;<a href=\"https:\/\/www.softeng.cn\/?p=175\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u5728CentOS\u7cfb\u7edf\u4e0b\u5b9e\u73b0MySQL\u81ea\u52a8\u5168\u91cf\u5907\u4efd<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[20,23,25],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-19","tag-linux","tag-mysql","tag-25"],"_links":{"self":[{"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/posts\/175"}],"collection":[{"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=175"}],"version-history":[{"count":9,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":222,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/posts\/175\/revisions\/222"}],"wp:attachment":[{"href":"https:\/\/www.softeng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}