1>启动数据库二进制日志
查看sql_log_bin是否启用
MariaDB [(none)]> show variables like ‘sql_log_bin‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
在会话时使用set sql_log_bin=ON;开启二进制日志
修改配置文件
指定二进制日志的路径(二进制日志文件不应该与数据文件放在同一磁盘)
[[email protected] ~]#vim /etc/my.cnf
[mysqld]
log_bin=/data/mysql/bin/mysql-bin
重启数据库服务
[[email protected] ~]#systemctl restart mariadb
2>完全备份
创建备份数据存放路径(最好存放在远程主机多份)
[[email protected] ~]#mkdir /data/backup
使用备份工具mysqldump
[[email protected] ~]#mysqldump -A --single-transaction --master-data=2 | xz > /data/backup/all_`date +%F`.sql.xz
[[email protected] ~]#ls -l /data/backup/
total 104
-rw-r--r-- 1 root root 105844 May 6 19:40 all_2019-05-06.sql.xz
3>修改数据库
Teachers表插入两条数据
MariaDB [hellodb]> insert teachers (name,age,gender)value(‘zhangshan‘,42,‘M‘);
Query OK, 1 row affected (0.01 sec)
MariaDB [hellodb]> insert teachers (name,age,gender)value(‘lisi‘,33,‘F‘);
Query OK, 1 row affected (0.00 sec)
删除students表
MariaDB [hellodb]> drop table students;
Query OK, 0 rows affected (0.01 sec)
Teachers表插入一条数据
MariaDB [hellodb]> insert teachers (name,age,gender)value(‘wangwu‘,33,‘F‘);
Query OK, 1 row affected (0.01 sec)
4>破换数据库
[[email protected] ~]#rm -rf /var/lib/mysql/*
5>还原
查看二进制文件列表
[[email protected] ~]#ll /data/mysql/bin/
total 1072
-rw-rw---- 1 mysql mysql 288 May 6 18:51 mysql-bin.000002
-rw-rw---- 1 mysql mysql 288 May 6 18:52 mysql-bin.000003
-rw-rw---- 1 mysql mysql 264 May 6 19:10 mysql-bin.000004
-rw-rw---- 1 mysql mysql 1119 May 6 20:59 mysql-bin.000005
-rw-rw---- 1 mysql mysql 30373 May 6 20:59 mysql-bin.000006
-rw-rw---- 1 mysql mysql 1038814 May 6 20:59 mysql-bin.000007
-rw-rw---- 1 mysql mysql 245 May 6 20:59 mysql-bin.000008
-rw-rw---- 1 mysql mysql 231 May 6 20:59 mysql-bin.index
重启数据库
[[email protected] ~]#systemctl restart mariadb
临时关闭二进制日志记录
MariaDB [(none)]> show variables like ‘sql_log_bin‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> set sql_log_bin=OFF;
Query OK, 0 rows affected (0.00 sec)
使用备份文件还原数据库
解压
[[email protected] ~]#ls /data/backup/all_2019-05-06.sql.xz
/data/backup/all_2019-05-06.sql.xz
[[email protected] ~]#xz -d /data/backup/all_2019-05-06.sql.xz
[[email protected] ~]#ll /data/backup/
total 512
-rw-r--r-- 1 root root 521990 May 6 19:40 all_2019-05-06.sql
还原
MariaDB [test]> source /data/backup/all_2019-05-06.sql
查看备份时二进制日志的记录点,从此位置后的日志导出到inc.sql
[[email protected] ~]#vim /data/backup/all_2019-05-06.sql
-- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000005‘, MASTER_LOG_POS=245;
//此条语句记录了数据库完全备份时二进制日志的记录点
将二进制日志mysql-bin.000005的245之后的日志导出到inc.sql文件里
[[email protected] ~]#mysqlbinlog --start-position=245 /data/mysql/bin/mysql-bin.000005 > inc.sql
由于之前误删除了students表,所以要删除或注释inc.sql里面的删除students表操作的语句
[[email protected] ~]#vim inc.sql
#DROP TABLE `students` /* generated by server */
使用二进制日志恢复完全备份后的数据更新
MariaDB [hellodb]> source inc.sql
开启二进制日志
MariaDB [hellodb]> set sql_log_bin=ON;
最后测试是否还原了数据库
MariaDB [hellodb]> show tables;
MariaDB [hellodb]> select * from teachers;
原文地址:https://blog.51cto.com/14233815/2390073
时间: 2024-10-06 00:10:49