mysql版本
[root@zxl-nginx~]#mysql-VmysqlVer14.14Distrib5.6.28,forlinux-glibc2.5(x86_64)usingEditLinewrapper
一、mysqldump
mysqldump仅适用于数据集较小场景
用法:mysqldump[options][db_name[tbl_name...]]主要选项解释:--all-databases,-A:备份所有数据库--databases,-B:要备份的数据库,可以同时备份多个,使用空格分隔--flush-logs,-F:备份前、请求到锁之后滚动日志,要记录下复制时的二进制日志--flush-privileges:通知数据库重读授权表--host=host_name,-hhost_name:要备份的数据库的主机名,可以基于网络备份--lock-all-tables,-x:请求锁定所有表之后再备份,对MyISAM,InnoDB,Aria做温备--single-transaction:能够对InnoDB存储引擎实现热备-uusename备份的用户名-ppassword登陆数据库的密码--events:备份事件调度器代码--routines:备份存储过程和存储函数--triggers:备份触发器--master-date={0|1|2},0表示不记录,1表示距离为changemaster语句,2表示记录为注释的changemaster语句
二、创建数据库以及表并插入数据
mysql>createdatabasezxl;QueryOK,1rowaffected(0.00sec)mysql>usezxlDatabasechangedmysql>CREATETABLE`users`(->`id`bigint(20)NOTNULLAUTO_INCREMENT,->`name`varchar(255)DEFAULTNULL,->PRIMARYKEY(`id`)->)ENGINE=InnoDBAUTO_INCREMENT=7DEFAULTCHARSET=utf8;QueryOK,0rowsaffected(0.02sec)
#这是创建表以及插入数据的示例,来自互联网。。。
#------------------------------#--Tablestructureforusers#------------------------------#DROPTABLEIFEXISTS`users`;#CREATETABLE`users`(#`id`bigint(20)NOTNULLAUTO_INCREMENT,#`name`varchar(255)DEFAULTNULL,#PRIMARYKEY(`id`)#)ENGINE=InnoDBAUTO_INCREMENT=7DEFAULTCHARSET=utf8;##------------------------------#--Recordsofusers#------------------------------#INSERTINTO`users`VALUES('1','小明');#INSERTINTO`users`VALUES('2','小虎');#INSERTINTO`users`VALUES('3','小花');#INSERTINTO`users`VALUES('4','小花');#INSERTINTO`users`VALUES('5','小花');#INSERTINTO`users`VALUES('6','小虎');
插入数据
mysql>INSERTINTO`users`VALUES('1','小明');QueryOK,1rowaffected(0.00sec)mysql>INSERTINTO`users`VALUES('2','小虎');QueryOK,1rowaffected(0.00sec)mysql>INSERTINTO`users`VALUES('3','小花');QueryOK,1rowaffected(0.00sec)mysql>INSERTINTO`users`VALUES('4','小花');QueryOK,1rowaffected(0.00sec)mysql>INSERTINTO`users`VALUES('5','小花');QueryOK,1rowaffected(0.01sec)mysql>INSERTINTO`users`VALUES('6','小虎');QueryOK,1rowaffected(0.00sec)
查看插入的数据
mysql>select*fromusers;+----+--------+|id|name|+----+--------+|1|小明||2|小虎||3|小花||4|小花||5|小花||6|小虎|+----+--------+6rowsinset(0.00sec)
三、备份数据库以及演示如何恢复
[root@zxl-nginx~]#mysqldump-uroot-p123456--databaseszxl--single-transaction--flush-logs--master-data=2>/tmp/zxl_users.sql
Warning: Using a password on the command line interface can be insecure.
注:提示warning,因为5.6版本增加安全机制,不允许在命令行中出现密码,具体没研究,可以在my.cnf文件中加入[mysqldump]下加入用户以及密码就不会提示warning了。
备份数据库之后,再次插入新的数据。
mysql>INSERTINTO`users`VALUES('7','bob');QueryOK,1rowaffected(0.01sec)mysql>INSERTINTO`users`VALUES('8','tom');QueryOK,1rowaffected(0.00sec)mysql>INSERTINTO`users`VALUES('9','lili');QueryOK,1rowaffected(0.00sec)
查看新插入的数据
mysql>select*fromusers;+----+--------+|id|name|+----+--------+|1|小明||2|小虎||3|小花||4|小花||5|小花||6|小虎||7|bob||8|tom||9|lili|+----+--------+9rowsinset(0.00sec)
删除数据库zxl
mysql>dropdatabasezxl;QueryOK,1rowaffected(0.01sec)mysql>showdatabases;+--------------------+|Database|+--------------------+|information_schema||mysql||performance_schema||test|+--------------------+4rowsinset(0.00sec)
查看mysqldump备份的sql文件中的pos节点位置以及相应的二进制文件名
二进制文件以及pos节点如下:
-- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000060', MASTER_LOG_POS=120;
使用mysqlbiglog查看二进制文件,删除数据库zxl的at位置是778
[root@zxl-nginxdata]#mysqlbinlogmaster-bin.000060#at778#16012014:25:42serverid1end_log_pos867CRC320x014503a4Querythread_id=44exec_time=0error_code=0SETTIMESTAMP=1453271142;dropdatabasezxl
备份二进制日志位置
[root@zxl-nginx~]#mysqlbinlog--start-position=120--stop-position=778/usr/local/mysql/data/master-bin.000060>/tmp/big_log.sql
恢复数据库
[root@zxl-nginx~]#mysql-uroot-p
查看恢复的数据库
关闭二进制日志
mysql>setsessionsql_log_bin=0;QueryOK,0rowsaffected(0.00sec)mysql>showdatabases;+--------------------+|Database|+--------------------+|information_schema||mysql||performance_schema||test||zxl|+--------------------+5rowsinset(0.00sec)
查看恢复的表mysql>usezxl;ReadingtableinformationforcompletionoftableandcolumnnamesYoucanturnoffthisfeaturetogetaquickerstartupwith-ADatabasechangedmysql>showtables;+---------------+|Tables_in_zxl|+---------------+|users|+---------------+1rowinset(0.00sec)mysql>select*fromusers;+----+--------+|id|name|+----+--------+|1|小明||2|小虎||3|小花||4|小花||5|小花||6|小虎|+----+--------+6rowsinset(0.00sec)注:后增加的数据没有恢复过来
恢复二进制备份的big-log文件
[root@zxl-nginx~]#mysql-uroot-p
再次查看users表
mysql>select*fromusers;+----+--------+|id|name|+----+--------+|1|小明||2|小虎||3|小花||4|小花||5|小花||6|小虎||7|bob||8|tom||9|lili|+----+--------+9rowsinset(0.00sec)
开启big-log
mysql>setsessionsql_log_bin=1;QueryOK,0rowsaffected(0.00sec)
注:关闭不关闭二进制只有不进行任何操作即可不关闭,原因你懂的。
注:在实际恢复时最好编辑my.cnf配置文件,添加如下项:
skip-networking //跳过网络功能来恢复数据