mysqldump备份及数据还原
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysqldump备份及数据还原相关的知识,希望对你有一定的参考价值。
mysqldump建议使用在10g以下的数据进行备份,是mysql数据库的逻辑备份
mysqldump 指令介绍 -A 所有表 —all-databases -B --databases db1 db2 ...: 备份指定的多个库,同时创建库(不能跟A同时使用) -x 锁表 --lock-all-tables 请求锁定所有表之后再备份,对MyISAM、InnoDB、Aria做温备 --single-transaction: 能够对InnoDB存储引擎实现热备;(要确定表是innodb 跟锁表不能同时使用) --flush-logs: 备份前、请求到锁之后滚动日志; 恢复时候用数据加滚动后的二进制日志 复制时的同步位置标记: --master-data=[0|1|2] (0: 不记录 )( 1:记录为CHANGE MASTER语句)( 2:记录为注释的CHANGE MASTER语句) 备份代码: --events: 备份事件调度器代码 --routines: 备份存储过程和存储函数 --triggers:备份触发器 建议:关闭二进制日志,关闭其它用户连接;
手动锁表备份:
mysql> flush tables with read lock; mysql> flush logs; mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000004 | 120 | | | | +-------------------+----------+--------------+------------------+-------------------+ #mysql> insert into t3 (id,data) values(2,‘22‘); #ERROR 1223 (HY000): Can‘t execute the query because you have a conflicting read lock [[email protected] ~]# mysqldump -hlocalhost -uroot -p -B martin > a.sql mysql> unlock tables;
自动备份:
[[email protected] ~]# mysqldump -hlocalhost -uroot -p -B martin -x --flush-logs --master-data=2 > a.sql [[email protected] ~]# cat a.sql -- CHANGE MASTER TO MASTER_LOG_FILE=‘master-bin.000005‘, MASTER_LOG_POS=120; -- -- Current Database: `martin` -- CREATE DATABASE /*!32312 IF NOT EXISTS*/ `martin` /*!40100 DEFAULT CHARACTER SET latin1 */; USE `martin`; mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000005 | 120 | | | | +-------------------+----------+--------------+------------------+-------------------+
还原:
模拟备份之后添加数据:
mysql> CREATE TABLE IF NOT EXISTS `t1` ( -> `id` varchar(32) NOT NULL, -> `data` text NOT NULL , -> `expire` int(11) NOT NULL DEFAULT ‘0‘, -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.00 sec) mysql> insert into t1 (id,data) values(2,‘22‘); Query OK, 1 row affected (0.00 sec) mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000005 | 927 | | | | +-------------------+----------+--------------+------------------+-------------------+
数据丢失
mysql> drop database martin; Query OK, 2 rows affected (0.00 sec) [[email protected] local]# service mysqld stop Shutting down MySQL.. SUCCESS!
查看数据:
[[email protected] local]# cd /mydata/mysql/log-bin/ [[email protected] log-bin]# mysqlbinlog --start-position=120 master-bin.000005 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #160524 15:29:25 server id 1 end_log_pos 120 CRC32 0xa1962627 Start: binlog v 4, server v 5.6.30-log created 160524 15:29:25 BINLOG ‘ 1QJEVw8BAAAAdAAAAHgAAAAAAAQANS42LjMwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAScm lqE= ‘/*!*/; # at 120 #160524 15:32:29 server id 1 end_log_pos 249 CRC32 0x74e33ea9 Query thread_id=2 exec_time=0 error_code=0 use `martin`/*!*/; SET TIMESTAMP=1464075149/*!*/; SET @@session.pseudo_thread_id=2/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=1075838976/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C latin1 *//*!*/; SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; DROP TABLE IF EXISTS `t1` /* generated by server */ /*!*/; # at 249 #160524 15:32:40 server id 1 end_log_pos 378 CRC32 0x7ae6077a Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1464075160/*!*/; DROP TABLE IF EXISTS `t1` /* generated by server */ /*!*/; # at 378 #160524 15:32:40 server id 1 end_log_pos 643 CRC32 0x10173878 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1464075160/*!*/; CREATE TABLE IF NOT EXISTS `t1` ( `id` varchar(32) NOT NULL, `data` text NOT NULL , `expire` int(11) NOT NULL DEFAULT ‘0‘, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 /*!*/; # at 643 #160524 15:32:54 server id 1 end_log_pos 726 CRC32 0xad187d7d Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1464075174/*!*/; BEGIN /*!*/; # at 726 #160524 15:32:54 server id 1 end_log_pos 843 CRC32 0x861ba6b0 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1464075174/*!*/; insert into t1 (id,data) values(2,‘22‘) /*!*/; # at 843 #160524 15:32:54 server id 1 end_log_pos 927 CRC32 0xa1a5bf4a Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1464075174/*!*/; COMMIT /*!*/; # at 927 #160524 15:35:30 server id 1 end_log_pos 1025 CRC32 0x77d2ec0e Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1464075330/*!*/; drop database martin /*!*/; # at 1025 #160524 15:37:18 server id 1 end_log_pos 1048 CRC32 0xb1a3e339 Stop DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET [email protected]_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
找到 at 927 恢复数据到drop之前
[[email protected] log-bin]# mysqlbinlog --start-position=120 --stop-position=927 master-bin.000005>~/a.bin.sql
连接mysql:唯一连入保证数据唯一:
mysql> set session sql_log_bin=0; mysql> source ~/a.sql mysql> source ~/a.bin.sql mysql> set session sql_log_bin=1; mysql> show tables; +------------------+ | Tables_in_martin | +------------------+ | t1 | | t3 | +------------------+
over
以上是关于mysqldump备份及数据还原的主要内容,如果未能解决你的问题,请参考以下文章
MYSQL数据库备份还原,并还原到最新状态(mysqldump)