新聞中心
數據庫備份概述
創(chuàng)新互聯公司是專業(yè)的名山網站建設公司,名山接單;提供成都網站設計、網站建設,網頁設計,網站設計,建網站,PHP網站建設等專業(yè)做網站服務;采用PHP框架,可快速的進行名山網站開發(fā)網頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網站,專業(yè)的做網站團隊,希望更多企業(yè)前來合作!
概述:將數據庫中存在的現有數據,進行存放成為副本數據,可解決數據容災;
? ? ? ? ?? 提高系統(tǒng)的高可用性和災難恢復性,數據崩潰時,以最小代價重新恢復數據;
數據丟失的原因:程序錯誤、人為錯誤、磁盤錯誤、天災人禍
數據庫備份的分類
物理備份
? ? ? ?? 指對數據庫操作系統(tǒng)的物理文件(數據文件、日志文件)等備份
冷備份
必須在數據庫關閉狀態(tài)下進行備份,能夠更好保證數據庫的完整性
熱備份
能夠在數據庫處于正常運行的情況下備份,能夠更高的保證服務的可用性
邏輯備份
指對數據庫的邏輯組件(數據庫、表、數據對象)進行備份
完全備份
將數據進行完整的備份,包含完整的庫、表、索引、視圖等,需要花費更長的時間
差異備份
備份自上次完全備份到現在發(fā)生改變的數據庫內容,備份的文件比完整備份的文件小,備份速度更快
增量備份
? ? ? 備份至上次完全備份或增量備份后被修改的數據庫內容
數據備份以及恢復
物理備份(冷備份)
[root@host50?backup]#?cp?-rp?/var/lib/MySQL/dumptest1/???/opt/backup/dumptest1 [root@host50?backup]#?ls dumptest1 [root@host50?backup]#?ls?dumptest1/ db.opt??test.frm??test.ibd [root@host50?backup]#?ls?/var/lib/mysql/dumptest1/ db.opt??test.frm??test.ibd [root@host50?backup]#?tar?-zcvf?mysql-backup-$(date?+%F).tar.gz?dumptest1/ dumptest1/ dumptest1/db.opt dumptest1/test.frm dumptest1/test.ibd [root@host50?backup]#?ls dumptest1??mysql-backup-2019-07-07.tar.gz??????????????????????????????????????????//dumptest1庫備份并且壓縮完成 [root@host50?backup]#?rm?-dfr?/var/lib/mysql/dumptest1/????????????????????????????//刪除mysql數據庫dumptest1庫 [root@host50?backup]#?systemctl?restart?mysqld [root@host50?backup]#?mysql?-uroot?-p123456 mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. Welcome?to?the?MySQL?monitor.??Commands?end?with?;?or?\g. Your?MySQL?connection?id?is?3 Server?version:?5.7.17?MySQL?Community?Server?(GPL) Copyright?(c)?2000,?2016,?Oracle?and/or?its?affiliates.?All?rights?reserved. Oracle?is?a?registered?trademark?of?Oracle?Corporation?and/or?its affiliates.?Other?names?may?be?trademarks?of?their?respective owners. Type?'help;'?or?'\h'?for?help.?Type?'\c'?to?clear?the?current?input?statement. mysql>?show?databases;???????????????????????????????????????????????????????????????//查看所刪庫已經不在 +--------------------+ |?Database???????????| +--------------------+ |?information_schema?| |?mysql??????????????| |?performance_schema?| |?sys????????????????| |?transtb????????????| +--------------------+ 5?rows?in?set?(0.00?sec) [root@host50?backup]#?tar?-zxvf?mysql-backup-2019-07-07.tar.gz?-C?/var/lib/mysql/????????????//恢復數據庫 dumptest1/ dumptest1/db.opt dumptest1/test.frm dumptest1/test.ibd [root@host50?backup]#?systemctl?restart?mysqld [root@host50?backup]#?mysql?-uroot?-p1234566 mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. ERROR?1045?(28000):?Access?denied?for?user?'root'@'localhost'?(using?password:?YES) [root@host50?backup]#?mysql?-uroot?-p123456 mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. Welcome?to?the?MySQL?monitor.??Commands?end?with?;?or?\g. Your?MySQL?connection?id?is?4 Server?version:?5.7.17?MySQL?Community?Server?(GPL) Copyright?(c)?2000,?2016,?Oracle?and/or?its?affiliates.?All?rights?reserved. Oracle?is?a?registered?trademark?of?Oracle?Corporation?and/or?its affiliates.?Other?names?may?be?trademarks?of?their?respective owners. Type?'help;'?or?'\h'?for?help.?Type?'\c'?to?clear?the?current?input?statement. mysql>?show?databases; +--------------------+ |?Database???????????| +--------------------+ |?information_schema?| |?dumptest1??????????| |?mysql??????????????| |?performance_schema?| |?sys????????????????| |?transtb????????????| +--------------------+ 6?rows?in?set?(0.00?sec) mysql>?use?dumptest1; Reading?table?information?for?completion?of?table?and?column?names You?can?turn?off?this?feature?to?get?a?quicker?startup?with?-A Database?changed mysql>?select?*?from?test;??????????????????????????????????????????????????????????????//庫恢復成功 +-----+ |?job?| +-----+ |?aa??| |?bb??| |?c???| |?dd??| +-----+ 4?rows?in?set?(0.00?sec)
邏輯備份(熱備份)
庫名表示方式
?- --all-databases 或 -A ? ? ?? 所有庫
?-? 數據庫名 ? ? ? ? ? ? ? ? ? ? ? ? ? 單個庫
? ? ?? - 數據庫名 表名 ? ? ? ? ? ? ? ? ?? 單張表
? ? ?? - -B 數據庫1 數據庫2 ? ? ? ? ?? 多個庫
注意事項
- 無論備份還是恢復,都要驗證用戶權限
[root@host50?backup]#?mysqldump?-uroot?-p?--all-databases?>?/opt/backup/alldb.sql???????????//備份所有庫 Enter?password:? [root@host50?backup]#?ls alldb.sql [root@host50?backup]#?grep?-vE?'^/|^-|^$'?alldb.sql?|?head?-15?????????????????????????????//查看庫中部分內容 CREATE?DATABASE?/*!32312?IF?NOT?EXISTS*/?`dumptest1`?/*!40100?DEFAULT?CHARACTER?SET?latin1?*/; USE?`dumptest1`; DROP?TABLE?IF?EXISTS?`test`; CREATE?TABLE?`test`?( ??`job`?varchar(30)?NOT?NULL )?ENGINE=InnoDB?DEFAULT?CHARSET=latin1; LOCK?TABLES?`test`?WRITE; INSERT?INTO?`test`?VALUES?('aa'),('bb'),('c'),('dd'); UNLOCK?TABLES; CREATE?DATABASE?/*!32312?IF?NOT?EXISTS*/?`mysql`?/*!40100?DEFAULT?CHARACTER?SET?latin1?*/; USE?`mysql`; DROP?TABLE?IF?EXISTS?`columns_priv`; CREATE?TABLE?`columns_priv`?( ??`Host`?char(60)?COLLATE?utf8_bin?NOT?NULL?DEFAULT?'', ??`Db`?char(64)?COLLATE?utf8_bin?NOT?NULL?DEFAULT?'', ?? [root@host50?backup]#?mysqldump?-uroot?-p?dumptest1?>?/opt/backup/dumptest1.sql??????????????//只備份一個庫 Enter?password:? [root@host50?backup]#?ls alldb.sql??dumptest1.sql [root@host50?backup]#?mysqldump?-uroot?-p?-B?dumptest1??transtb>?/opt/backup/dumptest1+transdb.sql????//備份指定多個庫 Enter?password:? [root@host50?backup]#?ls alldb.sql??dumptest1.sql??dumptest1+transdb.sql
使用MYSQL命令從備份中恢復
通常不建議直接覆蓋舊庫,而是采用建立新庫并導入邏輯備份的方式執(zhí)行恢復,待新庫正常后即可廢棄或刪除舊庫。
[root@host50?backup]#?mysql?-uroot?-p?dumptest2?/opt/backup/dumptest1.sql? Enter?password:? [root@host50?backup]#?mysql?-uroot?-p123456 mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. Welcome?to?the?MySQL?monitor.??Commands?end?with?;?or?\g. Your?MySQL?connection?id?is?14 Server?version:?5.7.17?MySQL?Community?Server?(GPL) Copyright?(c)?2000,?2016,?Oracle?and/or?its?affiliates.?All?rights?reserved. Oracle?is?a?registered?trademark?of?Oracle?Corporation?and/or?its affiliates.?Other?names?may?be?trademarks?of?their?respective owners. Type?'help;'?or?'\h'?for?help.?Type?'\c'?to?clear?the?current?input?statement. mysql>?select?*?from?dumptest2.test; +-----+ |?job?| +-----+ |?aa??| |?bb??| |?c???| |?dd??| +-----+ 4?rows?in?set?(0.00?sec)
binlog日志概述
二進制日志用途及配置方式
類型 | 用途 | 配置 |
二進制日志 | 記錄所有更改數據的操作 | log_bin=[dir/name] server_id=數字 max_binlog_size=數字m |
采用binlog日志的好處
? ? -記錄除查詢之外的所有SQL命令
? ? -可用于數據恢復
? ? -配置MySQL主從同步的必要條件
binlog相關文件
- 主機名-bin.index ? ? ? ? ? ? 記錄已有日志文件名
- 主機名-bin.000001 ? ? ? ?? 第一個二進制日志
- 主機名-bin.000002 ? ? ? ?? 第二個二進制日志
例:
配置/etc/my.cnf,并重啟服務
[root@host50?backup]#?vim?/etc/my.cnf [mysqld] ..?.. log-bin-index=mysql-bin??????????????????????????????//啟用二進制日志,并指定前綴 server_id=1 binlog_format=STATEMENT?????????????????????????????//在Mysql5.7中,binlog日志格式默認為ROW,但它不記錄sql語句上下文相關信息。需要將binlog日志格式修改為STATEMENT [root@host50?backup]#?systemctl?restart?mysqld [root@host50?backup]#?ls?/var/lib/mysql/mysql-bin.*??//新啟用binlog后,每次啟動MySQl服務都會新生成一份日志文件 /var/lib/mysql/mysql-bin.000001??/var/lib/mysql/mysql-bin.index [root@host50?backup]#?ls?/var/lib/mysql/mysql-bin.*????//重啟MySQL服務程序,或者執(zhí)行SQL操作“FLUSH?LOGS;”,會生成一份新的日志: /var/lib/mysql/mysql-bin.000001??/var/lib/mysql/mysql-bin.index /var/lib/mysql/mysql-bin.000002 [root@host50?backup]#?cat?/var/lib/mysql/mysql-bin.index???//mysql-bin.index文件記錄了當前保持的二進制文件列表 ./mysql-bin.000001 ./mysql-bin.000002
使用binlog日志恢復表記錄
mysql>?create?database?binlogdb;???????????//創(chuàng)建庫?表,并刪除后恢復 Query?OK,?1?row?affected?(0.01?sec) mysql>?use?binlogdb; Database?changed mysql>?create?table?tb1( ????->?id?int(4), ????->?name?varchar(20)); Query?OK,?0?rows?affected?(0.00?sec) mysql>?insert?into?tb1?values ????->?(1,'aa'), ????->?(2,'bb'), ????->?(3,'cc'); Query?OK,?3?rows?affected?(0.21?sec) Records:?3??Duplicates:?0??Warnings:?0 mysql>?select?*?from?tb1; +------+------+ |?id???|?name?| +------+------+ |????1?|?aa???| |????2?|?bb???| |????3?|?cc???| +------+------+ 3?rows?in?set?(0.00?sec) mysql>?delete?from?tb1; Query?OK,?3?rows?affected?(0.00?sec) mysql>?select?*?from?tb1; Empty?set?(0.00?sec) [root@host50?backup]#?mysqlbinlog?/var/lib/mysql/host50-bin.000003?????//查看binlog日志確認恢復時間 /*!50530?SET?@@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003?SET?@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER?/*!*/; #?at?4 #190707??1:49:26?server?id?1??end_log_pos?123?CRC32?0x92d92fbb??Start:?binlog?v?4,?server?v?5.7.17-log?created?190707??1:49:26 #?Warning:?this?binlog?is?either?in?use?or?was?not?closed?properly. BINLOG?' Jt8gXQ8BAAAAdwAAAHsAAAABAAQANS43LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA Absv2ZI= '/*!*/; #?at?123 #190707??1:49:26?server?id?1??end_log_pos?154?CRC32?0x12dd5e8b??Previous-GTIDs #?[empty] #?at?154 #190707??1:53:03?server?id?1??end_log_pos?219?CRC32?0xba04792a??Anonymous_GTID?last_committed=0?sequence_number=1 SET?@@SESSION.GTID_NEXT=?'ANONYMOUS'/*!*/; #?at?219 #190707??1:53:03?server?id?1??end_log_pos?325?CRC32?0x651624b7??Query?thread_id=6?exec_time=0?error_code=0 SET?TIMESTAMP=1562435583/*!*/; SET?@@session.pseudo_thread_id=6/*!*/; SET?@@session.foreign_key_checks=1,?@@session.sql_auto_is_null=0,?@@session.unique_checks=1,?@@session.autocommit=1/*!*/; SET?@@session.sql_mode=1436549152/*!*/; SET?@@session.auto_increment_increment=1,?@@session.auto_increment_offset=1/*!*/; /*!\C?utf8?*//*!*/; SET?@@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/; SET?@@session.lc_time_names=0/*!*/; SET?@@session.collation_database=DEFAULT/*!*/; create?database?binlogdb /*!*/; #?at?325 #190707??1:53:43?server?id?1??end_log_pos?390?CRC32?0x13116bdf??Anonymous_GTID?last_committed=1?sequence_number=2 SET?@@SESSION.GTID_NEXT=?'ANONYMOUS'/*!*/; #?at?390 #190707??1:53:43?server?id?1??end_log_pos?518?CRC32?0x431ff6ab??Query?thread_id=6?exec_time=0?error_code=0 use?`binlogdb`/*!*/; SET?TIMESTAMP=1562435623/*!*/; create?table?tb1( id?int(4), name?varchar(20)) /*!*/; #?at?518 #190707??1:54:23?server?id?1??end_log_pos?583?CRC32?0x8dc7fcdb??Anonymous_GTID?last_committed=2?sequence_number=3 SET?@@SESSION.GTID_NEXT=?'ANONYMOUS'/*!*/; #?at?583 #190707??1:54:23?server?id?1??end_log_pos?670?CRC32?0x58cc7317??Query?thread_id=6?exec_time=0?error_code=0 SET?TIMESTAMP=1562435663/*!*/; BEGIN /*!*/; #?at?670 #190707??1:54:23?server?id?1??end_log_pos?803?CRC32?0xf0848df1??Query?thread_id=6?exec_time=0?error_code=0 SET?TIMESTAMP=1562435663/*!*/; insert?into?tb1?values (1,'aa'), (2,'bb'), (3,'cc') /*!*/; #?at?803 #190707??1:54:23?server?id?1??end_log_pos?834?CRC32?0x01790e76??Xid?=?17 COMMIT/*!*/; #?at?834 #190707??1:54:55?server?id?1??end_log_pos?899?CRC32?0xba024a34??Anonymous_GTID?last_committed=3?sequence_number=4 SET?@@SESSION.GTID_NEXT=?'ANONYMOUS'/*!*/; #?at?899 #190707??1:54:55?server?id?1??end_log_pos?986?CRC32?0xc99b2859??Query?thread_id=6?exec_time=0?error_code=0 SET?TIMESTAMP=1562435695/*!*/; BEGIN /*!*/; #?at?986 #190707??1:54:55?server?id?1??end_log_pos?1083?CRC32?0xda0eb644??Query?thread_id=6?exec_time=0?error_code=0 SET?TIMESTAMP=1562435695/*!*/; delete?from?tb1 /*!*/; #?at?1083 #190707??1:54:55?server?id?1??end_log_pos?1114?CRC32?0xd72d7ba3??Xid?=?19 COMMIT/*!*/; SET?@@SESSION.GTID_NEXT=?'AUTOMATIC'?/*?added?by?mysqlbinlog?*/?/*!*/; DELIMITER?; #?End?of?log?file /*!50003?SET?COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530?SET?@@SESSION.PSEUDO_SLAVE_MODE=0*/; [root@host50?backup]#?mysqlbinlog?--start-datetime="2019-07-07?1:54:23"?--stop-datetime="2019-07-07?1:54:55"?/var/lib/mysql/host50-bin.000003?|?mysql?-uroot?-p123456 mysql>?select?*?from?binlogdb.tb1; +------+------+ |?id???|?name?| +------+------+ |????1?|?aa???| |????2?|?bb???| |????3?|?cc???| +------+------+ 3?rows?in?set?(0.00?sec)
生產環(huán)境 Mysql 數據庫備份策略
策略設計思路:
1.數據更新頻繁,則應該進行較為頻繁的備份;
2.數據較為重要,則在有適當更新時進行備份;
3.在數據庫壓力小的時段進行全量備份;? ?備份方案:
1.在每周末的某個時段使用mysqldump進行重要數據庫的全量備份(最好備份單個數據庫,
而不是all);
2.在平時每天的晚上時段將二進制日志文件終止(前提需要根據數據記錄的生成數量合理指
定單個二進制文件的大小),這樣每隔24小時會生成一個二進制日志文件;
3.每周的全量備份文件和每天的二進制日志文件相加,就是這一周內數據庫的整體內容;
4.出現故障時, 首先恢復全量備份, 可以執(zhí)行mysqlbinlog命令結合實際情況根據position值
或者時間點進行恢復,確保萬無一失;
網站標題:數據庫備份概述
網頁網址:http://www.ef60e0e.cn/article/iehiid.html