mysql data recovery, binlog Detailed

Personal blog: mysql data recovery, binlog Detailed

binlog log data recovery, is to save the error and data corruption straw, so understanding and use of binglog for technicians or necessary

binlog generally used

  • Open binlog replication master from the master node to the slave node transmitting binary log data from the master to achieve consistent
  • The second point is natural for data recovery, and use mysqlbinlog tool to recover data

Because my own site I encountered mysql table is accidentally deleted operation, after the drop table when I was quite calm, although I do not know if I can save the data back, for personal websites may not have a master-slave, too did not the perfect mysql configuration settings, in this case, of course, is most worried about whether binlog opens, if not open binlog data backup and did not do, I feel basically the GG

Because I mysql is installed by docker container, so the specific my.cnf configuration file in which to have forgotten

  1. Use find / -name my.cnf find the file in which
root@0d5861775029:/# find / -name my.cnf

find: '/proc/1/map_files': Operation not permitted
find: '/proc/182/map_files': Operation not permitted
find: '/proc/187/map_files': Operation not permitted
find: '/proc/1601/map_files': Operation not permitted
find: '/proc/1731/map_files': Operation not permitted
find: '/proc/1741/map_files': Operation not permitted
/etc/alternatives/my.cnf
/etc/mysql/my.cnf
/var/lib/dpkg/alternatives/my.cnf

root@0d5861775029:/# 
  • View my.cnf configuration and find binlog mysql data storage location
#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Custom config should go here
!includedir /etc/mysql/conf.d/
  • See data storage directory is mysql datadir = / var / lib / mysql directory
root@0d5861775029:/etc/mysql# cd /var/lib/mysql/
root@0d5861775029:/var/lib/mysql# ls
#innodb_temp   binlog.index client-key.pem  ib_logfile1  mysql.ibd       server-cert.pem  undo_002
auto.cnf       ca-key.pem   db_blog     ibdata1      performance_schema  server-key.pem
binlog.000001  ca.pem       ib_buffer_pool  ibtmp1       private_key.pem     sys
binlog.000002  client-cert.pem  ib_logfile0 mysql        public_key.pem  undo_001

The above foreplay have read, this is actually not the binlog specific use, but I personally find the way data directory Next, I will detail the use of binlog

First, open the binlog log

  • Check whether to open binlog
    • ON said it had opened
    • So you can see more show variables like 'log_%';
    mysql> show variables like 'log_bin';
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | log_bin       | ON    |
    +---------------+-------+
    1 row in set (0.00 sec)
mysql> show variables like 'log_%';
+----------------------------------------+----------------------------------------+
| Variable_name                          | Value                                  |
+----------------------------------------+----------------------------------------+
| log_bin                                | ON                                     |
| log_bin_basename                       | /var/lib/mysql/binlog                  |
| log_bin_index                          | /var/lib/mysql/binlog.index            |
| log_bin_trust_function_creators        | OFF                                    |
| log_bin_use_v1_row_events              | OFF                                    |
| log_error                              | stderr                                 |
| log_error_services                     | log_filter_internal; log_sink_internal |
| log_error_suppression_list             |                                        |
| log_error_verbosity                    | 2                                      |
| log_output                             | FILE                                   |
| log_queries_not_using_indexes          | OFF                                    |
| log_slave_updates                      | ON                                     |
| log_slow_admin_statements              | OFF                                    |
| log_slow_extra                         | OFF                                    |
| log_slow_slave_statements              | OFF                                    |
| log_statements_unsafe_for_binlog       | ON                                     |
| log_throttle_queries_not_using_indexes | 0                                      |
| log_timestamps                         | UTC                                    |
+----------------------------------------+----------------------------------------+
18 rows in set (0.00 sec)
  • Edit my.cnf open binlog
在[mysqld] 区块
设置/添加 log-bin=mysql-bin  确认是打开状态(值 mysql-bin 是日志的基本名或前缀名);

Then restart the mysql

Second, the operation command to view the log binlog

  1. View all binlog log list
mysql> show logs;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'logs' at line 1
mysql> show master logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |   3091158 | No        |
| binlog.000002 | 141156437 | No        |
+---------------+-----------+-----------+
2 rows in set (0.17 sec)
  1. View master status, which is the latest in a binlog log number and the last name of the operation end position pos event
mysql> show master status;
+---------------+-----------+--------------+------------------+-------------------+
| File          | Position  | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+-----------+--------------+------------------+-------------------+
| binlog.000002 | 141156437 |              |                  |                   |
+---------------+-----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  1. Refresh log logs will generate a number of new binlog log file
mysql> flush logs;
  1. Reset (Clear) all binlog log
mysql> reset master;

Third, view the contents of the log binlog

  1. Use mysqlbinlog command to view

    Because binlog is a binary file, the ordinary viewer can not open the file, you must use the built-in command to view mysqlbinlog

  • mysqlbinlog binlog.000002 not observed using mysqlbinlog
  1. View binlog log in mysql
mysql> show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];

 选项解析:
   IN 'log_name'   指定要查询的binlog文件名(不指定就是第一个binlog文件)
   FROM pos        指定从哪个pos起始点开始查起(不指定就是从整个文件首个pos点开始算)
   LIMIT [offset,] 偏移量(不指定就是0)
   row_count       查询总条数(不指定就是所有行)

 截取部分查询结果:
 *************************** 20. row ***************************
    Log_name: mysql-bin.000021  ----------------------------------------------> 查询的binlog日志文件名
         Pos: 11197 ----------------------------------------------------------> pos起始点:
  Event_type: Query ----------------------------------------------------------> 事件类型:Query
   Server_id: 1 --------------------------------------------------------------> 标识是由哪台服务器执行的
 End_log_pos: 11308 ----------------------------------------------------------> pos结束点:11308(即:下行的pos起始点)
        Info: use `zyyshop`; INSERT INTO `team2` VALUES (0,345,'asdf8er5') ---> 执行的sql语句
 *************************** 21. row ***************************
    Log_name: mysql-bin.000021
         Pos: 11308 ----------------------------------------------------------> pos起始点:11308(即:上行的pos结束点)
  Event_type: Query
   Server_id: 1
 End_log_pos: 11417
        Info: use `zyyshop`; /*!40000 ALTER TABLE `team2` ENABLE KEYS */
 *************************** 22. row ***************************
    Log_name: mysql-bin.000021
         Pos: 11417
  Event_type: Query
   Server_id: 1
 End_log_pos: 11510
        Info: use `zyyshop`; DROP TABLE IF EXISTS `type`
  1. Specify the query log binlog.000002
mysql> show binlog events in 'binlog.000002' limit 10;
+---------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                                  |
+---------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| binlog.000002 |    4 | Format_desc    |         1 |         124 | Server ver: 8.0.16, Binlog ver: 4                                                                                                                                     |
| binlog.000002 |  124 | Previous_gtids |         1 |         155 |                                                                                                                                                                       |
| binlog.000002 |  155 | Anonymous_Gtid |         1 |         234 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                  |
| binlog.000002 |  234 | Query          |         1 |         482 | CREATE USER 'schwarzeni'@'localhost' IDENTIFIED WITH 'caching_sha2_password' AS '$A$005$H{;gmzB@[}K1i\nBcce80ezg8j3o0qDdYocc1OxBkShlQyzmOV/c4rGP69' /* xid=7 */ |
| binlog.000002 |  482 | Anonymous_Gtid |         1 |         561 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                  |
| binlog.000002 |  561 | Query          |         1 |         801 | CREATE USER 'cuishifeng'@'%' IDENTIFIED WITH 'caching_sha2_password' AS '$A$005$f8Zs\ZhY(9]HPTcaN83yCTNmHs/LQsa2DerCX.ZVgd4InrYiCpj75mA' /* xid=8 */         |
| binlog.000002 |  801 | Anonymous_Gtid |         1 |         878 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                  |
| binlog.000002 |  878 | Query          |         1 |         968 | FLUSH PRIVILEGES                                                                                                                                                      |
| binlog.000002 |  968 | Anonymous_Gtid |         1 |        1047 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                  |
| binlog.000002 | 1047 | Query          |         1 |        1256 | ALTER USER 'cuishifeng'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*10320381F36BE49A18F09B06A4BC005223975101' /* xid=12 */                                       |
+---------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)
  1. Specify the query binlog.000002 this file from pos point: 968 search from the beginning
mysql> show binlog events in 'binlog.000002' from 968 limit 10;
+---------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
| Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                            |
+---------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
| binlog.000002 |  968 | Anonymous_Gtid |         1 |        1047 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| binlog.000002 | 1047 | Query          |         1 |        1256 | ALTER USER 'cuishifeng'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*10320381F36BE49A18F09B06A4BC005223975101' /* xid=12 */ |
| binlog.000002 | 1256 | Anonymous_Gtid |         1 |        1333 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| binlog.000002 | 1333 | Query          |         1 |        1423 | flush privileges                                                                                                                |
| binlog.000002 | 1423 | Anonymous_Gtid |         1 |        1500 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| binlog.000002 | 1500 | Query          |         1 |        1646 | GRANT ALL PRIVILEGES ON *.* TO 'cuishifeng'@'%' /* xid=70 */                                                                    |
| binlog.000002 | 1646 | Anonymous_Gtid |         1 |        1723 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| binlog.000002 | 1723 | Query          |         1 |        1813 | flush privileges                                                                                                                |
| binlog.000002 | 1813 | Anonymous_Gtid |         1 |        1890 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| binlog.000002 | 1890 | Query          |         1 |        1968 | FLUSH TABLES                                                                                                                    |
+---------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)

As can be seen from the log mysql command and has a starting position, it is useful for recovering data

  1. The first query (oldest) of binlog log

mysql> show binlog events;

Fourth, now recover data from binlog log

  1. Common Commands
恢复语法格式:
# mysqlbinlog mysql-bin.0000xx | mysql -u用户名 -p密码 数据库名
 常用选项:
  --start-position=953                   起始pos点
  --stop-position=1437                   结束pos点
  --start-datetime="2013-11-29 13:18:54" 起始时间点
  --stop-datetime="2013-11-29 13:21:53"  结束时间点
  --database=zyyshop                     指定只恢复zyyshop数据库(一台主机上往往有多个数据库,只限本地log日志)
    
不常用选项:    
  -u --user=name              Connect to the remote server as username.连接到远程主机的用户名
  -p --password[=name]        Password to connect to remote server.连接到远程主机的密码
  -h --host=name              Get the binlog from server.从远程主机上获取binlog日志
  --read-from-remote-server   Read binary logs from a MySQL server.从某个MySQL服务器上读取binlog日志

小结:实际是将读出的binlog日志内容,通过管道符传递给mysql命令。这些命令、文件尽量写成绝对路径;

Log recovery time is equivalent to execute DDL statements, if a table log log recovery statement such as the existence of your library and the process of creating this table is certainly a dead end so it is best to specify a specific location recovery

  1. View binlog log to determine where to resume
 mysql> show binlog events in 'mysql-bin.000023';
        
以下为末尾片段:
+------------------+------+------------+-----------+-------------+------------------------------------------------------------+
| Log_name         | Pos  | Event_type | Server_id | End_log_pos | Info                                                       |
+------------------+------+------------+-----------+-------------+------------------------------------------------------------+
| mysql-bin.000023 |  922 | Xid        |         1 |         953 | COMMIT /* xid=3820 */                                      |
| mysql-bin.000023 |  953 | Query      |         1 |        1038 | BEGIN                                                      |
| mysql-bin.000023 | 1038 | Query      |         1 |        1164 | use `zyyshop`; update zyyshop.tt set name='李四' where id=4|
| mysql-bin.000023 | 1164 | Xid        |         1 |        1195 | COMMIT /* xid=3822 */                                      |
| mysql-bin.000023 | 1195 | Query      |         1 |        1280 | BEGIN                                                      |
| mysql-bin.000023 | 1280 | Query      |         1 |        1406 | use `zyyshop`; update zyyshop.tt set name='小二' where id=2|
| mysql-bin.000023 | 1406 | Xid        |         1 |        1437 | COMMIT /* xid=3823 */                                      |
| mysql-bin.000023 | 1437 | Query      |         1 |        1538 | drop database zyyshop                                      |
+------------------+------+------------+-----------+-------------+------------------------------------------------------------+

通过分析,造成数据库破坏的pos点区间是介于 1437--1538 之间,只要恢复到1437前就可。
mysqlbinlog  --start-position=953  --stop-position=1538 --database=zyyshop binlog.000002 | mysql -uroot -p123456 -v zyyshop
  1. Specified time to recover
  • You know what period of time the misuse
mysql> drop table tt;

@ --start-datetime="2013-11-29 13:18:54"  起始时间点
@ --stop-datetime="2013-11-29 13:21:53"   结束时间点

# mysqlbinlog --start-datetime="2013-11-29 13:18:54" --stop-datetime="2013-11-29 13:21:53" --database=zyyshop binlog.000002 | mysql -uroot -p123456 -v zyyshop

I hope this article has helped mistakenly deleted data recovery operations smoothly friend

Guess you like

Origin www.cnblogs.com/NiceCui/p/11246156.html
Recommended