mysql数据备份与恢复基础
2015-11-19 19:57
573 查看
mysqldump备份
备份所有库
备份db1数据库
备份单个表
一致性备份
如果不是使用一致性备份的话,mysql会锁表。如果使用会单独开启一个事务,不会锁表,锁库
远程备份
导出数据为csv格式
xtrabackup备份
全量备份
增量备份
流式备份
并行备份
限速备份
–throttle=10 表示10M/s的限速
压缩备份
mysqlbinlog
binlog恢复实验
备份所有库
[root@miles ~]# mysqldump -uroot -pbeijing --socket=/data/mysql/mysql.sock --all-databases>/home/mysql/backup/all_db.sql 查看备份 [root@miles backup]# pwd /home/mysql/backup [root@miles backup]# ls -lth total 920M -rw-r--r--. 1 root root 277M Nov 23 11:26 all_db.sql
备份db1数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 ... [root@miles ~]# mysqldump -uroot -pbeijing --socket=/data/mysql/mysql.sock --databases db1>/home/mysql/backup/db1_db.sql
备份单个表
[root@miles ~]# mysqldump -uroot -pbeijing --socket=/data/mysql/mysql.sock db1 test>/home/mysql/backup/db1_test.sql
一致性备份
如果不是使用一致性备份的话,mysql会锁表。如果使用会单独开启一个事务,不会锁表,锁库
[root@miles ~]# mysqldump --single-transaction -uroot -pbeijing --socket=/data/mysql/mysql.sock --all-databases>/home/mysql/backup/all_db_trans.sql
远程备份
[root@m1 ~]# mysqldump -uroot -pbeijing -h192.168.137.20 -P3306 --all-databases > ./backup/remote_backupall.sql -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly. mysqldump: Got error: 1142: SELECT,LOCK TABL command denied to user 'root'@'192.168.137.10' for table 'cond_instances' when using LOCK TABLES 报错,添加参数--skip-lock-tables或--single-transaction即可 [root@m1 backup]# mysqldump -uroot -pbeijing -h192.168.137.20 -P3306 --skip-lock-tables --all-databases > /root/backup/remote_backupall_skip.sql 或 [root@m1 ~]# mysqldump -uroot -pbeijing -h192.168.137.20 -P3306 --single-transaction --all-databases > ./backup/remote_backupall.sql
导出数据为csv格式
[root@miles ~]# mysqldump --single-transaction -uroot -pbeijing --socket=/data/mysql/mysql.sock db1 test -T /tmp 查看导出数据,默认以制表符分隔 [root@miles tmp]# more test.sql DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(10) DEFAULT NULL, `name` varchar(30) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; [root@miles tmp]# more test.txt 1 zhangsan1 2 zhangsan2 3 zhangsan3 [root@miles ~]# mysqldump --single-transaction -uroot -pbeijing --socket=/data/mysql/mysql.sock --fields-terminated-by=, db1 test -T /tmp 查看导出数据,参数--fields-terminated-by指定分隔符为"," [root@miles tmp]# more test.txt 1,zhangsan1 2,zhangsan2 3,zhangsan3
xtrabackup备份
全量备份
[root@miles ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=beijing --socket=/data/mysql/mysql.sock /home/mysql/backup ... 151119 11:52:35 Executing UNLOCK TABLES 151119 11:52:35 All tables unlocked 151119 11:52:35 Backup created in directory '/home/mysql/backup/2015-11-19_11-52-27' 151119 11:52:35 [00] Writing backup-my.cnf 151119 11:52:35 [00] ...done 151119 11:52:35 [00] Writing xtrabackup_info 151119 11:52:35 [00] ...done xtrabackup: Transaction log of lsn (832649524) to (832649524) was copied. 151119 11:52:35 completed OK! 查看备份信息 [root@miles 2015-11-20_11-34-42]# pwd /home/mysql/backup/2015-11-20_11-34-42 [root@miles 2015-11-20_11-34-42]# du -sh * 4.0K backup-my.cnf 73M db1 18M ibdata1 4.0K log 1.1M mysql 212K performance_schema 116K test 4.0K xtrabackup_checkpoints 4.0K xtrabackup_info 4.0K xtrabackup_logfile
增量备份
首先新增一些数据 mysql> use test; Database changed mysql> show tables; Empty set (0.00 sec) mysql> create table t(id int(10),name varchar(30)); Query OK, 0 rows affected (0.02 sec) mysql> insert into t values(1,'zhangsan'); Query OK, 1 row affected (0.00 sec) mysql> insert into t values(2,'zhangsan2'); Query OK, 1 row affected (0.01 sec) mysql> insert into t values(3,'zhangsan3'); Query OK, 1 row affected (0.00 sec) mysql> insert into t values(4,'zhangsan4'); Query OK, 1 row affected (0.01 sec) mysql> insert into t values(5,'zhangsan5'); Query OK, 1 row affected (0.01 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) 开始增量备份 --incremental 表明当前备份为增量备份 --incremental-dir 指定上次备份的路径 [root@miles 2015-11-19_11-52-27]#innobackupex --defaults-file=/etc/my.cnf --user=root --password=beijing --incremental /home/mysql/backup --incremental-basedir=/home/mysql/backup/2015-11-20_11-34-42 ... 151120 11:57:28 Executing UNLOCK TABLES 151120 11:57:28 All tables unlocked 151120 11:57:28 Backup created in directory '/home/mysql/backup/2015-11-20_11-57-24' 151120 11:57:28 [00] Writing backup-my.cnf 151120 11:57:28 [00] ...done 151120 11:57:28 [00] Writing xtrabackup_info 151120 11:57:28 [00] ...done xtrabackup: Transaction log of lsn (71789633) to (71789633) was copied. 151120 11:57:28 completed OK! 查看备份信息,可见增量备份 [root@miles backup]# du -sh * 92M 2015-11-20_11-34-42 2.0M 2015-11-20_11-57-24
流式备份
[root@miles mysql]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=beijing --stream=xbstream /home/mysql/backup/ > /home/mysql/backup/stream.bak 查看备份信息 [root@miles backup]# ls -lh ... -rw-r--r--. 1 root root 92M Nov 20 14:26 stream.bak
并行备份
[root@miles mysql]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=beijing --parallel=4 /home/mysql/backup/ ... 151120 15:44:20 Executing UNLOCK TABLES 151120 15:44:20 All tables unlocked 151120 15:44:20 Backup created in directory '/home/mysql/backup//2015-11-20_15-44-18' 151120 15:44:20 [00] Writing backup-my.cnf 151120 15:44:20 [00] ...done 151120 15:44:20 [00] Writing xtrabackup_info 151120 15:44:20 [00] ...done xtrabackup: Transaction log of lsn (71789633) to (71789633) was copied.
限速备份
–throttle=10 表示10M/s的限速
[root@miles tpcc]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=beijing --throttle=10 /home/mysql/backup/ 通过iosstat命令查看io状态 [root@miles backup]# iostat -dmx 1
压缩备份
[root@miles tpcc]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=beijing --compress --compress-threads 4 /home/mysql/backup/ 效果对比 [root@miles backup]# du -sh * 448M 2015-11-20_16-25-42 278M 2015-11-20_17-14-10
mysqlbinlog
[mysql@miles mysql]$ mysqlbinlog -vv binlog.000002 ... BEGIN /*!*/; # at 174 #151124 17:11:48 server id 1 end_log_pos 274 Query thread_id=1 exec_time=0 error_code=0 use `db1`/*!*/; SET TIMESTAMP=1448356308/*!*/; insert into test values(4,'zhangsan4') /*!*/; # at 274 #151124 17:11:48 server id 1 end_log_pos 301 Xid = 11 COMMIT/*!*/; # at 301 #151124 17:11:58 server id 1 end_log_pos 368 Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1448356318/*!*/; BEGIN /*!*/; # at 368 #151124 17:11:58 server id 1 end_log_pos 468 Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1448356318/*!*/; insert into test values(5,'zhangsan5') /*!*/; # at 468 #151124 17:11:58 server id 1 end_log_pos 495 Xid = 12 COMMIT/*!*/; # at 495 #151124 17:12:10 server id 1 end_log_pos 562 Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1448356330/*!*/; BEGIN /*!*/; # at 562 #151124 17:12:10 server id 1 end_log_pos 651 Query thread_id=1 exec_time=0 error_code=0 SET TIMESTAMP=1448356330/*!*/; delete from test where id=7 /*!*/; # at 651 #151124 17:12:10 server id 1 end_log_pos 678 Xid = 13 COMMIT/*!*/; ...
binlog恢复实验
查看当前binlog状态 mysql> show master status; +---------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+----------+--------------+------------------+ | binlog.000003 | 868 | | | +---------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 插入并提交实验数据 mysql> select * from test; Empty set (0.00 sec) mysql> insert into test values (1,'M1'),(2,'M2'),(3,'M3'),(4,'M4'),(5,'M5'),(6,'M6'),(7,'M7'),(8,'M8'),(9,'M9'),(10,'M10'); Query OK, 10 rows affected (0.01 sec) Records: 10 Duplicates: 0 Warnings: 0 mysql> select * from test; +------+------+ | id | name | +------+------+ | 1 | M1 | | 2 | M2 | | 3 | M3 | | 4 | M4 | | 5 | M5 | | 6 | M6 | | 7 | M7 | | 8 | M8 | | 9 | M9 | | 10 | M10 | +------+------+ 10 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.01 sec) mysql> show master status; +---------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+----------+--------------+------------------+ | binlog.000003 | 1139 | | | +---------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 删除实验数据 mysql> delete from test; Query OK, 10 rows affected (0.07 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) 恢复删除数据 [root@miles ~]# mysqlbinlog --start-position=868 --stop-position=1139 /data/mysql/binlog.000003 | mysql -uroot -pbeijing mysql> select * from test; +------+------+ | id | name | +------+------+ | 1 | M1 | | 2 | M2 | | 3 | M3 | | 4 | M4 | | 5 | M5 | | 6 | M6 | | 7 | M7 | | 8 | M8 | | 9 | M9 | | 10 | M10 | +------+------+ 10 rows in set (0.00 sec)
相关文章推荐
- Mysql 列转行统计查询 、行转列统计查询
- mysql日常操作命令
- 详解MySQL Server端如何发送结果集给客户端
- mysql null 转0
- wamp设置mysql编码格式
- 下载数据库mysql压缩包地址
- mysql开启慢查询方法
- mysql存储过程
- MySQL全文索引
- 修改MYSQL默认编码为UTF8
- mysql从库同步时出现Thread stack overrun
- ext上传文件到mysql上
- Lost connection to MySQL server at 'waiting for initial communication packet', system error: 0
- MySql基本命令
- mysql数据库 触发器简单实例
- mysql mysqldump只导出表结构或只导出数据的实现方法
- ubuntu mysql编码设置5.5以后
- navicat创建mysql 数据库存储过程
- mysql : utf8mb4 的问题
- Mysql常用命令(二)