一、备份单个表
语法:mysqldump -u 用户名 -p 数据库名 表名 >备份的文件名
mysqldump -u root -pdubin oldboy test >/opt/table.sql
执行结果:
[root@localhost ~]# mysqldump -u root -pdubin oldboy test >/opt/table.sql [root@localhost ~]# egrep -v "#|*|--|^$" /opt/table.sql DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; LOCK TABLES `test` WRITE; INSERT INTO `test` VALUES (1,'oldboy'),(2,'oldgirl'),(3,'inca'),(4,'zuma'),(5,'kaka'); UNLOCK TABLES;
二、备份多个表
语法:mysqldump -u 用户名 -p 数据库名 表名1 表名2 >备份的文件名
[root@localhost ~]# mysqldump -u root -pdubin oldboy test student>/opt/table.sql [root@localhost ~]# egrep -v "#|*|--|^$" /opt/table.sql DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; LOCK TABLES `test` WRITE; INSERT INTO `test` VALUES (1,'oldboy'),(2,'oldgirl'),(3,'inca'),(4,'zuma'),(5,'kaka'); UNLOCK TABLES; DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uni_inde_name` (`name`), KEY `index_dept` (`dept`(8)), KEY `ind_name_dept` (`name`(8),`dept`(10)) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; LOCK TABLES `student` WRITE; UNLOCK TABLES;
分表备份缺点:文件多,碎。
1、备一个完整全备,再做一个分库分表备份。
2、脚本批量服务多个SQL文件。
三、备份表结构 -d
[root@localhost ~]# mysqldump -u root -pdubin oldboy test -d >/opt/table.sql [root@localhost ~]# egrep -v "#|*|--|^$" /opt/table.sql DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
四、备份数据 -t(只导出数据)
[root@localhost ~]# mysqldump -u root -pdubin oldboy test -t --compact INSERT INTO `test` VALUES (1,'oldboy'),(2,'oldgirl'),(3,'inca'),(4,'zuma'),(5,'kaka');
五、刷新binlog参数
-F --master-data [root@localhost ~]# mysqldump -uroot -pdubin --master-data=1 --compact oldboy CHANGE MASTER TO MASTER_LOG_FILE='mysqlbin_oldboy.000002', MASTER_LOG_POS=14729;
记录当时的文件大小
[root@localhost ~]# mysqlbinlog /application/mysql/data/mysqlbin_oldboy.000001 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #190920 22:12:42 server id 1 end_log_pos 107 Start: binlog v 4, server v 5.5.32-log created 190920 22:12:42 at startup ROLLBACK/*!*/; BINLOG ' Wt6EXQ8BAAAAZwAAAGsAAAAAAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAABa3oRdEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA== '/*!*/; # at 107 #190920 22:13:02 server id 1 end_log_pos 177 Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1568988782/*!*/; SET @@session.pseudo_thread_id=1/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=0/*!*/; 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=33/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; BEGIN /*!*/;
--mater-data=2 备份的记录提示前加注释,恢复的时候不执行该条语句 [root@localhost ~]# mysqldump -uroot -pdubin --master-data=2 --compact oldboy -- CHANGE MASTER TO MASTER_LOG_FILE='mysqlbin_oldboy.000002', MASTER_LOG_POS=14729;