[root@bogon ~]# mysql -uroot -p123
CREATE DATABASE client;
USE client;
CREATE TABLE user_info(
IdentityCard CHAR(20) NOT NULL,
name CHAR(20) NOT NULL,
gender CHAR(4),
UserID CHAR(10) NOT NULL,
expenses INT(10));
INSERT INTO user_info VALUES('000006','张三','男','016','10');
INSERT INTO user_info VALUES('000007','李四','女','017','91');
INSERT INTO user_info VALUES('000008','王五','女','018','23');
SELECT * FROM user_info;
mysql> exit
Bye
先进行一次完全备份
[root@bogon ~]# mkdir /mysql_bak[root@bogon ~]# mysqldump -uroot -p -A > /mysql_bak/mysql-all.sql
Enter password:# 刷新日志[root@bogon ~]# mysqladmin -uroot -p flush-logs[root@bogon ~]# ls /usr/local/mysql/data/mysql-bin.*/usr/local/mysql/data/mysql-bin.000001/usr/local/mysql/data/mysql-bin.index
/usr/local/mysql/data/mysql-bin.000002
继续录入新的数据并进行增量备份
USE client;
INSERT INTO user_info VALUES('000009','赵六','男','019','37');
INSERT INTO user_info VALUES('000010','孙七','男','020','36');# 停顿几秒之后再插入
INSERT INTO user_info VALUES('000011','张三','男','021','35');
INSERT INTO user_info VALUES('000012','李四','男','022','37');# 刷新日志[root@bogon ~]# mysqladmin -uroot -p flush-logs
Enter password:[root@bogon ~]# ls /usr/local/mysql/data/mysql-bin.*/usr/local/mysql/data/mysql-bin.000001/usr/local/mysql/data/mysql-bin.000003/usr/local/mysql/data/mysql-bin.000002/usr/local/mysql/data/mysql-bin.index
# 备份[root@bogon ~]# cp /usr/local/mysql/data/mysql-bin.000002 /mysql_bak/[root@bogon ~]# ls /mysql_bak/
client_userinfo-2024-07-29.sql mysql-all.sql mysql-bin.000002[root@bogon ~]# ls /mysql_bak/
client_userinfo-2024-07-29.sql mysql-all.sql mysql-bin.000002# 查看[root@bogon ~]# mysqlbinlog --no-defaults /mysql_bak/mysql-bin.000002
模拟误操作
[root@bogon ~]# mysql -uroot -p -e 'DROP database client;'
Enter password:[root@bogon ~]# mysql -uroot -p -e 'show databases;'
Enter password:+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+
恢复操作
[root@bogon ~]# mysql -uroot -p < /mysql_bak/mysql-all.sql
Enter password:[root@bogon ~]# mysql -uroot -p -e 'show databases;'
Enter password:+--------------------+| Database |+--------------------+| information_schema || client || mysql || performance_schema || sys |+--------------------+
2.基于位置恢复
# 把mysql-bin.000002的内容全部恢复
mysqlbinlog --no-defaults /mysql_bak/mysql-bin.000002| mysql -uroot -p
--start-position
--stop-position
[root@bogon ~]# mysql -uroot -p -e 'select * from client.user_info;'
Enter password:+--------------+--------+--------+--------+----------+| IdentityCard | name | gender | UserID | expenses |+--------------+--------+--------+--------+----------+|000006| 张三 | 男 |016|10||000007| 李四 | 女 |017|91||000008| 王五 | 女 |018|23|+--------------+--------+--------+--------+----------+# 恢复 孙七 之前的内容 基于位置 623[root@bogon ~]# mysqlbinlog --no-defaults --stop-position='623' /mysql_bak/mysql-bin.000002 | mysql -uroot -p
Enter password:[root@bogon ~]# mysql -uroot -p -e 'select * from client.user_info;'
Enter password:+--------------+--------+--------+--------+----------+| IdentityCard | name | gender | UserID | expenses |+--------------+--------+--------+--------+----------+|000006| 张三 | 男 |016|10||000007| 李四 | 女 |017|91||000008| 王五 | 女 |018|23||000009| 赵六 | 男 |019|37|+--------------+--------+--------+--------+----------+# 恢复 孙七 之后的内容 基于位置 623[root@bogon ~]# mysqlbinlog --no-defaults --start-position='623' /mysql_bak/mysql-bin.000002 | mysql -uroot -p
Enter password:[root@bogon ~]# mysql -uroot -p -e 'select * from client.user_info;'
Enter password:+--------------+--------+--------+--------+----------+| IdentityCard | name | gender | UserID | expenses |+--------------+--------+--------+--------+----------+|000006| 张三 | 男 |016|10||000007| 李四 | 女 |017|91||000008| 王五 | 女 |018|23||000009| 赵六 | 男 |019|37||000010| 孙七 | 男 |020|36||000011| 张三 | 男 |021|35||000012| 李四 | 男 |022|37|+--------------+--------+--------+--------+----------+
3.基于时间点恢复
--start-datetime
--stop-datetime
mysqlbinlog --no-defaults --start-datetime='2024-07-28 22:13:33'/mysql_bak/mysql-bin.000002| mysql -uroot -p
mysqlbinlog --no-defaults --stop-datetime='2024-07-28 22:13:33'/mysql_bak/mysql-bin.000002| mysql -uroot -p
[root@bogon ~]# mysql -uroot -p -e 'select * from client.user_info;'
Enter password:+--------------+--------+--------+--------+----------+| IdentityCard | name | gender | UserID | expenses |+--------------+--------+--------+--------+----------+|000006| 张三 | 男 |016|10||000007| 李四 | 女 |017|91||000008| 王五 | 女 |018|23|+--------------+--------+--------+--------+----------+# 恢复 孙七 之前的内容 基于时间 2024-07-29 10:13:20[root@bogon ~]# mysqlbinlog --no-defaults --stop-datetime='2024-07-29 10:13:20' /mysql_bak/mysql-bin.000002 | mysql -uroot -p
Enter password:[root@bogon ~]# mysql -uroot -p -e 'select * from client.user_info;'
Enter password:+--------------+--------+--------+--------+----------+| IdentityCard | name | gender | UserID | expenses |+--------------+--------+--------+--------+----------+|000006| 张三 | 男 |016|10||000007| 李四 | 女 |017|91||000008| 王五 | 女 |018|23||000009| 赵六 | 男 |019|37|+--------------+--------+--------+--------+----------+# 恢复 孙七 之后的内容 基于时间 2024-07-29 10:13:20[root@bogon ~]# mysqlbinlog --no-defaults --start-datetime='2024-07-29 10:13:20' /mysql_bak/mysql-bin.000002 | mysql -uroot -p
Enter password:[root@bogon ~]# mysql -uroot -p -e 'select * from client.user_info;'
Enter password:+--------------+--------+--------+--------+----------+| IdentityCard | name | gender | UserID | expenses |+--------------+--------+--------+--------+----------+|000006| 张三 | 男 |016|10||000007| 李四 | 女 |017|91||000008| 王五 | 女 |018|23||000009| 赵六 | 男 |019|37||000010| 孙七 | 男 |020|36||000011| 张三 | 男 |021|35||000012| 李四 | 男 |022|37|+--------------+--------+--------+--------+----------+