詳解MySQL資料庫恢複操作

來源:互聯網
上載者:User

詳解MySQL資料庫教程恢複操作

binlog是通過記錄二進位檔案方式來備份資料,然後在從二進位檔案將資料恢複到某一時段或某一操作點。

1:開啟binlog日誌記錄

修改mysql教程設定檔mysql.ini,在[mysqld]節點下添加

Xml代碼 
# log-bin   
log-bin = E:/log/logbin.log  

# log-bin
log-bin = E:/log/logbin.log路徑中不要包含中文和空格。重啟mysql服務。通過命令列停止和啟動mysql服務

Xml代碼 
c:>net stop mysql;   
c:>net start mysql;  

c:>net stop mysql;
c:>net start mysql;進入命令列進入mysql並查看二進位日誌是否已經啟動

Sql代碼 
mysql>show variables like 'log_%';  

mysql>show variables like 'log_%';日誌成功開啟後,會在E:/log/目錄下建立logbin.index和logbin.000001兩個檔案。logbin.000001就是資料庫的備份檔案,以後就可以通過此檔案對資料庫進行恢複操作。

2:查看備份的二進位檔案

Sql代碼 
c:mysqlbin>mysqlbinlog e:/log/logbin.000001  

c:mysqlbin>mysqlbinlog e:/log/logbin.000001   日後記錄的操作多了,命令列方式基本就用不上了。可以使用將日誌匯出檔案的方式來查看日誌內容

   2.1 匯出

Xml代碼 
c:mysqlbin>mysqlbinlog e:/log/logbin.000001 > e:/log/log.txt  

c:mysqlbin>mysqlbinlog e:/log/logbin.000001 > e:/log/log.txt   ">": 匯入到檔案中; ">>": 追加到檔案中

   如果有多個記錄檔

Sql代碼 
c:mysqlbin> mysqlbinlog e:/log/logbin.000001 >  e:/log/log.sql   
c:mysqlbin> mysqlbinlog e:/log/logbin.000002 >> e:/log/log.sql   

c:mysqlbin> mysqlbinlog e:/log/logbin.000001 >  e:/log/log.sql
c:mysqlbin> mysqlbinlog e:/log/logbin.000002 >> e:/log/log.sql    2.2 按指定位置匯出:

Sql代碼 
c:mysqlbin>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 > e:/log/log3.txt  

c:mysqlbin>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 > e:/log/log3.txt   2.3 按指定時間匯出:

Xml代碼 
c:mysqlbin>mysqlbinlog --start-datetime="2010-01-07 11:25:56" --stop-datetime="2010-01-07 13:23:50" e:/log/logbin.000001 > e:/log/log_by_date22.txt  

c:mysqlbin>mysqlbinlog --start-datetime="2010-01-07 11:25:56" --stop-datetime="2010-01-07 13:23:50" e:/log/logbin.000001 > e:/log/log_by_date22.txt 

3:從備份恢複資料庫

    做了一次更新操作,之後日誌的內容如下:

Sql代碼 
/*!40019 SET @@session.max_insert_delayed_threads=0*/;   
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;   
DELIMITER /*!*/;   
# at 4   
#110107 13:23:50 server id 1  end_log_pos 106   Start: binlog v 4, server v 5.1.53-community-log created 110107 13:23:50 at startup   
# Warning: this binlog is either in use or was not closed properly.   
ROLLBACK/*!*/;   
BINLOG '   
ZqMmTQ8BAAAAZgAAAGoAAAABAAQANS4xLjUzLWNvbW11bml0eS1sb2cAAAAAAAAAAAAAAAAAAAAA   
AAAAAAAAAAAAAAAAAABmoyZNEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC   
'/*!*/;   
# at 106   
#110107 13:26:58 server id 1  end_log_pos 185   Query   thread_id=44    exec_time=1 error_code=0   
SET TIMESTAMP=1294378018/*!*/;   
SET @@session.ps教程eudo_thread_id=44/*!*/;   
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;   
SET @@session.sql_mode=1344274432/*!*/;   
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;   
/*!C utf8 *//*!*/;   
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;   
SET @@session.lc_time_names=0/*!*/;   
SET @@session.collation_database=DEFAULT/*!*/;   
BEGIN  
/*!*/;   
# at 185   
#110107 13:26:58 server id 1  end_log_pos 338   Query   thread_id=44    exec_time=1 error_code=0   
use ncl-interactive/*!*/;   
SET TIMESTAMP=1294378018/*!*/;   
UPDATE `t_system_id` SET `id_value`='3000' WHERE (`table_name`='t_working_day')   
/*!*/;   
# at 338   
#110107 13:26:58 server id 1  end_log_pos 365   Xid = 8016   
COMMIT/*!*/;   
DELIMITER ;   
DELIMITER /*!*/;   
DELIMITER ;   
# End of log file   
ROLLBACK /* added by mysqlbinlog */;   
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;  

/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#110107 13:23:50 server id 1  end_log_pos 106  Start: binlog v 4, server v 5.1.53-community-log created 110107 13:23:50 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
ZqMmTQ8BAAAAZgAAAGoAAAABAAQANS4xLjUzLWNvbW11bml0eS1sb2cAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAABmoyZNEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
'/*!*/;
# at 106
#110107 13:26:58 server id 1  end_log_pos 185  Query thread_id=44 exec_time=1 error_code=0
SET TIMESTAMP=1294378018/*!*/;
SET @@session.pseudo_thread_id=44/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1344274432/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 185
#110107 13:26:58 server id 1  end_log_pos 338  Query thread_id=44 exec_time=1 error_code=0
use ncl-interactive/*!*/;
SET TIMESTAMP=1294378018/*!*/;
UPDATE `t_system_id` SET `id_value`='3000' WHERE (`table_name`='t_working_day')
/*!*/;
# at 338
#110107 13:26:58 server id 1  end_log_pos 365  Xid = 8016
COMMIT/*!*/;
DELIMITER ;
DELIMITER /*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
  3.1 恢複:

Sql代碼 
c:mysqlbin>mysqlbinlog e:/log/logbin.000001 | mysql -u root -p  

c:mysqlbin>mysqlbinlog e:/log/logbin.000001 | mysql -u root -p   3.2 按指定位置恢複:

Sql代碼 
c:mysqlbin>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 | mysql -u root -p   

c:mysqlbin>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 | mysql -u root -p    3.3 按指定時間恢複:

Xml代碼 
c:mysqlbin>mysqlbinlog --start-datetime="2010-01-07 11:25:56" --stop-datetime="2010-01-07 13:23:50" e:/log/logbin.000001 | mysql -u root -p   

c:mysqlbin>mysqlbinlog --start-datetime="2010-01-07 11:25:56" --stop-datetime="2010-01-07 13:23:50" e:/log/logbin.000001 | mysql -u root -p    3.4 通過匯出的指令檔恢複

Sql代碼 
c:mysqlbin>mysql -e "source e:/log/log.sql"  

c:mysqlbin>mysql -e "source e:/log/log.sql"4.其他常用操作

    4.1 查看所有記錄檔


Sql代碼 
mysql>show master logs;  

mysql>show master logs;    4.2 當前使用的binlog檔案

Sql代碼 
mysql>show binlog events g;  

mysql>show binlog events g;    4.3 產生一個新的binlog記錄檔

Sql代碼 
mysql>flush logs;  

mysql>flush logs;    4.4 刪除所有二進位日誌,並從新開始記錄(注意:reset master命令會刪除所有的二進位日誌)

Sql代碼 
mysql > flush logs;   
mysql > reset master;  

mysql > flush logs;
mysql > reset master;    4.5 快速備份資料到sql檔案

Sql代碼 
c:mysqlbin>mysqldump -u root -p --opt --quick interactive > e:/log/mysqldump.sql  

c:mysqlbin>mysqldump -u root -p --opt --quick interactive > e:/log/mysqldump.sql    為了方便查看,把從指令碼恢複的命令在寫一次

Sql代碼 
c:mysqlbin>mysql -e "source e:/log/mysqldump.sql" 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.