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