mysql數據誤刪恢復

mysql環境:ubuntu;mysql

1.首先肯定是否開啓日誌功能;sql

show variables like 'log_%'; 
顯示結果
log_bin    ON
log_bin_basename    /var/lib/mysql/bin-log
log_bin_index    /var/lib/mysql/bin-log.index
log_bin_trust_function_creators    OFF
log_bin_use_v1_row_events    OFF
log_error    /var/log/mysql/error.log

若是你的log_bin選項爲OFF,此種方法沒法進行數據修復,這裏作學習恢復數據使用,瞭解binlog恢復數據流程;shell

2.若未開啓log_bin,須要進行數據庫的my.cnf文件中加入,個人是在 /etc/mysql/mysql.conf.d/mysqld.cnf數據庫

[mysqld]
#
# * Basic Settings
#
user            = mysql
log-bin=/var/lib/mysql/bin-log
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp

在user = mysql下增長一行log-bin=/var/lib/mysql/bin-log,此處爲存放日誌的路徑;ubuntu

重啓mysql;再到數據庫中查詢日誌開啓狀態(步驟1);直到log_bin=ON在進行下步操做socket

3.準備數據;學習

CREATE TABLE TEST_BAK ( 
    `name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL ,
     `age` INT( 3 ) NOT NULL
) ENGINE = MYISAM;

INSERT INTO TEST_BAK (`name`, `age`) VALUES ('zs', '1');
INSERT INTO TEST_BAK (`name`, `age`) VALUES ('ls', '2'); 
INSERT INTO TEST_BAK (`name`, `age`) VALUES ('ww', '3'); 
INSERT INTO TEST_BAK (`name`, `age`) VALUES ('zl', '4');

在執行刪除操做;delete from  TEST_BAK; 3d

4.還原: 查看日誌文件日誌

show master status;

顯示爲:
bin-log.000001	2893

找到對應的日誌文件;日誌文件路徑爲第二步log-bin指定的路徑code

在shell中執行

mysqlbinlog /var/lib/mysql/bin-log.000001
日誌顯示爲
CREATE TABLE TEST_BAK ( `name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL , `age` INT( 3 ) NOT NULL ) ENGINE = MYISAM
/*!*/;
# at 591
#161115 17:09:01 server id 1  end_log_pos 678 CRC32 0x69e55186 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
BEGIN
/*!*/;
# at 678
#161115 17:09:01 server id 1  end_log_pos 815 CRC32 0xf9cddc45 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
INSERT INTO TEST_BAK (`name`, `age`) VALUES ('zs', '1')
/*!*/;
# at 815
#161115 17:09:01 server id 1  end_log_pos 903 CRC32 0xce579575 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
COMMIT
/*!*/;
# at 903
#161115 17:09:01 server id 1  end_log_pos 990 CRC32 0x4e2f02a6 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
BEGIN
/*!*/;
# at 990
#161115 17:09:01 server id 1  end_log_pos 1127 CRC32 0x4ee68971 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
INSERT INTO TEST_BAK (`name`, `age`) VALUES ('ls', '2')
/*!*/;
# at 1127
#161115 17:09:01 server id 1  end_log_pos 1215 CRC32 0x7406a47f 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
COMMIT
/*!*/;
# at 1215
#161115 17:09:01 server id 1  end_log_pos 1302 CRC32 0x24c229d7 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
BEGIN
/*!*/;
# at 1302
#161115 17:09:01 server id 1  end_log_pos 1439 CRC32 0x2ee77621 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
INSERT INTO TEST_BAK (`name`, `age`) VALUES ('ww', '3')
/*!*/;
# at 1439
#161115 17:09:01 server id 1  end_log_pos 1527 CRC32 0xc72638bb 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
COMMIT
/*!*/;
# at 1527
#161115 17:09:01 server id 1  end_log_pos 1614 CRC32 0x856ba8e1 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
BEGIN
/*!*/;
# at 1614
#161115 17:09:01 server id 1  end_log_pos 1751 CRC32 0xe0f92561 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
INSERT INTO TEST_BAK (`name`, `age`) VALUES ('zl', '4')
/*!*/;
# at 1751
#161115 17:09:01 server id 1  end_log_pos 1839 CRC32 0x9050a3dd 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200941/*!*/;
COMMIT
/*!*/;
# at 1839
#161115 17:09:08 server id 1  end_log_pos 1926 CRC32 0xfb73c0c9 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200948/*!*/;
BEGIN
/*!*/;
# at 1926
#161115 17:09:08 server id 1  end_log_pos 2028 CRC32 0xb1ce3ffa 	Query	thread_id=1	exec_time=0	error_code=0
SET TIMESTAMP=1479200948/*!*/;
delete from TEST_BAK

上面爲最近操做的全部記錄;從678到1926都是我新增的數據,最後一行纔是我進行刪除操做,

5.還原:

mysqlbinlog /var/lib/mysql/bin-log.000001 --start-position=678 --stop-position=1926 | mysql -uroot -p

此處只爲binlog方式還原數據;若是是線上數據記得鎖表及解鎖lock tables TEST_BAK read ;unlock tables;

6.碰到的問題:

Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

數據庫默認安裝的目錄爲/tmp/mysql.sock,而我用ps -ef|grep mysqld

顯示爲/var/run/mysqld/mysqld.sock

mysql     4134  3570  0 17:51 ?        00:00:04 /usr/sbin/mysqld --basedir=/usr 
--datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql 
--log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid 
--socket=/var/run/mysqld/mysqld.sock --port=3306

解決方式:vi /etc/my.cnf 將 socket 都指向/var/run/mysqld/mysqld.sock, 重啓mysql

[client]
#password       = your_password
port            = 3306
socket          = /var/run/mysqld/mysqld.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
log-bin=/usr/log/bin-log
port            = 3306
socket          = /var/run/mysqld/mysqld.sock
相關文章
相關標籤/搜索