版本:5.7.28
PHP:5.6
服务器:mysql 192.168.4.20
1.数据库备份与恢复
逻辑备份工具 mysqldump
使用mysql 恢复数据库
1.1备份MySQL服务器上的所有库
]# mysqldump -u root -p123456 --all-databases > /root/alldb.sql
]# file /root/alldb.sql //确认备份文件类型
alldb.sql: UTF-8 Unicode text, with very long lines
1.2只备份指定的某一个库
]# mysqldump -u root -p123456 userdb > userdb.sql
1.3 同时备份指定的多个库
]# mysqldump -u root -p123456 -B mysql userdb > mysql+test+userdb.sql
1.4使用mysql命令从备份中恢复数据库、表
创建名为userdb2的新库
mysql> CREATE DATABASE userdb2;
导入备份文件,在新库中重建表及数据
]# mysql -u root -p123456 userdb2 < /root/userdb.sql
前面备份过,现在恢复看是否有数据
确认新库正常,启用新库
mysql> USE userdb2;
mysql> SHOW TABLES;
2 使用binlog日志
利用binlog恢复库表,要求如下:
启用binlog日志
创建db1库tb1表,插入3条记录
删除tb1表中刚插入的3条记录
使用mysqlbinlog恢复删除的3条记录
2.1 启用binlog日志
]# vim /etc/my.cnf
[mysqld]
.. ..
server_id=20
log-bin=mysql20
binlog_format=STATEMENT
//在Mysql5.7中,binlog日志格式默认为ROW,但它不记录sql语句上下文相关信息。需要将binlog日志格式修改为STATEMENT
]# systemctl restart mysqld.service
]# ls /var/lib/mysql/mysql20*
/var/lib/mysql/mysql20.000001 /var/lib/mysql/mysql20.index
其中mysql20.index文件记录了当前保持的二进制文件列表
]# cat /var/lib/mysql/mysql20.index
./mysql20.000001
重启MySQL服务程序,或者执行SQL操作“FLUSH LOGS;”,会生成一份新的日志
]# ls /var/lib/mysql/mysql20*
/var/lib/mysql/mysql20.000001
/var/lib/mysql/mysql20.000002
/var/lib/mysql/mysql20.index
]# cat /var/lib/mysql/mysql20.index
./mysql20.000001
./mysql20.000002
2.2 利用binlog日志重做数据库操作
删除003以前所有日志
mysql> purge master logs to 'mysql20.000003';
删除所有日志
mysql> reset master;
创建db1.tb1,插入值
mysql> CREATE DATABASE db1;
mysql> USE db1;
mysql> CREATE TABLE tb1(
-> id int(10) NOT NULL,
-> name varchar(24)
-> );
mysql> INSERT INTO tb1 VALUES
-> (1,'AA'),
-> (2,'BB'),
-> (3,'CC');
mysql> SELECT * FROM tb1;
+----+------+
| id | name |
+----+------+
| 1 | AA |
| 2 | BB |
| 3 | CC |
+----+------+
删除tb1数据
mysql> DELETE FROM tb1;
mysql> SELECT * FROM tb1;
Empty set (0.00 sec)
2.3 通过binlog日志恢复表记录
查看mysql20.000001日志内容
]# mysqlbinlog /var/lib/mysql/mysql20.000001
# at 647
#200203 16:32:51 server id 20 end_log_pos 770 CRC32 0xbd6acf32 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1580718771/*!*/;
INSERT INTO tb1 VALUES
(1,'AA'),
(2,'BB'),
(3,'CC')
/*!*/;
# at 770
#200203 16:32:51 server id 20 end_log_pos 848 CRC32 0x7bdc4adf Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1580718771/*!*/;
COMMIT
/*!*/;
# at 848
]# mysqlbinlog \
> --start-position=647 \
> --stop-position=848 \
( --start-datetime="200203 16:32:51" \ )
( --stop-datetime="200203 16:32:51" \ )
> /var/lib/mysql/mysql20.000001 | mysql -u root -p1234567
查看,还原成功
mysql> SELECT * FROM tb1;
+----+------+
| id | name |
+----+------+
| 1 | AA |
| 2 | BB |
| 3 | CC |
+----+------+
3.innobackupex备份工具
安装percona软件包
innobackupex完整备份、增量备份操作。
恢复数据
3.1 安装XtraBackup软件包
]# yum -y install libev-4.15-1.el6.rf.x86_64.rpm
]# yum -y install perl-DBD-MySQL perl-Digest-MD5
]# wget https://repo.percona.com/centos/7/RPMS/x86_64/percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm
]# yum -y percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm
3.2 innobackupex完整备份、增量备份操作
--host 主机名
--port 3306
--user 用户名
--password 密码
--databases="库名"
--databases="库1 库2"
--databases="库.表"
--no-timestamp 不用日期命名备份文件存储的子目录,使用备份的数据库名做备份目录名
--no-timestmap 不使用日期命名备份目录名
]# innobackupex --user=root --password=1234567 /backup --no-timestamp
...
19:38:53 completed OK!
]# du -sh /backup
27M/backup
]# ls /backup/
backup-my.cnf mysql test xtrabackup_checkpoints
db1 performance_schema userdb xtrabackup_info
ib_buffer_pool qqq userdb2 xtrabackup_logfile
ibdata1 sys xtrabackup_binlog_info
3.3 做一个增量备份
mysql> USE db1;
mysql> CREATE TABLE mytb(id int(4), name varchar(24));
mysql> INSERT INTO mytb VALUES \
-> (1,'bon'), \
-> (2,'bo');
mysql> SELECT * FROM mytb;
+------+------+
| id | name |
+------+------+
| 1 | bon |
| 2 | bo |
+------+------+
以前一次保存到/backup的完整备份为基础,做一个增量备份,保存到/incr01/,
指定增量备份参照的基本目录(完整备份目录)需要用到选项--incremental-basedir
]# innobackupex --user=root --password=1234567 --incremental /incr01 --incremental-basedir=/backup --no-timestamp
]# du -sh /incr01
3.9M/incr01
3.3 再做一个增量备份
mysql> CREATE TABLE mytb2(id int(4), name varchar(24));
mysql> INSERT INTO mytb2 VALUES \
-> (1,'aa'),
-> (2,'bb');
mysql> SELECT * FROM mytb2;
+------+------+
| id | name |
+------+------+
| 1 | aa |
| 2 | bb |
+------+------+
]# innobackupex --user=root --password=1234567 --incremental /incr02 --incremental-basedir=/incr01 --no-timestamp
]# du -sh /incr02
4.1M/incr02
3.4 恢复数据
mysql> DROP DATABASE db1;
]# systemctl stop mysqld.service
]# cat /backup/xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 2994642
last_lsn = 2994651
compact = 0
recover_binlog_info = 0
准备恢复“完整备份”
完成准备以后,最终/backup可用来重建MySQL服务器。
这种情况下,需要先做一个“--apply-log --redo-only ”的准备操作,以确保数据一致性:
]# innobackupex --user=root --password=1234567 --apply-log --redo-only /backup/
]# cat /backup/xtrabackup_checkpoints
backup_type = log-applied
from_lsn = 0
to_lsn = 2994642
last_lsn = 2994651
compact = 0
recover_binlog_info = 0
合并/incr01
]# innobackupex --user=root --password=1234567 --apply-log --redo-only /backup/ --incremental-dir=/incr01
]# cat /backup/xtrabackup_checkpoints
backup_type = log-applied
from_lsn = 0
to_lsn = 3000744
last_lsn = 3000753
compact = 0
recover_binlog_info = 0
合并/incr02
]# innobackupex --user=root --password=1234567 --apply-log --redo-only /backup/ --incremental-dir=/incr02
]# cat /backup/xtrabackup_checkpoints
backup_type = log-applied
from_lsn = 0
to_lsn = 3011372
last_lsn = 3011381
compact = 0
recover_binlog_info = 0
]# cat /incr02/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 3000744
to_lsn = 3011372
last_lsn = 3011381
compact = 0
recover_binlog_info = 0
删除所有数据
]# rm -rf /var/lib/mysql/*
恢复所有数据
]# innobackupex --copy-back /backup
]# ls -ld /var/lib/mysql/*
drwxr-x--- 2 root root 116 2月 13 20:37 /var/lib/mysql/db1
-rw-r----- 1 root root 480 2月 13 20:37 /var/lib/mysql/ib_buffer_pool
-rw-r----- 1 root root 12582912 2月 13 20:37 /var/lib/mysql/ibdata1
...
]# chown -R mysql:mysql /var/lib/mysql
]# ls -ld /var/lib/mysql/*
drwxr-x--- 2 mysql mysql 116 2月 13 20:37 /var/lib/mysql/db1
-rw-r----- 1 mysql mysql 480 2月 13 20:37 /var/lib/mysql/ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 2月 13 20:37 /var/lib/mysql/ibdata1
...
查看
]# systemctl start mysqld.service
]# mysql -uroot -p1234567 -e "select * from db1.mytb2"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+------+
| id | name |
+------+------+
| 1 | aa |
| 2 | bb |
+------+------+
相关文章
- 01-05MySQL innobackupex 全备+增量备份+还原+增量还原
- 01-0527_mysqldump 实时增量备份 、innobackupex
- 01-05innobackupex做MySQL增量备份及恢复
- 01-05innobackupex全量备份和增量备份脚本
- 01-05innobackupex全量备份和增量备份脚本
- 01-05使用innobackupex在线增量备份及恢复mysql数据库
- 01-05阿里云独门绝技之无代理混合云数据库实时增量备份
- 01-05innobackupex在线备份及恢复(全量和增量)
- 01-05mysql之 Innobackupex(全备+增量)备份恢复
- 01-05rsync实时备份增量数据