From the "delete library on foot 'talk turned BinLog prevent accidental deletion table data and database structure

Many students in the recent online crazy pass epidemic delete library = running, work accidentally deleted data or database we will need at running it? I do not think we know the MySQL database binlog log records all of our operations on the database Then you turn on the road to self-help programmers

First, open Binlog log data recovery

1, confirm whether the database is open binlog log

show variables like 'log_%';

Output follows the need to manually stop opening disposed MySQL binlog log

mysql> show variables like 'log_%';
+----------------------------------------+---------------------------------------------+
| Variable_name                          | Value                                       |
+----------------------------------------+---------------------------------------------+
| log_bin                                | OFF                                         |
| log_bin_basename                       |                                             |
| log_bin_index                          |                                             |
| log_bin_trust_function_creators        | OFF                                         |
| log_bin_use_v1_row_events              | OFF                                         |
| log_builtin_as_identified_by_password  | OFF                                         |
+----------------------------------------+---------------------------------------------+
21 rows in set (0.00 sec)

2, open binlog log

Unix, Linux system to find  my.cnffiles, if your computer is windowns find the  my.ini file or modify the configuration parameters as follows:

#修改配置之前节奏不能乱 害
cd /usr/local/MySQL_5.7.29/(这里是我的Mysql安装目录,不会查的输入:ps -ef|grep mysql)
[root@mryu MySQL_5.7.29]# ps -ef|grep mysql
root      1011     1  0 Feb29 ?        00:00:00 /bin/sh /usr/local/MySQL_5.7.29/bin/mysqld_safe --datadir=/usr/local/MySQL_5.7.29/mysql_data --pid-file=/usr/local/MySQL_5.7.29/mysql_pid/mysql.pid
mysql     1511  1011  0 Feb29 ?        00:05:03 /usr/local/MySQL_5.7.29/bin/mysqld --basedir=/usr/local/MySQL_5.7.29 --datadir=/usr/local/MySQL_5.7.29/mysql_data --plugin-dir=/usr/local/MySQL_5.7.29/lib/plugin --user=mysql --log-error=/usr/local/MySQL_5.7.29/mysql_log/mysql.err --pid-file=/usr/local/MySQL_5.7.29/mysql_pid/mysql.pid --socket=/tmp/mysql.sock --port=3306
root     25405 25382  0 21:37 pts/1    00:00:00 mysql -u root -p
root     25429 25055  0 21:50 pts/0    00:00:00 grep --color=auto mysql

I believe that many small partners would like this to configure binlog path (Congratulations stepped pit, I have also stepped Shetenglebantian)

cd /usr/local/MySQL_5.7.29
mkdir mysql_bin
cd mysql_bin
touch mysql_bin.log
chmod 754 mysql_bin/
# Binary Logging.
#日志文件的名字及存储路径
log-bin=/usr/local/MySQL_5.7.29/mysql_bin/mysql_bin.log
#设置日志格式
binlog-format=mixed

Which binlog-format have three options:

STATMENT Each SQL statement that modifies data is recorded in the binlog.
ROW Not recorded context information each SQL statement, only records which record is modified.
MIXED Mixtures of at least two modes, normal copy mode using STATEMENT save binlog, for operational use ROW mode STATEMENT mode can not be copied to save binlog, MySQL will choose to save the log According SQL statement executed.

3, CentOS system opens up the MySQL binlog log results restart parsing error

One problem: the lack of service ID (you have not provided the mandatory server-id)

Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details 
通过报错信息的提示分别执行systemctl status mysqld.service和journalctl -xe命令,看不出问题来
翻墙看到有人说可以查看系统错误日志 tail -f /var/log/messages
发现/var/log/messages里面打印出下面错误信息:
[root@mryu bin]# tail -f /var/log/messages
    [ERROR] You have enabled the binary log, but you haven’t provided the mandatory server-id. Please refer to the proper server start-up parameters documentation

Solution to configure new server-id

提示你启用了binlog,但是没有提供强制规定的server-id参数。
原来我在my.cnf里面只配置了log-bin = mysql-bin没有配置server-id
添加配置server-id=1573(随意设定但集群环境下要配置不同的值不能重复)再次重启mysql,正常!

Second problem: the lack of SOCK file (through socket '/tmp/mysql.sock')

[root@mryu MySQL_5.7.29]# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)

Solution establish flexible connections

#具体看自己的安装目录
ln -s /usr/local/MySQL_5.7.29/mysql_sock/mysql.sock /tmp/mysql.sock

After more than two months to solve the problem restart MYSQL service

service mysql restart (可能有些同学是mysqld,具体看自己)
systemtcl restart mysql.service

4, check that it is turned ON successfully on behalf of success

mysql> show variables like 'log_%';
+----------------------------------------+---------------------------------------------------+
| Variable_name                          | Value                                             |
+----------------------------------------+---------------------------------------------------+
| log_bin                                | ON                                                |
| log_bin_basename                       | /usr/local/MySQL_5.7.29/mysql_bin/mysql_bin       |
| log_bin_index                          | /usr/local/MySQL_5.7.29/mysql_bin/mysql_bin.index |
|+----------------------------------------+---------------------------------------------------+
21 rows in set (0.00 sec)

Second, create and delete libraries and restore data

1. Create a Test table

#创建简单表结构
mysql> create table student(
   id INT NOT NULL AUTO_INCREMENT,
   name VARCHAR(100) NOT NULL,
   num VARCHAR(40) NOT NULL,
   PRIMARY KEY (id )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

#插入测试数据
mysql> use Blog;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> insert into student(name,num)values('张三','1235');
Query OK, 1 row affected (0.01 sec)
mysql> insert into student(name,num)values('李四','1236');
Query OK, 1 row affected (0.00 sec)

#查询表内容
mysql> select * from student;
+----+--------+------+
| id | name   | num  |
+----+--------+------+
|  1 | 张三   | 1235 |
|  2 | 李四   | 1236 |
+----+--------+------+
2 rows in set (0.00 sec)

Third, delete libraries do not foot stage

Scene One simple experiment: an article mistakenly deleted data (the student accidentally table id = 1 data is deleted how to do?)

mysql> delete from student where id=1;
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+--------+------+
| id | name   | num  |
+----+--------+------+
|  2 | 李四   | 1235 |
+----+--------+------+
1 row in set (0.00 sec)

With the memory manually insert this data? Try to use the  show master statuscommand to view the latest binlog log file

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 |     1416 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

After obtaining the latest binlog log file, use the  show binlog events in 'mysql-bin.000001';command to view binlog log file, as shown below: You need to keep in mind with the deletion of the offset value before deletion is End_log_pos value, Begin began to remember a good student affairs of this

mysql> show binlog events in 'mysql_bin.000001';
+------------------+------+--------------+--------+-----------+--------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                                                           |
+------------------+------+--------------+--------+-----------+--------------+

#删除前

| mysql_bin.000001 |  536 | Query        |    1573 |      615 | BEGIN                                                                                                                                                                                          |
| mysql_bin.000001 |  615 | Intvar       |    1573 |      647 | INSERT_ID=1                                                                                                                                                                                    |
| mysql_bin.000001 |  647 | Query        |    1573 |      773 | use `Blog`; insert into student(name,num)values('张三','1235')     

#删除后
                                                                                                                            
| mysql_bin.000001 | 1202 | Query        |    1573 |     1281 | BEGIN                                                                                                                                                                                          |
| mysql_bin.000001 | 1281 | Query        |    1573 |     1385 | use `Blog`; delete from student where id=1                                                                                                                                                     |
| mysql_bin.000001 | 1385 | Xid          |    1573 |     1416 | COMMIT /* xid=74 */   

binlog log records all of our operations on the database, including the offset of the statements before submission and submit the data will be used to restore these two offsets. Before officially entering data recovery we first understand a tool provided by MySQL: mysqlbinlog binlog log file is used to operate

Solution:

#还记得开始创建的mysql_bin文件夹吗 记住它的位置pwd一下再找到序列号对应的bin文件
[root@mryu ~]# cd /usr/local/MySQL_5.7.29/mysql_bin/
[root@mryu mysql_bin]# ll
-rw-r----- 1 mysql mysql 1416 Mar  9 23:06 mysql_bin.000001
-rw-r----- 1 mysql mysql   51 May  8  2019 mysql_bin.index

Open a new view window to resume using mysqlbinlog binary log file:

 mysqlbinlog binlog日志文件 --start-position 初始偏移量的位置 --stop-position 结束偏移量的位置 | mysql -u root -p 数据库名称;
#退出mysql命令窗口输入
 mysqlbinlog /usr/local/MySQL_5.27.9 --start-position 615 --stop-position 733| mysql -u root -p Blog;
再次查询则数据找回

For mistakenly deleted the table and library can also use binlog log to recover after all binlog diary records of all of our operations for MySQL, like a single data recovery, we also need to look at binlog log file to the starting offset and ending offset shift amount, to restore the data. And a single data table is not the same for the starting offset is the start offset before creating tables, end offset is the last one before the end of the offset delete the database, if executed rm -rf xxx this case only data recovery company to recover, no solution will lead to white pointer exception disk data to be lost again.

Published 35 original articles · won praise 16 · views 190 000 +

Guess you like

Origin blog.csdn.net/qq_38795430/article/details/104762097