mysql 51 bin_mysqldump和bin-log备份以及恢复示例
mysql版本[root@zxl-nginx?~]#?mysql?-V
mysql??Ver?14.14?Distrib?5.6.28,?for?linux-glibc2.5?(x86_64)?using??EditLine?wrapper
一、mysqldump
mysqldump僅適用于數(shù)據(jù)集較小場景
用法:mysqldump?[options]?[db_name?[tbl_name?...]]
主要選項(xiàng)解釋:
--all-databases,?-A:備份所有數(shù)據(jù)庫
--databases,?-B:要備份的數(shù)據(jù)庫,可以同時(shí)備份多個,使用空格分隔
--flush-logs,?-F:備份前、請求到鎖之后滾動日志,要記錄下復(fù)制時(shí)的二進(jìn)制日志
--flush-privileges:通知數(shù)據(jù)庫重讀授權(quán)表
--host=host_name,?-h?host_name:要備份的數(shù)據(jù)庫的主機(jī)名,可以基于網(wǎng)絡(luò)備份
--lock-all-tables,?-x:請求鎖定所有表之后再備份,對MyISAM,InnoDB,Aria做溫備
--single-transaction:能夠?qū)nnoDB存儲引擎實(shí)現(xiàn)熱備
-u?usename?備份的用戶名
-p?password?登陸數(shù)據(jù)庫的密碼
--events:備份事件調(diào)度器代碼
--routines:備份存儲過程和存儲函數(shù)
--triggers:備份觸發(fā)器
--master-date={0|1|2},0表示不記錄,1表示距離為change?master?語句,2表示記錄為注釋的change?master語句
二、創(chuàng)建數(shù)據(jù)庫以及表并插入數(shù)據(jù)mysql>?create?database?zxl;
Query?OK,?1?row?affected?(0.00?sec)
mysql>?use?zxl
Database?changed
mysql>?CREATE?TABLE?`users`?(
->???`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
->???`name`?varchar(255)?DEFAULT?NULL,
->???PRIMARY?KEY?(`id`)
->?)?ENGINE=InnoDB?AUTO_INCREMENT=7?DEFAULT?CHARSET=utf8;
Query?OK,?0?rows?affected?(0.02?sec)
#這是創(chuàng)建表以及插入數(shù)據(jù)的示例,來自互聯(lián)網(wǎng)。。。#--?----------------------------
#--?Table?structure?for?users
#--?----------------------------
#DROP?TABLE?IF?EXISTS?`users`;
#CREATE?TABLE?`users`?(
#??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
#??`name`?varchar(255)?DEFAULT?NULL,
#??PRIMARY?KEY?(`id`)
#)?ENGINE=InnoDB?AUTO_INCREMENT=7?DEFAULT?CHARSET=utf8;
#
#--?----------------------------
#--?Records?of?users
#--?----------------------------
#INSERT?INTO?`users`?VALUES?('1',?'小明');
#INSERT?INTO?`users`?VALUES?('2',?'小虎');
#INSERT?INTO?`users`?VALUES?('3',?'小花');
#INSERT?INTO?`users`?VALUES?('4',?'小花');
#INSERT?INTO?`users`?VALUES?('5',?'小花');
#INSERT?INTO?`users`?VALUES?('6',?'小虎');
插入數(shù)據(jù)mysql>?INSERT?INTO?`users`?VALUES?('1',?'小明');
Query?OK,?1?row?affected?(0.00?sec)
mysql>?INSERT?INTO?`users`?VALUES?('2',?'小虎');
Query?OK,?1?row?affected?(0.00?sec)
mysql>?INSERT?INTO?`users`?VALUES?('3',?'小花');
Query?OK,?1?row?affected?(0.00?sec)
mysql>?INSERT?INTO?`users`?VALUES?('4',?'小花');
Query?OK,?1?row?affected?(0.00?sec)
mysql>?INSERT?INTO?`users`?VALUES?('5',?'小花');
Query?OK,?1?row?affected?(0.01?sec)
mysql>?INSERT?INTO?`users`?VALUES?('6',?'小虎');
Query?OK,?1?row?affected?(0.00?sec)
查看插入的數(shù)據(jù)mysql>?select?*?from?users;
+----+--------+
|?id?|?name???|
+----+--------+
|??1?|?小明???|
|??2?|?小虎???|
|??3?|?小花???|
|??4?|?小花???|
|??5?|?小花???|
|??6?|?小虎???|
+----+--------+
6?rows?in?set?(0.00?sec)
三、備份數(shù)據(jù)庫以及演示如何恢復(fù)[root@zxl-nginx?~]#?mysqldump?-uroot?-p123456?--databases?zxl?--single-transaction?--flush-logs?--master-data=2?>?/tmp/zxl_users.sql
Warning: Using a password on the command line interface can be insecure.
注:提示warning,因?yàn)?.6版本增加安全機(jī)制,不允許在命令行中出現(xiàn)密碼,具體沒研究,可以在my.cnf文件中加入[mysqldump]下加入用戶以及密碼就不會提示warning了。
備份數(shù)據(jù)庫之后,再次插入新的數(shù)據(jù)。mysql>?INSERT?INTO?`users`?VALUES?('7',?'bob');
Query?OK,?1?row?affected?(0.01?sec)
mysql>?INSERT?INTO?`users`?VALUES?('8',?'tom');
Query?OK,?1?row?affected?(0.00?sec)
mysql>?INSERT?INTO?`users`?VALUES?('9',?'lili');
Query?OK,?1?row?affected?(0.00?sec)
查看新插入的數(shù)據(jù)mysql>?select?*?from?users;
+----+--------+
|?id?|?name???|
+----+--------+
|??1?|?小明???|
|??2?|?小虎???|
|??3?|?小花???|
|??4?|?小花???|
|??5?|?小花???|
|??6?|?小虎???|
|??7?|?bob????|
|??8?|?tom????|
|??9?|?lili???|
+----+--------+
9?rows?in?set?(0.00?sec)
刪除數(shù)據(jù)庫zxlmysql>?drop?database?zxl;
Query?OK,?1?row?affected?(0.01?sec)
mysql>?show?databases;
+--------------------+
|?Database???????????|
+--------------------+
|?information_schema?|
|?mysql??????????????|
|?performance_schema?|
|?test???????????????|
+--------------------+
4?rows?in?set?(0.00?sec)
查看mysqldump備份的sql文件中的pos節(jié)點(diǎn)位置以及相應(yīng)的二進(jìn)制文件名
二進(jìn)制文件以及pos節(jié)點(diǎn)如下:
-- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000060', MASTER_LOG_POS=120;
使用mysqlbiglog查看二進(jìn)制文件,刪除數(shù)據(jù)庫zxl的at位置是778[root@zxl-nginx?data]#?mysqlbinlog?master-bin.000060
#?at?778
#160120?14:25:42?server?id?1??end_log_pos?867?CRC32?0x014503a4?Querythread_id=44exec_time=0error_code=0
SET?TIMESTAMP=1453271142/*!*/;
drop?database?zxl
備份二進(jìn)制日志位置[root@zxl-nginx?~]#?mysqlbinlog?--start-position=120?--stop-position=778?/usr/local/mysql/data/master-bin.000060?>?/tmp/big_log.sql
恢復(fù)數(shù)據(jù)庫[root@zxl-nginx?~]#?mysql?-uroot?-p?
Enter?password:
查看恢復(fù)的數(shù)據(jù)庫
關(guān)閉二進(jìn)制日志mysql>?set?session?sql_log_bin=0;
Query?OK,?0?rows?affected?(0.00?sec)
mysql>?show?databases;
+--------------------+
|?Database???????????|
+--------------------+
|?information_schema?|
|?mysql??????????????|
|?performance_schema?|
|?test???????????????|
|?zxl????????????????|
+--------------------+
5?rows?in?set?(0.00?sec)查看恢復(fù)的表
mysql>?use?zxl;
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>?show?tables;
+---------------+
|?Tables_in_zxl?|
+---------------+
|?users?????????|
+---------------+
1?row?in?set?(0.00?sec)
mysql>?select?*?from?users;
+----+--------+
|?id?|?name???|
+----+--------+
|??1?|?小明???|
|??2?|?小虎???|
|??3?|?小花???|
|??4?|?小花???|
|??5?|?小花???|
|??6?|?小虎???|
+----+--------+
6?rows?in?set?(0.00?sec)
注:后增加的數(shù)據(jù)沒有恢復(fù)過來
恢復(fù)二進(jìn)制備份的big-log文件[root@zxl-nginx?~]#?mysql?-uroot?-p?
Enter?password:
再次查看users表mysql>?select?*?from?users;
+----+--------+
|?id?|?name???|
+----+--------+
|??1?|?小明???|
|??2?|?小虎???|
|??3?|?小花???|
|??4?|?小花???|
|??5?|?小花???|
|??6?|?小虎???|
|??7?|?bob????|
|??8?|?tom????|
|??9?|?lili???|
+----+--------+
9?rows?in?set?(0.00?sec)
開啟big-logmysql>?set?session?sql_log_bin=1;
Query?OK,?0?rows?affected?(0.00?sec)
注:關(guān)閉不關(guān)閉二進(jìn)制只有不進(jìn)行任何操作即可不關(guān)閉,原因你懂的。
注:在實(shí)際恢復(fù)時(shí)最好編輯my.cnf配置文件,添加如下項(xiàng):
skip-networking ? ?//跳過網(wǎng)絡(luò)功能來恢復(fù)數(shù)據(jù)
總結(jié)
以上是生活随笔為你收集整理的mysql 51 bin_mysqldump和bin-log备份以及恢复示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为mate20pro和华为p30pro
- 下一篇: kettle mysql 分页_kett