mysql数据备份与恢复基础

原创 2015年11月19日 19:57:38

mysqldump备份
备份所有库

[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)
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

php源码,MySQL数据备份恢复

  • 2016年12月31日 01:38
  • 7KB
  • 下载

Mysql数据备份与恢复

备份/恢复策略: 1. 要定期做 mysql备份,并考虑系统可以承受的恢复时间。 2. 确保 mysql 打开log-bin,有了 binarylog,mysql才可以在必要的时候做完整恢复,或基...

MySQL数据备份与恢复

备份/恢复策略: 1. 要定期做 mysql备份,并考虑系统可以承受的恢复时间。 2. 确保 mysql 打开log-bin,有了 binarylog,mysql才可以在必要的时候做完整恢复,或基于时...

MySQL数据备份与恢复

MySQL命令行导出数据库   MySQL命令行导出数据库: 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Fi...

linux下mysql数据备份 恢复

比如我们要备份mysql中已经存在的名为linux的数据库,要用到命令mysqldump  命令格式如下:  [root@linuxsir01 root]# mysqldump -u root -...

mysql数据备份和恢复

mysqlhotcopy yum install perl-DBD-MySQL perl-DBI perl perl-DBD-MySQL [root@localhost database]# wg...

结合mysqlbinlog与mysqldump进行MySQL数据备份与恢复(适用于较小的数据量)

通过mysqlbinlog工具备份二进制日志文件 mysqlbinlog  --read-from-remote-server --host=192.168.83.37 --port=559...

MySql数据备份常用命令集

  • 2012年09月13日 14:41
  • 4KB
  • 下载

MySQL数据备份多种方法

  • 2012年08月11日 14:31
  • 45KB
  • 下载

mysql数据备份与还原

1、使用mysqldump命令备份  mysqldump命令将数据库中的数据备份成一个文本文件。表的结构和表中的数据将存储在生成的文本文件中。  mysqldump命令的工作原理很简单。它先查出需要备...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:mysql数据备份与恢复基础
举报原因:
原因补充:

(最多只允许输入30个字)