mysqldump备份
备份所有库
[root@miles ~]
查看备份
[root@miles backup]
/home/mysql/backup
[root@miles backup]
total 920M
-rw-r--r--. 1 root root 277M Nov 23 11:26 all_db.sql
备份db1数据库
| information_schema |
| db1
...
[root@miles ~]# mysqldump -uroot -pbeijing --socket=/data/mysql/mysql.sock --databases db1>/home/mysql/backup/db1_db.sql
备份单个表
[root@miles ~]
一致性备份
如果不是使用一致性备份的话,mysql会锁表。如果使用会单独开启一个事务,不会锁表,锁库
[ ] --- - - --. --->.
远程备份
[root@m1 ~]# mysqldump -uroot -pbeijing -h192.168.137.20 -P3306
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
查看导出数据,默认以制表符分隔
[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
查看导出数据,参数
[root@miles tmp]# more test.txt
1,zhangsan1
2,zhangsan2
3,zhangsan3
xtrabackup备份
全量备份
[root@miles ~]
...
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]
/home/mysql/backup/2015-11-20_11-34-42
[root@miles 2015-11-20_11-34-42]
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]
...
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]
92M 2015-11-20_11-34-42
2.0M 2015-11-20_11-57-24
流式备份
[root@miles mysql]
查看备份信息
[root@miles backup]
...
-rw-r--r--. 1 root root 92M Nov 20 14:26 stream.bak
并行备份
[root@miles mysql]
...
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的限速
[ ] ---. -- -- --
[ ] -
压缩备份
[ ] ---. -- -- -- ---
[ ] -
----
----
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状态
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
| 1 | M1 |
| 2 | M2 |
| 3 | M3 |
| 4 | M4 |
| 5 | M5 |
| 6 | M6 |
| 7 | M7 |
| 8 | M8 |
| 9 | M9 |
10 rows in set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.01 sec)
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
| 1 | M1 |
| 2 | M2 |
| 3 | M3 |
| 4 | M4 |
| 5 | M5 |
| 6 | M6 |
| 7 | M7 |
| 8 | M8 |
| 9 | M9 |
10 rows in set (0.00 sec)