{"id":127462,"date":"2018-11-20T09:42:11","date_gmt":"2018-11-20T01:42:11","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=127462"},"modified":"2018-11-12T09:44:25","modified_gmt":"2018-11-12T01:44:25","slug":"mysql-backup-database","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-backup-database.html","title":{"rendered":"mysql\u5b9e\u73b0\u4e3b\u4ece\u590d\u5236"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nMysql\u7684 Replication \u662f\u4e00\u4e2a\u5f02\u6b65\u7684\u590d\u5236\u8fc7\u7a0b\uff08mysql5.1.7\u4ee5\u4e0a\u7248\u672c\u5206\u4e3a\u5f02\u6b65\u590d\u5236\u548c\u534a\u540c\u6b65\u4e24\u79cd\u6a21\u5f0f\uff09\uff0c\u4ece\u4e00\u4e2a Mysql instace(\u6211\u4eec\u79f0\u4e4b\u4e3a Master)\u590d\u5236\u5230\u53e6\u4e00\u4e2a Mysql instance(\u6211\u4eec\u79f0\u4e4b Slave)\u3002\u5728 Master \u4e0e Slave \u4e4b\u95f4\u7684\u5b9e\u73b0\u6574\u4e2a\u590d\u5236\u8fc7\u7a0b\u4e3b\u8981\u7531\u4e09\u4e2a\u7ebf\u7a0b\u6765\u5b8c\u6210\uff0c\u5176\u4e2d\u4e24\u4e2a\u7ebf\u7a0b(Sql\u7ebf\u7a0b\u548cIO\u7ebf\u7a0b)\u5728 Slave \u7aef\uff0c\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b(IO\u7ebf\u7a0b)\u5728 Master \u7aef\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

1.1. mysql\u4e3b\u4ece\u7684\u539f\u7406<\/strong><\/div>\n
1.1.1. Replication \u7ebf\u7a0b<\/strong><\/span><\/div>\n

Mysql\u7684 Replication \u662f\u4e00\u4e2a\u5f02\u6b65\u7684\u590d\u5236\u8fc7\u7a0b\uff08mysql5.1.7\u4ee5\u4e0a\u7248\u672c\u5206\u4e3a\u5f02\u6b65\u590d\u5236\u548c\u534a\u540c\u6b65\u4e24\u79cd\u6a21\u5f0f\uff09\uff0c\u4ece\u4e00\u4e2a Mysql instace(\u6211\u4eec\u79f0\u4e4b\u4e3a Master)\u590d\u5236\u5230\u53e6\u4e00\u4e2a Mysql instance(\u6211\u4eec\u79f0\u4e4b Slave)\u3002\u5728 Master \u4e0e Slave \u4e4b\u95f4\u7684\u5b9e\u73b0\u6574\u4e2a\u590d\u5236\u8fc7\u7a0b\u4e3b\u8981\u7531\u4e09\u4e2a\u7ebf\u7a0b\u6765\u5b8c\u6210\uff0c\u5176\u4e2d\u4e24\u4e2a\u7ebf\u7a0b(Sql\u7ebf\u7a0b\u548cIO\u7ebf\u7a0b)\u5728 Slave \u7aef\uff0c\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b(IO\u7ebf\u7a0b)\u5728 Master \u7aef\u3002<\/p>\n

\u8981\u5b9e\u73b0 MySQL \u7684 Replication \uff0c\u9996\u5148\u5fc5\u987b\u6253\u5f00 Master \u7aef\u7684Binary Log(mysql-bin.xxxxxx)\u529f\u80fd\uff0c\u5426\u5219\u65e0\u6cd5\u5b9e\u73b0\u3002\u56e0\u4e3a\u6574\u4e2a\u590d\u5236\u8fc7\u7a0b\u5b9e\u9645\u4e0a\u5c31\u662fSlave\u4eceMaster\u7aef\u83b7\u53d6\u8be5\u65e5\u5fd7\u7136\u540e\u518d\u5728\u81ea\u5df1\u8eab\u4e0a\u5b8c\u5168 \u987a\u5e8f\u7684\u6267\u884c\u65e5\u5fd7\u4e2d\u6240\u8bb0\u5f55\u7684\u5404\u79cd\u64cd\u4f5c\u3002\u6253\u5f00 MySQL \u7684 Binary Log \u53ef\u4ee5\u901a\u8fc7\u5728\u542f\u52a8 MySQL Server \u7684\u8fc7\u7a0b\u4e2d\u4f7f\u7528 \u201c\u2014log-bin\u201d \u53c2\u6570\u9009\u9879\uff0c\u6216\u8005\u5728 my.cnf \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 mysqld \u53c2\u6570\u7ec4([mysqld]\u6807\u8bc6\u540e\u7684\u53c2\u6570\u90e8\u5206)\u589e\u52a0 \u201clog-bin\u201d \u53c2\u6570\u9879\u3002<\/p>\n

1.1.2. MySQL \u590d\u5236\u7684\u57fa\u672c\u8fc7\u7a0b\uff1a<\/strong><\/span><\/div>\n

1.Slave \u4e0a\u9762\u7684IO\u7ebf\u7a0b\u8fde\u63a5\u4e0a Master\uff0c\u5e76\u8bf7\u6c42\u4ece\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u7684\u6307\u5b9a\u4f4d\u7f6e(\u6216\u8005\u4ece\u6700\u5f00\u59cb\u7684\u65e5\u5fd7)\u4e4b\u540e\u7684\u65e5\u5fd7\u5185\u5bb9\uff1b<\/p>\n

Master \u63a5\u6536\u5230\u6765\u81ea Slave \u7684 IO \u7ebf\u7a0b\u7684\u8bf7\u6c42\u540e\uff0c\u901a\u8fc7\u8d1f\u8d23\u590d\u5236\u7684 IO \u7ebf\u7a0b\u6839\u636e\u8bf7\u6c42\u4fe1\u606f\u8bfb\u53d6\u6307\u5b9a\u65e5\u5fd7\u6307\u5b9a\u4f4d\u7f6e\u4e4b\u540e\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u8fd4\u56de\u7ed9 Slave \u7aef\u7684 IO \u7ebf\u7a0b\u3002\u8fd4\u56de\u4fe1\u606f\u4e2d\u9664\u4e86\u65e5\u5fd7\u6240\u5305\u542b\u7684\u4fe1\u606f\u4e4b\u5916\uff0c\u8fd8\u5305\u62ec\u672c\u6b21\u8fd4\u56de\u7684\u4fe1\u606f\u5728 Master \u7aef\u7684 Binary Log \u6587\u4ef6\u7684\u540d\u79f0\u4ee5\u53ca\u5728 Binary Log \u4e2d\u7684\u4f4d\u7f6e\uff1b<\/p>\n

Slave \u7684 IO \u7ebf\u7a0b\u63a5\u6536\u5230\u4fe1\u606f\u540e\uff0c\u5c06\u63a5\u6536\u5230\u7684\u65e5\u5fd7\u5185\u5bb9\u4f9d\u6b21\u5199\u5165\u5230 Slave \u7aef\u7684Relay Log\u6587\u4ef6(mysql-relay-bin.xxxxxx)\u7684\u6700\u672b\u7aef\uff0c\u5e76\u5c06\u8bfb\u53d6\u5230\u7684Master\u7aef\u7684bin-log\u7684\u6587\u4ef6\u540d\u548c\u4f4d\u7f6e\u8bb0\u5f55\u5230master- info\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u5728\u4e0b\u4e00\u6b21\u8bfb\u53d6\u7684\u65f6\u5019\u80fd\u591f\u6e05\u695a\u7684\u9ad8\u901fMaster\u201c\u6211\u9700\u8981\u4ece\u67d0\u4e2abin-log\u7684\u54ea\u4e2a\u4f4d\u7f6e\u5f00\u59cb\u5f80\u540e\u7684\u65e5\u5fd7\u5185\u5bb9\uff0c\u8bf7\u53d1\u7ed9\u6211\u201d<\/p>\n

Slave \u7684 SQL \u7ebf\u7a0b\u68c0\u6d4b\u5230 Relay Log \u4e2d\u65b0\u589e\u52a0\u4e86\u5185\u5bb9\u540e\uff0c\u4f1a\u9a6c\u4e0a\u89e3\u6790\u8be5 Log \u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u6210\u4e3a\u5728 Master \u7aef\u771f\u5b9e\u6267\u884c\u65f6\u5019\u7684\u90a3\u4e9b\u53ef\u6267\u884c\u7684 Query \u8bed\u53e5\uff0c\u5e76\u5728\u81ea\u8eab\u6267\u884c\u8fd9\u4e9b Query\u3002\u8fd9\u6837\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u5728 Master \u7aef\u548c Slave \u7aef\u6267\u884c\u4e86\u540c\u6837\u7684 Query\uff0c\u6240\u4ee5\u4e24\u7aef\u7684\u6570\u636e\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002<\/p>\n

1.2. db01<\/strong><\/div>\n
1.2.1. \u5728\u4e3b\u5e93\u4e0a\u5f00\u542f\u7684binlog\u65e5\u5fd7<\/strong><\/span><\/div>\n
[root@db01 ~]# cat \/etc\/my.cnf\r\n[mysqld]\r\nlog-bin\r\nserver-id=160<\/pre>\n
1.2.2. \u91cd\u542f\u6570\u636e\u5e93<\/strong><\/span><\/div>\n
[root@db01 ~]# systemctl restart mysqld<\/pre>\n
1.2.3. \u6388\u6743\uff0c\u5141\u8bb8\u80fd\u591f\u8fdc\u7a0b\u8fde\u63a5\u7684\u4e3b\u673a\uff08replicaiton\uff09<\/strong><\/span><\/div>\n
mysql> grant replication slave, replication client on *.* to 'repl'@'172.16.1.52' identified by 'All123.com';<\/pre>\n
1.2.4. \u5bfc\u51fa\u4e3b\u5e93\u5f53\u524d\u6570\u636e<\/strong><\/span><\/div>\n
[root@db01 ~]# mysqldump -uroot -pBgx123.com \\\r\n--all-databases \\\r\n--single-transaction \\\r\n--master-data=1 \\\r\n--flush-logs > \/root\/db-$(date +%F)-all.sql<\/pre>\n
1.2.5. \u62f7\u8d1d\u5907\u4efd\u7684\u6570\u636e\u81f3\u4ece\u5e93<\/strong><\/span><\/div>\n
[root@db01 ~]# scp db-2018-10-23-all.sql root@172.16.1.52:\/tmp<\/pre>\n
1.3. db02<\/strong><\/div>\n

\u5728\u4ece\u5e93\u4e0a\u542f\u52a8\u6570\u636e\u5e93\uff0c\u7136\u540e\u5012\u5165\u6570\u636e<\/p>\n

1.3.1. \u68c0\u67e5\u662f\u5426\u80fd\u4f7f\u7528\u8fdc\u7a0b\u8d26\u6237\u767b\u5f55<\/strong><\/span><\/div>\n
[root@slave ~]# mysql -h 172.16.1.51 -urep -pRep123.com<\/pre>\n
1.3.2. \u4fee\u6539\u914d\u7f6e\u6587\u4ef6\/etc\/my.cnf\uff0c\u4ece\u9700\u5f00\u542fbinlog<\/strong><\/span><\/div>\n
server-id=161<\/pre>\n
1.3.3. \u91cd\u542fMySQL\u7684\u6570\u636e\u5e93\u670d\u52a1<\/strong><\/span><\/div>\n
systemctl restart mysqld<\/pre>\n
1.3.4. \u767b\u9646\u7684MySQL\u6570\u636e\u5e93[\u5bc6\u7801\u4e2d\u586b\u5199\u4e0a\u4e00\u6b65\u8fc7\u6ee4\u7684\u5bc6\u7801]<\/strong><\/span><\/div>\n
[root@web02 ~]# mysql -uroot -p$(awk '\/temporary password\/{print $NF}' \/var\/log\/mysqld.log)<\/pre>\n
1.3.5. \u91cd\u65b0\u4fee\u6539\u6570\u636e\u5e93\u5bc6\u7801<\/strong><\/span><\/div>\n
mysql> ALTER USER 'root'@'localhos``t' IDENTIFIED BY 'Bgx123.com';<\/pre>\n
1.3.6. \u5bfc\u5165\u6570\u636e\uff0c\u8ffd\u4e3b\u7684bin_log<\/strong><\/span><\/div>\n
[root@db02 ~]# mysql -uroot -pBgx123.com < \/tmp\/db-2018-10-23-all.sql<\/pre>\n
1.3.7. \u6307\u5411\u4e3b\uff0c\u65e0\u9700\u6307\u5b9abinlogfile\u548cPOS<\/strong><\/span><\/div>\n
mysql> change master to\r\nmaster_host='172.16.1.51',\r\nmaster_user='repl',\r\nmaster_password='All123.com';<\/pre>\n
1.3.8. \u542f\u52a8slave\u7ebf\u7a0b<\/strong><\/span><\/div>\n
mysql> start slave;<\/pre>\n
1.3.9. \u67e5\u770bMySQL\u7684\u4e3b\u4ece\u72b6\u6001<\/strong><\/span><\/div>\n
mysql> show slave status\\G\r\n*************************** 1. row ***************************\r\nSlave_IO_State: Waiting for master to send event\r\nMaster_Host: 172.16.1.51\r\nMaster_User: rep\r\nMaster_Port: 3306\r\nConnect_Retry: 60\r\nMaster_Log_File: db01-bin.000002\r\nRead_Master_Log_Pos: 6671\r\nRelay_Log_File: db02-relay-bin.000004\r\nRelay_Log_Pos: 6882\r\nRelay_Master_Log_File: db01-bin.000002\r\nSlave_IO_Running: Yes\r\nSlave_SQL_Running: Yes<\/pre>\n","protected":false},"excerpt":{"rendered":"

Mysql\u7684 Replication \u662f\u4e00\u4e2a\u5f02\u6b65\u7684\u590d\u5236\u8fc7\u7a0b\uff08mysql5.1.7\u4ee5\u4e0a\u7248\u672c\u5206\u4e3a\u5f02\u6b65\u590d\u5236\u548c\u534a\u540c\u6b65\u4e24 […]<\/p>\n","protected":false},"author":1769,"featured_media":127465,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-127462","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/127462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1769"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=127462"}],"version-history":[{"count":8,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/127462\/revisions"}],"predecessor-version":[{"id":127809,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/127462\/revisions\/127809"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/127465"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=127462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=127462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=127462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}