MySQL數(shù)據(jù)庫恢復(fù)(使用mysqlbinlog命令)
更新時(shí)間:2011年08月07日 11:12:40 作者:
binlog是通過記錄二進(jìn)制文件方式來備份數(shù)據(jù),然后在從二進(jìn)制文件將數(shù)據(jù)恢復(fù)到某一時(shí)段或某一操作點(diǎn)。
1:開啟binlog日志記錄
修改mysql配置文件mysql.ini,在[mysqld]節(jié)點(diǎn)下添加
# log-bin
log-bin = E:/log/logbin.log
路徑中不要包含中文和空格。重啟mysql服務(wù)。通過命令行停止和啟動mysql服務(wù)
c:\>net stop mysql;
c:\>net start mysql;
進(jìn)入命令行進(jìn)入mysql并查看二進(jìn)制日志是否已經(jīng)啟動
Sql代碼
mysql>show variables like 'log_%';
日志成功開啟后,會在E:/log/目錄下創(chuàng)建logbin.index和logbin.000001兩個(gè)文件。logbin.000001就是數(shù)據(jù)庫的備份文件,以后就可以通過此文件對數(shù)據(jù)庫進(jìn)行恢復(fù)操作。
2:查看備份的二進(jìn)制文件
Sql代碼
c:\mysql\bin\>mysqlbinlog e:/log/logbin.000001
日后記錄的操作多了,命令行方式基本就用不上了??梢允褂脤⑷罩緦?dǎo)出文件的方式來查看日志內(nèi)容
2.1 導(dǎo)出
Xml代碼
c:\mysql\bin\>mysqlbinlog e:/log/logbin.000001 > e:/log/log.txt
">": 導(dǎo)入到文件中; ">>": 追加到文件中
如果有多個(gè)日志文件
Sql代碼
c:\mysql\bin\> mysqlbinlog e:/log/logbin.000001 > e:/log/log.sql
c:\mysql\bin\> mysqlbinlog e:/log/logbin.000002 >> e:/log/log.sq
2.2 按指定位置導(dǎo)出:
Sql代碼
c:\mysql\bin\>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 > e:/log/log3.txt
2.3 按指定時(shí)間導(dǎo)出:
Xml代碼
c:\mysql\bin\>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:從備份恢復(fù)數(shù)據(jù)庫
做了一次更新操作,之后日志的內(nèi)容如下:
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.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*/;
/*!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 恢復(fù):
Sql代碼
c:\mysql\bin\>mysqlbinlog e:/log/logbin.000001 | mysql -u root -p
3.2 按指定位置恢復(fù):
Sql代碼
c:\mysql\bin\>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 | mysql -u root -p
3.3 按指定時(shí)間恢復(fù):
Xml代碼
c:\mysql\bin\>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 通過導(dǎo)出的腳本文件恢復(fù)
Sql代碼
c:\mysql\bin\>mysql -e "source e:/log/log.sql"
4.其他常用操作
4.1 查看所有日志文件
Sql代碼
mysql>show master logs;
4.2 當(dāng)前使用的binlog文件
Sql代碼
mysql>show binlog events \g;
4.3 產(chǎn)生一個(gè)新的binlog日志文件
Sql代碼
mysql>flush logs;
4.4 刪除所有二進(jìn)制日志,并從新開始記錄(注意:reset master命令會刪除所有的二進(jìn)制日志)
Sql代碼
mysql > flush logs;
mysql > reset master;
4.5 快速備份數(shù)據(jù)到sql文件
Sql代碼
c:\mysql\bin>mysqldump -u root -p --opt --quick interactive > e:/log/mysqldump.sql
為了方便查看,把從腳本恢復(fù)的命令在寫一次
Sql代碼
c:\mysql\bin\>mysql -e "source e:/log/mysqldump.sql"
修改mysql配置文件mysql.ini,在[mysqld]節(jié)點(diǎn)下添加
復(fù)制代碼 代碼如下:
# log-bin
log-bin = E:/log/logbin.log
路徑中不要包含中文和空格。重啟mysql服務(wù)。通過命令行停止和啟動mysql服務(wù)
復(fù)制代碼 代碼如下:
c:\>net stop mysql;
c:\>net start mysql;
進(jìn)入命令行進(jìn)入mysql并查看二進(jìn)制日志是否已經(jīng)啟動
Sql代碼
復(fù)制代碼 代碼如下:
mysql>show variables like 'log_%';
日志成功開啟后,會在E:/log/目錄下創(chuàng)建logbin.index和logbin.000001兩個(gè)文件。logbin.000001就是數(shù)據(jù)庫的備份文件,以后就可以通過此文件對數(shù)據(jù)庫進(jìn)行恢復(fù)操作。
2:查看備份的二進(jìn)制文件
Sql代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\>mysqlbinlog e:/log/logbin.000001
日后記錄的操作多了,命令行方式基本就用不上了??梢允褂脤⑷罩緦?dǎo)出文件的方式來查看日志內(nèi)容
2.1 導(dǎo)出
Xml代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\>mysqlbinlog e:/log/logbin.000001 > e:/log/log.txt
">": 導(dǎo)入到文件中; ">>": 追加到文件中
如果有多個(gè)日志文件
Sql代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\> mysqlbinlog e:/log/logbin.000001 > e:/log/log.sql
c:\mysql\bin\> mysqlbinlog e:/log/logbin.000002 >> e:/log/log.sq
2.2 按指定位置導(dǎo)出:
Sql代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 > e:/log/log3.txt
2.3 按指定時(shí)間導(dǎo)出:
Xml代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\>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:從備份恢復(fù)數(shù)據(jù)庫
做了一次更新操作,之后日志的內(nèi)容如下:
Sql代碼
復(fù)制代碼 代碼如下:
/*!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*/;
/*!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 恢復(fù):
Sql代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\>mysqlbinlog e:/log/logbin.000001 | mysql -u root -p
3.2 按指定位置恢復(fù):
Sql代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\>mysqlbinlog --start-position=185 --stop-position=338 e:/log/logbin.000001 | mysql -u root -p
3.3 按指定時(shí)間恢復(fù):
Xml代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\>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 通過導(dǎo)出的腳本文件恢復(fù)
Sql代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\>mysql -e "source e:/log/log.sql"
4.其他常用操作
4.1 查看所有日志文件
Sql代碼
復(fù)制代碼 代碼如下:
mysql>show master logs;
4.2 當(dāng)前使用的binlog文件
Sql代碼
復(fù)制代碼 代碼如下:
mysql>show binlog events \g;
4.3 產(chǎn)生一個(gè)新的binlog日志文件
Sql代碼
復(fù)制代碼 代碼如下:
mysql>flush logs;
4.4 刪除所有二進(jìn)制日志,并從新開始記錄(注意:reset master命令會刪除所有的二進(jìn)制日志)
Sql代碼
復(fù)制代碼 代碼如下:
mysql > flush logs;
mysql > reset master;
4.5 快速備份數(shù)據(jù)到sql文件
Sql代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin>mysqldump -u root -p --opt --quick interactive > e:/log/mysqldump.sql
為了方便查看,把從腳本恢復(fù)的命令在寫一次
Sql代碼
復(fù)制代碼 代碼如下:
c:\mysql\bin\>mysql -e "source e:/log/mysqldump.sql"
您可能感興趣的文章:
相關(guān)文章
MySQL按時(shí)間拆分千萬級大表的實(shí)現(xiàn)代碼
這篇文章主要介紹了MySQL按時(shí)間拆分千萬級大表,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
mysql數(shù)據(jù)庫查詢優(yōu)化 mysql效率
MySQL由于它本身的小巧和操作的高效, 在數(shù)據(jù)庫應(yīng)用中越來越多的被采用.我在開發(fā)一個(gè)P2P應(yīng)用的時(shí)候曾經(jīng)使用MySQL來保存P2P節(jié)點(diǎn),由于P2P的應(yīng)用中,結(jié)點(diǎn)數(shù)動輒上萬個(gè),而且節(jié)點(diǎn)變化頻繁,因此一定要保持查詢和插入的高效.以下是我在使用過程中做的提高效率的三個(gè)有效的嘗試. 1. 使用statement進(jìn)行綁定查詢 2. 隨機(jī)的獲取記錄 3. 使用連接池管理連接.2008-01-01
mysql 批量查詢?nèi)∶恳唤M最新一條數(shù)據(jù)
根據(jù)車牌號查詢最新的一條交車記錄的‘合同號’ ,這里只需要查詢‘合同號’這個(gè)字段,這篇文章主要介紹了mysql 批量查詢?nèi)∶恳唤M最新一條數(shù)據(jù),需要的朋友可以參考下2024-02-02
從一個(gè)MySQL的例子來學(xué)習(xí)查詢語句
從一個(gè)MySQL的例子來學(xué)習(xí)查詢語句...2006-12-12

