檢視資料庫資訊:
備份資料庫:
[root@localhost ~]# mysqldump -u root -p t > /mnt/t。sqlEnter password: [root@localhost ~]# ll /mnt/t。sql -rw-r——r—— 1 root root 1771 Aug 25 11:56 /mnt/t。sql
2 開啟bin_log功能
首先檢視資料庫是否開啟bin_log功能
mysql> show variables like “%log_bin%”;
需要修改mysql的配置檔案,/etc/的my。cnf,新增一句log_bin = mysql_bin即可
3 模擬誤操作(插入3條資料,刪除資料庫)
mysql> insert into t1 values (3);Query OK, 1 row affected (0。00 sec)mysql> insert into t1 values (4);Query OK, 1 row affected (0。00 sec)mysql> insert into t1 values (5);Query OK, 1 row affected (0。00 sec)mysql> select * from t1;+————+| id |+————+| 1 || 2 || 5 || 4 || 3 |+————+5 rows in set (0。00 sec)mysql> flush logs;Query OK, 0 rows affected (0。00 sec)mysql> show master status;+——————————+——————+————————+——————————+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+——————————+——————+————————+——————————+| mysql_bin。000003 | 106 | | |+——————————+——————+————————+——————————+1 row in set (0。00 sec)
刪除資料:
mysql> truncate t1;Query OK, 0 rows affected (0。00 sec)mysql> select * from t1;Empty set (0。00 sec)
此時突然資料庫損壞或者人為刪除
mysql> drop table t1;Query OK, 0 rows affected (0。00 sec)mysql> show tables;Empty set (0。00 sec)
4 資料恢復
1 用已經備份的/mnt/t。sql來恢復資料
mysql> source /mnt/t。sql;Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。01 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 2 rows affected (0。00 sec)Records: 2 Duplicates: 0 Warnings: 0Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)Query OK, 0 rows affected (0。00 sec)mysql> show tables;+——————-+| Tables_in_t |+——————-+| t1 |+——————-+1 row in set (0。00 sec)mysql> select * from t1;+————+| id |+————+| 1 || 2 |+————+2 rows in set (0。00 sec)
2 還有三條資料沒有恢復,怎麼辦。只能用bin-log來恢復
[root@localhost ~]# mysqlbinlog ——no-defaults /var/lib/mysql/mysql_bin。000002 | mysql -u root -p123。com t
mysql> use t;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from t1;+————+| id |+————+| 1 || 2 || 3 || 4 || 5 |+————+5 rows in set (0。00 sec)mysql>
5 總結
備份資料
mysqldump -uroot -p123456 test -l -F ‘/tmp/test。sql’-l:讀鎖(只能讀取,不能更新)-F:即flush logs,可以重新生成新的日誌檔案,當然包括log-bin日誌
檢視binlog日誌
mysql>show master status;
匯入之前備份資料
mysql -uroot -p t -v -f
恢復binlog-file二進位制日誌檔案
mysqlbinlog ——no-defaults binlog-file | mysql -uroot -p t
從某一(367)點開始恢復
mysqlbinlog ——no-defaults ——stop-position=“367” mysql-bin。000001| mysql -uroot -p t
先查好那一點,用more來檢視
[root@localhost mysql]# /usr/bin/mysqlbinlog ——no-defaults mysql-bin。000002 ——start-position=“794” ——stop-position=“1055” | more
然後恢復
[root@localhost mysql]# /usr/bin/mysqlbinlog ——no-defaults mysql-bin。000002 ——start-position=“794” ——stop-position=“1055” | /usr/bin/mysql -uroot -p t
重置binlog日誌
mysql> reset master;Query OK, 0 rows affected (0。01 sec)mysql> show master status;+——————————+——————+————————+——————————+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+——————————+——————+————————+——————————+| mysql-bin。000001 | 106 | | |+——————————+——————+————————+——————————+
mysql> flush logs;#關閉當前的二進位制日誌檔案並建立一個新檔案,新的二進位制日誌檔案的名字在當前的二進位制檔案的編號上加1。