查看sql_log_bin是否啓用mysql
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;開啓二進制日誌sql
修改配置文件
指定二進制日誌的路徑(二進制日誌文件不該該與數據文件放在同一磁盤)數據庫
[root@CentOS7 ~]#vim /etc/my.cnf [mysqld] log_bin=/data/mysql/bin/mysql-bin
重啓數據庫服務vim
[root@CentOS7 ~]#systemctl restart mariadb
建立備份數據存放路徑(最好存放在遠程主機多份)ide
[root@CentOS7 ~]#mkdir /data/backup
使用備份工具mysqldump工具
[root@CentOS7 ~]#mysqldump -A --single-transaction --master-data=2 | xz > /data/backup/all_`date +%F`.sql.xz [root@CentOS7 ~]#ls -l /data/backup/ total 104 -rw-r--r-- 1 root root 105844 May 6 19:40 all_2019-05-06.sql.xz
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表rest
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)
[root@CentOS7 ~]#rm -rf /var/lib/mysql/*
查看二進制文件列表code
[root@CentOS7 ~]#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
重啓數據庫
[root@CentOS7 ~]#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)
使用備份文件還原數據庫
解壓
[root@CentOS7 ~]#ls /data/backup/all_2019-05-06.sql.xz /data/backup/all_2019-05-06.sql.xz [root@CentOS7 ~]#xz -d /data/backup/all_2019-05-06.sql.xz [root@CentOS7 ~]#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
[root@CentOS7 ~]#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文件裏
[root@CentOS7 ~]#mysqlbinlog --start-position=245 /data/mysql/bin/mysql-bin.000005 > inc.sql
因爲以前誤刪除了students表,因此要刪除或註釋inc.sql裏面的刪除students表操做的語句
[root@CentOS7 ~]#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;