1. 程式人生 > >mysql5.6使用xtrabackup和binlog進行完全恢復

mysql5.6使用xtrabackup和binlog進行完全恢復

1、連輸mysql,檢視mysql版本:

[[email protected] binlog]$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.39-log Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.39-log |
+------------+
1 row in set (0.00 sec)

2、查詢當前mysql的所有binlog日誌

mysql> show master logs ;
+---------------+-----------+
| Log_name      | File_size |
+---------------+-----------+
| binlog.000001 |       164 |
| binlog.000002 |       120 |
+---------------+-----------+
2 rows in set (0.00 sec)

3、建立測試表

mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jfedu              |
| mysql              |
| performance_schema |
| test               |
| zhjsdb01           |
+--------------------+
6 rows in set (0.00 sec)

mysql> use test ;
Database changed
mysql> show tables ;
Empty set (0.00 sec)

mysql> create table test001(id int,name varchar(10));
Query OK, 0 rows affected (0.07 sec)

mysql> show master logs ;
+---------------+-----------+
| Log_name      | File_size |
+---------------+-----------+
| binlog.000001 |       592 |
+---------------+-----------+
1 row in set (0.00 sec)

4、為了我們便於測試,清理掉歷史binlog日誌,從新開始

mysql> reset master; 
Query OK, 0 rows affected (0.00 sec)

mysql> show master logs ;
+---------------+-----------+
| Log_name      | File_size |
+---------------+-----------+
| binlog.000001 |       120 |
+---------------+-----------+
1 row in set (0.00 sec)

5、此時開始在表中插入兩條初始資料:

mysql> insert into test001 values(1,'AAAA');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test001 values(2,'BBBB');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test001 ;
+------+------+
| id   | name |
+------+------+
|    1 | AAAA |
|    2 | BBBB |
+------+------+
2 rows in set (0.00 sec)

6、然後就可以使用xtrabackup進行備份了:

[[email protected] xtrabackup]# innobackupex \
> --defaults-file=/u01/my3306/my.cnf \
> --user=root \
> --password='root123' \
> --socket=/u01/my3306/run/mysql.sock \
> --no-timestamp \
> /apps/backup/xtrabackup

--defaults-file 表示備份使用的配置檔案,我這邊就是我mysql資料庫的配置檔案;

--user和--password指定備份使用的mysql使用者及密碼;

--socket指定mysql的socket檔案;

--no-timestamp表示備份目錄自定義,不使用系統預設;

/apps/backup/xtrabackup表示備份檔案產生在此目錄中;

[[email protected] xtrabackup]# innobackupex \
> --defaults-file=/u01/my3306/my.cnf \
> --user=root \
> --password='root123' \
> --socket=/u01/my3306/run/mysql.sock \
> --no-timestamp \
> /apps/backup/xtrabackup
xtrabackup: recognized server arguments: --datadir=/u01/my3306/data --open_files_limit=65535 --server-id=101 --tmpdir=/u01/my3306/tmp --log_bin=/u01/my3306/log/binlog/binlog --innodb_data_home_dir=/u01/my3306/log/iblog --innodb_log_group_home_dir=/u01/my3306/log/iblog --innodb_adaptive_hash_index=1 --innodb_checksums=1 --innodb_buffer_pool_size=128M --innodb_data_file_path=ibdata1:32M;ibdata2:16M:autoextend --innodb_doublewrite=1 --innodb_file_per_table=1 --innodb_flush_log_at_trx_commit=1 --innodb_flush_method=O_DIRECT --innodb_io_capacity=1000 --innodb_log_buffer_size=67108864 --innodb_log_file_size=1048576000 --innodb_log_files_in_group=4 --innodb_max_dirty_pages_pct=60 --innodb_open_files=60000 --innodb_read_io_threads=4 --innodb_use_native_aio=0 --innodb_write_io_threads=10 
xtrabackup: recognized client arguments: --datadir=/u01/my3306/data --open_files_limit=65535 --server-id=101 --tmpdir=/u01/my3306/tmp --log_bin=/u01/my3306/log/binlog/binlog --innodb_data_home_dir=/u01/my3306/log/iblog --innodb_log_group_home_dir=/u01/my3306/log/iblog --innodb_adaptive_hash_index=1 --innodb_checksums=1 --innodb_buffer_pool_size=128M --innodb_data_file_path=ibdata1:32M;ibdata2:16M:autoextend --innodb_doublewrite=1 --innodb_file_per_table=1 --innodb_flush_log_at_trx_commit=1 --innodb_flush_method=O_DIRECT --innodb_io_capacity=1000 --innodb_log_buffer_size=67108864 --innodb_log_file_size=1048576000 --innodb_log_files_in_group=4 --innodb_max_dirty_pages_pct=60 --innodb_open_files=60000 --innodb_read_io_threads=4 --innodb_use_native_aio=0 --innodb_write_io_threads=10 
180624 19:01:54 innobackupex: Starting the backup operation

IMPORTANT: Please check that the backup run completes successfully.
           At the end of a successful backup run innobackupex
           prints "completed OK!".

180624 19:01:54  version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;port=3306;mysql_socket=/u01/my3306/run/mysql.sock' as 'root'  (using password: YES).
180624 19:01:54  version_check Connected to MySQL server
180624 19:01:54  version_check Executing a version check against the server...
180624 19:01:54  version_check Done.
180624 19:01:54 Connecting to MySQL server host: localhost, user: root, password: set, port: 3306, socket: /u01/my3306/run/mysql.sock
Using server version 5.6.39-log
innobackupex version 2.4.12 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 170eb8c)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /u01/my3306/data
xtrabackup: open files limit requested 65535, set to 65535
xtrabackup: using the following InnoDB configuration:
xtrabackup:   innodb_data_home_dir = /u01/my3306/log/iblog
xtrabackup:   innodb_data_file_path = ibdata1:32M;ibdata2:16M:autoextend
xtrabackup:   innodb_log_group_home_dir = /u01/my3306/log/iblog
xtrabackup:   innodb_log_files_in_group = 4
xtrabackup:   innodb_log_file_size = 1048576000
xtrabackup: using O_DIRECT
InnoDB: Number of pools: 1
180624 19:01:54 >> log scanned up to (2848651)
xtrabackup: Generating a list of tablespaces
InnoDB: Allocated tablespace ID 3 for mysql/slave_relay_log_info, old maximum was 0
180624 19:01:55 [01] Copying /u01/my3306/log/iblog/ibdata1 to /apps/backup/xtrabackup/ibdata1
180624 19:01:55 [01]        ...done
180624 19:01:55 [01] Copying /u01/my3306/log/iblog/ibdata2 to /apps/backup/xtrabackup/ibdata2
180624 19:01:55 [01]        ...done
180624 19:01:55 [01] Copying ./mysql/slave_relay_log_info.ibd to /apps/backup/xtrabackup/mysql/slave_relay_log_info.ibd
180624 19:01:55 [01]        ...done
180624 19:01:55 [01] Copying ./mysql/innodb_index_stats.ibd to /apps/backup/xtrabackup/mysql/innodb_index_stats.ibd
180624 19:01:55 [01]        ...done
180624 19:01:55 [01] Copying ./mysql/slave_master_info.ibd to /apps/backup/xtrabackup/mysql/slave_master_info.ibd
180624 19:01:55 [01]        ...done
180624 19:01:55 [01] Copying ./mysql/innodb_table_stats.ibd to /apps/backup/xtrabackup/mysql/innodb_table_stats.ibd
180624 19:01:55 [01]        ...done
180624 19:01:55 [01] Copying ./mysql/slave_worker_info.ibd to /apps/backup/xtrabackup/mysql/slave_worker_info.ibd
180624 19:01:55 [01]        ...done
180624 19:01:55 [01] Copying ./test/test001.ibd to /apps/backup/xtrabackup/test/test001.ibd
180624 19:01:55 [01]        ...done
180624 19:01:55 [01] Copying ./jfedu/t2_bak.ibd to /apps/backup/xtrabackup/jfedu/t2_bak.ibd
180624 19:01:55 [01]        ...done
180624 19:01:55 [01] Copying ./zhjsdb01/test01.ibd to /apps/backup/xtrabackup/zhjsdb01/test01.ibd
180624 19:01:55 [01]        ...done
180624 19:01:55 >> log scanned up to (2848651)
180624 19:01:56 Executing FLUSH NO_WRITE_TO_BINLOG TABLES...
180624 19:01:56 Executing FLUSH TABLES WITH READ LOCK...
180624 19:01:56 Starting to backup non-InnoDB tables and files
180624 19:01:56 [01] Copying ./mysql/time_zone_transition_type.MYI to /apps/backup/xtrabackup/mysql/time_zone_transition_type.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/proc.MYD to /apps/backup/xtrabackup/mysql/proc.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone.frm to /apps/backup/xtrabackup/mysql/time_zone.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/procs_priv.frm to /apps/backup/xtrabackup/mysql/procs_priv.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_category.frm to /apps/backup/xtrabackup/mysql/help_category.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_category.MYI to /apps/backup/xtrabackup/mysql/help_category.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/innodb_table_stats.frm to /apps/backup/xtrabackup/mysql/innodb_table_stats.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/slow_log.CSV to /apps/backup/xtrabackup/mysql/slow_log.CSV
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_transition.frm to /apps/backup/xtrabackup/mysql/time_zone_transition.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/ndb_binlog_index.MYD to /apps/backup/xtrabackup/mysql/ndb_binlog_index.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/columns_priv.frm to /apps/backup/xtrabackup/mysql/columns_priv.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_relation.MYD to /apps/backup/xtrabackup/mysql/help_relation.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/plugin.MYD to /apps/backup/xtrabackup/mysql/plugin.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/user.MYI to /apps/backup/xtrabackup/mysql/user.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/user.MYD to /apps/backup/xtrabackup/mysql/user.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_category.MYD to /apps/backup/xtrabackup/mysql/help_category.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/slave_master_info.frm to /apps/backup/xtrabackup/mysql/slave_master_info.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_leap_second.MYI to /apps/backup/xtrabackup/mysql/time_zone_leap_second.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/procs_priv.MYI to /apps/backup/xtrabackup/mysql/procs_priv.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/tables_priv.MYI to /apps/backup/xtrabackup/mysql/tables_priv.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_name.MYD to /apps/backup/xtrabackup/mysql/time_zone_name.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/proc.MYI to /apps/backup/xtrabackup/mysql/proc.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/tables_priv.MYD to /apps/backup/xtrabackup/mysql/tables_priv.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_transition_type.frm to /apps/backup/xtrabackup/mysql/time_zone_transition_type.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_leap_second.frm to /apps/backup/xtrabackup/mysql/time_zone_leap_second.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_topic.MYI to /apps/backup/xtrabackup/mysql/help_topic.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/plugin.MYI to /apps/backup/xtrabackup/mysql/plugin.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_keyword.frm to /apps/backup/xtrabackup/mysql/help_keyword.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/db.frm to /apps/backup/xtrabackup/mysql/db.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone.MYD to /apps/backup/xtrabackup/mysql/time_zone.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/proxies_priv.frm to /apps/backup/xtrabackup/mysql/proxies_priv.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/slave_worker_info.frm to /apps/backup/xtrabackup/mysql/slave_worker_info.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/db.MYD to /apps/backup/xtrabackup/mysql/db.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_topic.MYD to /apps/backup/xtrabackup/mysql/help_topic.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_name.frm to /apps/backup/xtrabackup/mysql/time_zone_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/ndb_binlog_index.frm to /apps/backup/xtrabackup/mysql/ndb_binlog_index.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/event.MYD to /apps/backup/xtrabackup/mysql/event.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/columns_priv.MYD to /apps/backup/xtrabackup/mysql/columns_priv.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/plugin.frm to /apps/backup/xtrabackup/mysql/plugin.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/event.frm to /apps/backup/xtrabackup/mysql/event.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/slow_log.frm to /apps/backup/xtrabackup/mysql/slow_log.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/slow_log.CSM to /apps/backup/xtrabackup/mysql/slow_log.CSM
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/db.MYI to /apps/backup/xtrabackup/mysql/db.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/ndb_binlog_index.MYI to /apps/backup/xtrabackup/mysql/ndb_binlog_index.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_relation.MYI to /apps/backup/xtrabackup/mysql/help_relation.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/func.MYI to /apps/backup/xtrabackup/mysql/func.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_relation.frm to /apps/backup/xtrabackup/mysql/help_relation.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_keyword.MYI to /apps/backup/xtrabackup/mysql/help_keyword.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/servers.MYD to /apps/backup/xtrabackup/mysql/servers.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/innodb_index_stats.frm to /apps/backup/xtrabackup/mysql/innodb_index_stats.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone.MYI to /apps/backup/xtrabackup/mysql/time_zone.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/func.frm to /apps/backup/xtrabackup/mysql/func.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/tables_priv.frm to /apps/backup/xtrabackup/mysql/tables_priv.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_keyword.MYD to /apps/backup/xtrabackup/mysql/help_keyword.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_name.MYI to /apps/backup/xtrabackup/mysql/time_zone_name.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/proxies_priv.MYI to /apps/backup/xtrabackup/mysql/proxies_priv.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/user.frm to /apps/backup/xtrabackup/mysql/user.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/general_log.CSM to /apps/backup/xtrabackup/mysql/general_log.CSM
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_transition.MYD to /apps/backup/xtrabackup/mysql/time_zone_transition.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/general_log.frm to /apps/backup/xtrabackup/mysql/general_log.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/columns_priv.MYI to /apps/backup/xtrabackup/mysql/columns_priv.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/slave_relay_log_info.frm to /apps/backup/xtrabackup/mysql/slave_relay_log_info.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/event.MYI to /apps/backup/xtrabackup/mysql/event.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/func.MYD to /apps/backup/xtrabackup/mysql/func.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_transition_type.MYD to /apps/backup/xtrabackup/mysql/time_zone_transition_type.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/servers.frm to /apps/backup/xtrabackup/mysql/servers.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/help_topic.frm to /apps/backup/xtrabackup/mysql/help_topic.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_leap_second.MYD to /apps/backup/xtrabackup/mysql/time_zone_leap_second.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/proc.frm to /apps/backup/xtrabackup/mysql/proc.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/servers.MYI to /apps/backup/xtrabackup/mysql/servers.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/proxies_priv.MYD to /apps/backup/xtrabackup/mysql/proxies_priv.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/general_log.CSV to /apps/backup/xtrabackup/mysql/general_log.CSV
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/procs_priv.MYD to /apps/backup/xtrabackup/mysql/procs_priv.MYD
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./mysql/time_zone_transition.MYI to /apps/backup/xtrabackup/mysql/time_zone_transition.MYI
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_waits_history_long.frm to /apps/backup/xtrabackup/performance_schema/events_waits_history_long.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_stages_summary_by_host_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_stages_summary_by_host_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_stages_summary_by_user_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_stages_summary_by_user_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_stages_summary_global_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_stages_summary_global_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/file_summary_by_instance.frm to /apps/backup/xtrabackup/performance_schema/file_summary_by_instance.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_waits_summary_by_account_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_waits_summary_by_account_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_stages_current.frm to /apps/backup/xtrabackup/performance_schema/events_stages_current.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/setup_actors.frm to /apps/backup/xtrabackup/performance_schema/setup_actors.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/setup_consumers.frm to /apps/backup/xtrabackup/performance_schema/setup_consumers.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_statements_history.frm to /apps/backup/xtrabackup/performance_schema/events_statements_history.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/setup_timers.frm to /apps/backup/xtrabackup/performance_schema/setup_timers.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_stages_history.frm to /apps/backup/xtrabackup/performance_schema/events_stages_history.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/threads.frm to /apps/backup/xtrabackup/performance_schema/threads.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 >> log scanned up to (2848651)
180624 19:01:56 [01] Copying ./performance_schema/socket_instances.frm to /apps/backup/xtrabackup/performance_schema/socket_instances.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/file_instances.frm to /apps/backup/xtrabackup/performance_schema/file_instances.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_waits_summary_by_thread_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_waits_summary_by_thread_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_statements_summary_by_user_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_statements_summary_by_user_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_statements_summary_by_account_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_statements_summary_by_account_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/performance_timers.frm to /apps/backup/xtrabackup/performance_schema/performance_timers.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/file_summary_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/file_summary_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/cond_instances.frm to /apps/backup/xtrabackup/performance_schema/cond_instances.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_stages_summary_by_account_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_stages_summary_by_account_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/table_io_waits_summary_by_index_usage.frm to /apps/backup/xtrabackup/performance_schema/table_io_waits_summary_by_index_usage.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/mutex_instances.frm to /apps/backup/xtrabackup/performance_schema/mutex_instances.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_statements_summary_by_digest.frm to /apps/backup/xtrabackup/performance_schema/events_statements_summary_by_digest.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_stages_summary_by_thread_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_stages_summary_by_thread_by_event_name.frm
180624 19:01:56 [01]        ...done
180624 19:01:56 [01] Copying ./performance_schema/events_statements_current.frm to /apps/backup/xtrabackup/performance_schema/events_statements_current.frm
180624 19:01:56 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/setup_objects.frm to /apps/backup/xtrabackup/performance_schema/setup_objects.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/objects_summary_global_by_type.frm to /apps/backup/xtrabackup/performance_schema/objects_summary_global_by_type.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/socket_summary_by_instance.frm to /apps/backup/xtrabackup/performance_schema/socket_summary_by_instance.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_stages_history_long.frm to /apps/backup/xtrabackup/performance_schema/events_stages_history_long.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_waits_summary_by_user_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_waits_summary_by_user_by_event_name.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_statements_summary_by_thread_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_statements_summary_by_thread_by_event_name.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_waits_history.frm to /apps/backup/xtrabackup/performance_schema/events_waits_history.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/db.opt to /apps/backup/xtrabackup/performance_schema/db.opt
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/table_lock_waits_summary_by_table.frm to /apps/backup/xtrabackup/performance_schema/table_lock_waits_summary_by_table.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/rwlock_instances.frm to /apps/backup/xtrabackup/performance_schema/rwlock_instances.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_statements_summary_by_host_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_statements_summary_by_host_by_event_name.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/host_cache.frm to /apps/backup/xtrabackup/performance_schema/host_cache.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_statements_summary_global_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_statements_summary_global_by_event_name.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/table_io_waits_summary_by_table.frm to /apps/backup/xtrabackup/performance_schema/table_io_waits_summary_by_table.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/setup_instruments.frm to /apps/backup/xtrabackup/performance_schema/setup_instruments.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_waits_summary_by_host_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_waits_summary_by_host_by_event_name.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_statements_history_long.frm to /apps/backup/xtrabackup/performance_schema/events_statements_history_long.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_waits_current.frm to /apps/backup/xtrabackup/performance_schema/events_waits_current.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_waits_summary_global_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/events_waits_summary_global_by_event_name.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/events_waits_summary_by_instance.frm to /apps/backup/xtrabackup/performance_schema/events_waits_summary_by_instance.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/socket_summary_by_event_name.frm to /apps/backup/xtrabackup/performance_schema/socket_summary_by_event_name.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/session_account_connect_attrs.frm to /apps/backup/xtrabackup/performance_schema/session_account_connect_attrs.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/users.frm to /apps/backup/xtrabackup/performance_schema/users.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/session_connect_attrs.frm to /apps/backup/xtrabackup/performance_schema/session_connect_attrs.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/hosts.frm to /apps/backup/xtrabackup/performance_schema/hosts.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./performance_schema/accounts.frm to /apps/backup/xtrabackup/performance_schema/accounts.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./test/test001.frm to /apps/backup/xtrabackup/test/test001.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./test/db.opt to /apps/backup/xtrabackup/test/db.opt
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./jfedu/t2_bak.frm to /apps/backup/xtrabackup/jfedu/t2_bak.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./jfedu/db.opt to /apps/backup/xtrabackup/jfedu/db.opt
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./zhjsdb01/v_test01.frm to /apps/backup/xtrabackup/zhjsdb01/v_test01.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./zhjsdb01/db.opt to /apps/backup/xtrabackup/zhjsdb01/db.opt
180624 19:01:57 [01]        ...done
180624 19:01:57 [01] Copying ./zhjsdb01/test01.frm to /apps/backup/xtrabackup/zhjsdb01/test01.frm
180624 19:01:57 [01]        ...done
180624 19:01:57 Finished backing up non-InnoDB tables and files
180624 19:01:57 [00] Writing /apps/backup/xtrabackup/xtrabackup_binlog_info
180624 19:01:57 [00]        ...done
180624 19:01:57 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '2848651'
xtrabackup: Stopping log copying thread.
.180624 19:01:57 >> log scanned up to (2848651)

180624 19:01:57 Executing UNLOCK TABLES
180624 19:01:57 All tables unlocked
180624 19:01:57 Backup created in directory '/apps/backup/xtrabackup/'
MySQL binlog position: filename 'binlog.000001', position '522'
180624 19:01:57 [00] Writing /apps/backup/xtrabackup/backup-my.cnf
180624 19:01:57 [00]        ...done
180624 19:01:57 [00] Writing /apps/backup/xtrabackup/xtrabackup_info
180624 19:01:57 [00]        ...done
xtrabackup: Transaction log of lsn (2848651) to (2848651) was copied.
180624 19:01:57 completed OK!
[[email protected] xtrabackup]# 

7、全備完成後,我們在進行資料變動,備份完成的變動後面就需要通過binlog日誌進行恢復了。

mysql> insert into test001 values(3,'CCCCC');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test001 ;
+------+-------+
| id   | name  |
+------+-------+
|    1 | AAAA  |
|    2 | BBBB  |
|    3 | CCCCC |
+------+-------+
3 rows in set (0.00 sec)

8、下面就可以進行資料庫誤操作了,我們可以把資料庫檔案全部清掉。

檢視下當前資料庫檔案位置

mysql> show variables like '%datadir%';
+---------------+-------------------+
| Variable_name | Value             |
+---------------+-------------------+
| datadir       | /u01/my3306/data/ |
+---------------+-------------------+
1 row in set (0.00 sec)

然後直接rm -rf掉:

[[email protected] ~]# cd /u01/my3306/data/
[[email protected] data]# ll
總用量 4157484
-rw-rw----. 1 mysql mysql         56 6月  23 23:50 auto.cnf
-rw-r-----. 1 mysql mysql        497 6月  23 23:49 backup-my.cnf
-rw-r-----. 1 mysql mysql   33554432 6月  23 23:49 ibdata1
-rw-r-----. 1 mysql mysql   16777216 6月  23 23:49 ibdata2
-rw-r-----. 1 mysql mysql 1048576000 6月  23 23:49 ib_logfile0
-rw-r-----. 1 mysql mysql 1048576000 6月  23 23:49 ib_logfile1
-rw-r-----. 1 mysql mysql 1048576000 6月  23 23:49 ib_logfile2
-rw-r-----. 1 mysql mysql 1048576000 6月  23 23:50 ib_logfile3
-rw-r-----. 1 mysql mysql   12582912 6月  23 23:50 ibtmp1
drwxr-x---. 2 mysql mysql       4096 6月  23 23:50 jfedu
drwxr-x---. 2 mysql mysql       4096 6月  23 23:50 mysql
drwxr-x---. 2 mysql mysql       4096 6月  23 23:50 performance_schema
drwxr-x---. 2 mysql mysql       4096 6月  24 18:59 test
drwxr-x---. 2 mysql mysql       4096 6月  23 23:50 zhjsdb01
[[email protected] data]# rm -rf *
[[email protected] data]# ll
總用量 0

清的很徹底,一點不剩。

注意我的binlog日誌並在該資料檔案目錄,是單獨存放的,如果binlog日誌在此目錄,還要主要不要把binlog日誌給清掉了。

9、下面就可以進行恢復了:

開始恢復前先應用日誌:

[[email protected] data]# innobackupex --defaults-file=/apps/backup/xtrabackup/backup-my.cnf --apply-log --user=root --password='root123' /apps/backup/xtrabackup

--defaults-file:要指定備份集產生的配置檔案,這個地方要注意;

--apply-log:表示要應用日誌,把備份集的lsn恢復到一致性的狀態;

最後的目錄指定了備份集的位置;

這裡有必要說明一下,使用xtrabackup備份的時候,在備份期間,假如備份在晚上10:00:00開始,備份用了半個小時,在10:30:00(時:分:秒),那麼在這備份的半個小時以內,還是會有一些業務資料的變動,xtrabackup怎麼解決前面備份的lsn跟後面的不一致呢?它也會產生日誌,這個日誌就是記錄的在這個時間段所有的資料變動,那麼等到恢復的時候,執行--apply-log就是對產生的備份集,進行日誌(這個日誌是xtrabackup產生額外記錄的,並不是mysql自己的binlog日誌)應用,把所有的page(可以理解成oracle裡面的block塊)的lsn都弄到一致狀態,也就是把備份集恢復到最終在10:30:00這個時刻的狀態。

[[email protected] data]# innobackupex --defaults-file=/apps/backup/xtrabackup/backup-my.cnf --apply-log --user=root --password='root123' /apps/backup/xtrabackup
xtrabackup: recognized server arguments: --innodb_checksum_algorithm=innodb --innodb_log_checksum_algorithm=innodb --innodb_data_file_path=ibdata1:32M;ibdata2:16M:autoextend --innodb_log_files_in_group=4 --innodb_log_file_size=1048576000 --innodb_fast_checksum=0 --innodb_page_size=16384 --innodb_log_block_size=512 --innodb_undo_directory=. --innodb_undo_tablespaces=0 --server-id=101 --redo-log-version=0 
xtrabackup: recognized client arguments: --innodb_checksum_algorithm=innodb --innodb_log_checksum_algorithm=innodb --innodb_data_file_path=ibdata1:32M;ibdata2:16M:autoextend --innodb_log_files_in_group=4 --innodb_log_file_size=1048576000 --innodb_fast_checksum=0 --innodb_page_size=16384 --innodb_log_block_size=512 --innodb_undo_directory=. --innodb_undo_tablespaces=0 --server-id=101 --redo-log-version=0 
180624 19:06:11 innobackupex: Starting the apply-log operation

IMPORTANT: Please check that the apply-log run completes successfully.
           At the end of a successful apply-log run innobackupex
           prints "completed OK!".

innobackupex version 2.4.12 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 170eb8c)
xtrabackup: cd to /apps/backup/xtrabackup/
xtrabackup: This target seems to be not prepared yet.
InnoDB: Number of pools: 1
xtrabackup: xtrabackup_logfile detected: size=8388608, start_lsn=(2848651)
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup:   innodb_data_home_dir = .
xtrabackup:   innodb_data_file_path = ibdata1:32M;ibdata2:16M:autoextend
xtrabackup:   innodb_log_group_home_dir = .
xtrabackup:   innodb_log_files_in_group = 1
xtrabackup:   innodb_log_file_size = 8388608
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup:   innodb_data_home_dir = .
xtrabackup:   innodb_data_file_path = ibdata1:32M;ibdata2:16M:autoextend
xtrabackup:   innodb_log_group_home_dir = .
xtrabackup:   innodb_log_files_in_group = 1
xtrabackup:   innodb_log_file_size = 8388608
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
InnoDB: PUNCH HOLE support available
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Uses event mutexes
InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: Number of pools: 1
InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, total size = 100M, instances = 1, chunk size = 100M
InnoDB: Completed initialization of buffer pool
InnoDB: page_cleaner coordinator priority: -20
InnoDB: Highest supported file format is Barracuda.
InnoDB: The log sequence number 2799158 in the system tablespace does not match the log sequence number 2848651 in the ib_logfiles!
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: xtrabackup: Last MySQL binlog file position 522, file name binlog.000001
InnoDB: Creating shared tablespace for temporary tables
InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
InnoDB: File './ibtmp1' size is now 12 MB.
InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redo rollback segment(s) are active.
InnoDB: 5.7.19 started; log sequence number 2848651
InnoDB: xtrabackup: Last MySQL binlog file position 522, file name binlog.000001

xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2851939
InnoDB: Number of pools: 1
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup:   innodb_data_home_dir = .
xtrabackup:   innodb_data_file_path = ibdata1:32M;ibdata2:16M:autoextend
xtrabackup:   innodb_log_group_home_dir = .
xtrabackup:   innodb_log_files_in_group = 4
xtrabackup:   innodb_log_file_size = 1048576000
InnoDB: PUNCH HOLE support available
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Uses event mutexes
InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: Number of pools: 1
InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, total size = 100M, instances = 1, chunk size = 100M
InnoDB: Completed initialization of buffer pool
InnoDB: page_cleaner coordinator priority: -20
InnoDB: Setting log file ./ib_logfile101 size to 1000 MB
InnoDB: Progress in MB:
 100 200 300 400 500 600 700 800 900 1000
InnoDB: Setting log file ./ib_logfile1 size to 1000 MB
InnoDB: Progress in MB:
 100 200 300 400 500 600 700 800 900 1000
InnoDB: Setting log file ./ib_logfile2 size to 1000 MB
InnoDB: Progress in MB:
 100 200 300 400 500 600 700 800 900 1000
InnoDB: Setting log file ./ib_logfile3 size to 1000 MB
InnoDB: Progress in MB:
 100 200 300 400 500 600 700 800 900 1000
InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
InnoDB: New log files created, LSN=2851939
InnoDB: Highest supported file format is Barracuda.
InnoDB: Log scan progressed past the checkpoint lsn 2852364
InnoDB: Doing recovery: scanned up to log sequence number 2852373 (0%)
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: xtrabackup: Last MySQL binlog file position 522, file name binlog.000001
InnoDB: Removed temporary tablespace data file: "ibtmp1"
InnoDB: Creating shared tablespace for temporary tables
InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
InnoDB: File './ibtmp1' size is now 12 MB.
InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redo rollback segment(s) are active.
InnoDB: page_cleaner: 1000ms intended loop took 40659ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
InnoDB: 5.7.19 started; log sequence number 2852373
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2852392
180624 19:06:55 completed OK!
[[email protected] data]# 

10、然後把具有lsn一致性的備份集直接拷貝到原mysql的data目錄中區:

[[email protected] ~]# cp -fr /apps/backup/xtrabackup/* /u01/my3306/data
[[email protected] ~]# chown -R mysql:mysql /u01/my3306/data/

11、進行到此,原來mysql服務並沒有停止,我們需要停止,然後在起來:

檢視當前mysql服務:

[[email protected] ~]# ps -ef|grep mysql
root      3400  2146  0 13:19 pts/0    00:00:00 su - mysql
mysql     3401  3400  0 13:19 pts/0    00:00:00 -bash
mysql     3435     1  0 13:20 pts/0    00:00:00 /bin/sh /u01/my3306/bin/mysqld_safe --defaults-file=/u01/my3306/my.cnf --user=mysql
mysql     4288  3435  0 13:20 pts/0    00:00:03 /u01/my3306/bin/mysqld --defaults-file=/u01/my3306/my.cnf --basedir=/u01/my3306 --datadir=/u01/my3306/data --plugin-dir=/u01/my3306/lib/plugin --log-error=/u01/my3306/log/error.log --open-files-limit=65535 --pid-file=/u01/my3306/run/mysqld.pid --socket=/u01/my3306/run/mysql.sock --port=3306
mysql     4325  3401  0 13:20 pts/0    00:00:00 mysql -uroot -p
root      4416  3343  0 13:47 pts/1    00:00:00 su - mysql
mysql     4417  4416  0 13:47 pts/1    00:00:00 -bash
mysql     5007  4417  0 18:40 pts/1    00:00:00 mysql -uroot -p
root      5183  5159  0 19:07 pts/4    00:00:00 grep mysql

停服務:

[[email protected] ~]# su - mysql
[[email protected] ~]$ cd /u01/my3306/
[[email protected] my3306]$ ls
bin               data     lib  my.cnf      README   share            stop_mysqld.sh  wf_backup
connect_mysql.sh  docs     log  my-new.cnf  run      sql-bench        support-files
COPYING           include  man  mysql-test  scripts  start_mysqld.sh  tmp
[[email protected] my3306]$ ./stop_mysqld.sh 
Warning: Using a password on the command line interface can be insecure.
[[email protected] my3306]$ ps -ef|grep mysql
root      3400  2146  0 13:19 pts/0    00:00:00 su - mysql
mysql     3401  3400  0 13:19 pts/0    00:00:00 -bash
mysql     4325  3401  0 13:20 pts/0    00:00:00 mysql -uroot -p
root      4416  3343  0 13:47 pts/1    00:00:00 su - mysql
mysql     4417  4416  0 13:47 pts/1    00:00:00 -bash
mysql     5007  4417  0 18:40 pts/1    00:00:00 mysql -uroot -p
root      5185  5159  0 19:08 pts/4    00:00:00 su - mysql
mysql     5186  5185  0 19:08 pts/4    00:00:00 -bash
mysql     5216  5186  1 19:08 pts/4    00:00:00 ps -ef
mysql     5217  5186  0 19:08 pts/4    00:00:00 grep mysql

在重新起服務:

[[email protected] my3306]$ ./start_mysqld.sh 
[[email protected] my3306]$ 180624 19:09:11 mysqld_safe Logging to '/u01/my3306/log/error.log'.
180624 19:09:11 mysqld_safe Starting mysqld daemon with databases from /u01/my3306/data

[[email protected] my3306]$ ps -ef|grep mysql
mysql     5219     1  0 19:09 pts/4    00:00:00 /bin/sh /u01/my3306/bin/mysqld_safe --defaults-file=/u01/my3306/my.cnf --user=mysql
mysql     6072  5219  0 19:09 pts/4    00:00:00 /u01/my3306/bin/mysqld --defaults-file=/u01/my3306/my.cnf --basedir=/u01/my3306 --datadir=/u01/my3306/data --plugin-dir=/u01/my3306/lib/plugin --log-error=/u01/my3306/log/error.log --open-files-limit=65535 --pid-file=/u01/my3306/run/mysqld.pid --socket=/u01/my3306/run/mysql.sock --port=3306

12、此時我們來驗證一下資料:

mysql> select * from test001 ;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    1
Current database: test

+------+------+
| id   | name |
+------+------+
|    1 | AAAA |
|    2 | BBBB |
+------+------+
2 rows in set (0.04 sec)

上面的報錯是因為我我們中間停過一次庫,所以原來的連線斷了自己重連了。

這裡的資料只是我們做全備之前的,並沒有我們做全備之後的那條id是3,name是CCCCC的資料。

後面我們將利用binlog日誌進行全備之後的資料恢復。

13、我們的目標應該是要找到在備份之後做的產生的日誌的pos點。因為我們這邊總共就兩個日誌,很好找,我們可以全量的找。

先看下當前所有日誌的情況:

mysql> show master logs ;
+---------------+-----------+
| Log_name      | File_size |
+---------------+-----------+
| binlog.000001 |       747 |
| binlog.000002 |       120 |
+---------------+-----------+
2 rows in set (0.00 sec)

mysql> show master status ;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      120 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

目前一共兩個日誌,binlog.000002是當前日誌。

對這 兩個日誌進行格式化輸出:

[[email protected] ~]# su - mysql
[[email protected] ~]$ cd /u01/my3306/log/binlog 
[[email protected] binlog]$ ls
binlog.000001  binlog.000002  binlog.index
[[email protected] binlog]$ mysqlbinlog --base64-output=decode-rows -vv /u01/my3306/log/binlog/binlog.000001 /u01/my3306/log/binlog/binlog.000002  >/tmp/1.sql
[[email protected] binlog]$

我們指定兩個日誌檔案:binlog.000001,binlog.000002進行格式化輸出,輸出內容寫到/tmp/1.sql檔案中,我們可以看下/tmp/1.sql檔案:

[[email protected] binlog]$ view /tmp/1.sql
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#180624 19:00:23 server id 101  end_log_pos 120 CRC32 0x8fb4ae18        Start: binlog v 4, server v 5.6.39-log created 180624 19:00:23 at startup
ROLLBACK/*!*/;
# at 120
#180624 19:00:40 server id 101  end_log_pos 192 CRC32 0x2a058264        Query   thread_id=7     exec_time=0     error_code=0
SET TIMESTAMP=1529838040/*!*/;
SET @@session.pseudo_thread_id=7/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1073741824/*!*/;
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 192                     #開始事務號,這個是第一次插入AAAA的記錄的時候的事務
#180624 19:00:40 server id 101  end_log_pos 245 CRC32 0xc922724c        Table_map: `test`.`test001` mapped to number 77
# at 245
#180624 19:00:40 server id 101  end_log_pos 290 CRC32 0x8123baef        Write_rows: table id 77 flags: STMT_END_F
### INSERT INTO `test`.`test001`
### SET
###   @1=1 /* INT meta=0 nullable=1 is_null=0 */
###   @2='AAAA' /* VARSTRING(10) meta=10 nullable=1 is_null=0 */      #這邊可以看到插入的兩個欄位值,1和'AAAA'
# at 290
#180624 19:00:40 server id 101  end_log_pos 321 CRC32 0x1878a6e6        Xid = 147
COMMIT/*!*/;                            #事務結束,自動提交
# at 321
#180624 19:00:46 server id 101  end_log_pos 393 CRC32 0xaae2b9c3        Query   thread_id=7     exec_time=0     error_code=0
SET TIMESTAMP=1529838046/*!*/;
BEGIN
/*!*/;
# at 393                #插入第二條記錄的事務開始號 
#180624 19:00:46 server id 101  end_log_pos 446 CRC32 0xb429c44e        Table_map: `test`.`test001` mapped to number 77
# at 446
#180624 19:00:46 server id 101  end_log_pos 491 CRC32 0xbc0020d4        Write_rows: table id 77 flags: STMT_END_F
### INSERT INTO `test`.`test001`
### SET
###   @1=2 /* INT meta=0 nullable=1 is_null=0 */           #看到插入的記錄的兩個欄位值2和'BBBB'
###   @2='BBBB' /* VARSTRING(10) meta=10 nullable=1 is_null=0 */
# at 491
#180624 19:00:46 server id 101  end_log_pos 522 CRC32 0x1daf52ea        Xid = 148
COMMIT/*!*/;                  #第二個事務自動提交結束
# at 522
#180624 19:04:56 server id 101  end_log_pos 594 CRC32 0x6c807299        Query   thread_id=7     exec_time=0     error_code=0
SET TIMESTAMP=1529838296/*!*/;
BEGIN
/*!*/;
# at 594                #插入第三條記錄的開始事務號 這個就是全備以後日誌起始地方,要記住,後面恢復要用
#180624 19:04:56 server id 101  end_log_pos 647 CRC32 0x48614ae6        Table_map: `test`.`test001` mapped to number 81
# at 647
#180624 19:04:56 server id 101  end_log_pos 693 CRC32 0x01ea9114        Write_rows: table id 81 flags: STMT_END_F
### INSERT INTO `test`.`test001`
### SET
###   @1=3 /* INT meta=0 nullable=1 is_null=0 */            #可以看到插入的兩個欄位值3和'CCCCC',也就是我們全備後的日誌開始的地方
###   @2='CCCCC' /* VARSTRING(10) meta=10 nullable=1 is_null=0 */
# at 693
#180624 19:04:56 server id 101  end_log_pos 724 CRC32 0x228ad586        Xid = 169
COMMIT/*!*/;         #第三個事務結束,自動提交了
# at 724
#180624 19:08:40 server id 101  end_log_pos 747 CRC32 0xe850edf9        Stop
# at 4      #這個地方日誌的pos開始變小了,說明日誌切換了,這個pos是00002日誌裡面的位置
#180624 19:09:11 server id 101  end_log_pos 120 CRC32 0x0fd01ca0        Start: binlog v 4, server v 5.6.39-log created 180624 19:09:11 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET [email protected]_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[[email protected] binlog]$ 

由此可見,我們需要回復的應該是在pos=594開始的事務以及以後的日誌記錄。

[[email protected] ~]# su - mysql
[[email protected] ~]$ mysqlbinlog /u01/my3306/log/binlog/binlog.000001 /u01/my3306/log/binlog/binlog.000002 --start-position=594 --stop-position=4 > /tmp/2.sql

其中

--start-position=594表示是從binlog.000001日誌的pos=594的地方開始恢復;

--stop-position=4表示是恢復停止於binlog.000002日誌的pos=4的地方;

日誌規整後輸出到/tmp/2.sql檔案中,後面我們將直接利用這個檔案進行恢復。

14、恢復:

[[email protected] ~]$ mysql -uroot -proot123 < /tmp/2.sql 
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]$ 

15、恢復完成後,我們去檢視下測試表資料是否回來了:

mysql> select * from test001 ;
+------+-------+
| id   | name  |
+------+-------+
|    1 | AAAA  |
|    2 | BBBB  |
|    3 | CCCCC |
+------+-------+
3 rows in set (0.00 sec)

mysql> 
發現數據已經回來了。

16、關於binlog開啟說明:

5.6和5.7版本的MySQL,有個引數binlog_row_image,預設值為FULL,表示記錄的是全部的binlog操作日誌(僅在binlog_format=ROW時候生效)。此外binlog_row_image還可以是minimal,表示binlog記錄的就只是影響後的行。如此一來使用ROW格式就能節約很多的磁碟空間。

mysql> show variables like '%log%' ;
+-----------------------------------------+-------------------------------------+
| Variable_name                           | Value                               |
+-----------------------------------------+-------------------------------------+
| binlog_format                           | ROW                                 |
| binlog_row_image                        | FULL                                |

上面是擷取的指定兩條,是預設的情況。

相關推薦

mysql5.6使用xtrabackupbinlog進行完全恢復

1、連輸mysql,檢視mysql版本:[[email protected] binlog]$ mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; o

利用binlog進行日誌恢復

過濾檢視記錄 [[email protected]]# /usr/bin/mysqlbinlog --no-defaults /var/lib/mysql/mysql-bin.000001 | more 全部執行 [[email protected]

Mysql 通過全量備份binlog恢復整體數據

mysql 通過全量備份和binlog恢復整體數據 某天工作時間,一個二貨犯暈登錯生產當測試環境了,直接drop了一個數據庫,需要緊急恢復!可利用備份的數據文件以及增量的 binlog 文件進行數據恢復。具體思路歸納幾點:1、恢復條件為 MySQL 要開啟 binlog 日誌功能,並且要全備和增量的所有

MySQL第四天(用戶授權與撤銷,完全備份完全恢復

運維day04一、用戶授權與撤銷數據庫管理員密碼設置?1.1修改數據庫管理員本機登錄密碼[root@db2 ~]# mysqladmin -hlocalhost -uroot -p password "123456"Enter password: mysqladmin: [Warning]

mysql 開發進階篇系列 43 邏輯備份與恢復(基於時間位置的不完全恢復)

一. 概述          在上篇講到了邏輯備份,使用mysqldump工具來備份一個庫,並使用完全恢復還原了資料庫。在結尾也講到了誤操作是不能用完全恢復的。解決辦法是:我們需要恢復到誤操作之前的狀態,然後跳過誤操作語句。再恢復後面執行的語句,完成我們的恢復,這種恢復叫“不完全恢復”。在mysql 中,不完

利用xtrabackupbinlog增量恢復時提示表記錄不存在案例

一.基本資訊版本:10.0.20-MariaDB-log 資料檔案和redo log位置:/apps/dbdat/mariadb10_data3306/ bing log位置:/apps/dbdat/mariadb10_data3306/log 主機資訊:mvxl0784

Mysql_innobackupex全備份+binlog進行恢復(可用)

準備備份檔案:全備份+binlog進行恢復:配置好MySQL解壓MySQL的安裝檔案,把安裝檔案的bin目錄配置到環境變數檔案裡面把/usr/local/mysql/bin新增到PATH路徑下。[[email protected] ~]# cat /root/.

MySQL利用frmidb檔案進行資料恢復

我們知道MySQL中如果用的是MYISAM資料引擎,那麼資料很好恢復,只要將相應.frm, .MYD, .MYI檔案拷貝過去即可。但是如果是innodb的話,如果開啟innodb_file_per_t

使用閉包代理Segue進行反向傳值

closure create 賦值 返回 protocol alt 類型 del uiview import UIKit class FirstViewController: UIViewController, SecondViewControllerDelegate

Oracle 表表數據恢復

sa1. 表恢復 對誤刪的表,只要沒有使用 purge 永久刪除選項,那麽基本上是能從 flashback table 區恢復回來的。 數據表和其中的數據都是可以恢復回來的,記得 flashback table 是從 Oralce 10g 提供的,一般步驟有: a.從 flashback tabl

用PHPAjax進行前後臺數據交互——以用戶登錄為例

serial click ots 多網站 頁面 用戶註冊 index -s password 很多網站中都有用戶登錄系統,要完成用戶的註冊和登陸,就一定要用到前後臺的數據交互。在這裏以簡單的用戶註冊和登陸為例介紹一下前後臺交互的大致流程。 首先,我們來做一個簡單的登陸界

yum安裝mysql5.75.6

mysql通常yum安裝mysql-server時,mysql版本都比較低,如果想安裝較高版本,可以導入官方的yum源來安裝安裝mysql5.71、下載官方的yum源並導入下載地址:http://dev.mysql.com/downloads/repo/yum/ 2、導入yum源:1rpm -ivh mysq

把編譯安裝的httpd 實現服務腳本,通過servicechkconfig 進行管理

成功 服務腳本 文件內容 roc grep sharp list roo httpd 把編譯安裝的httpd 實現服務腳本,通過service和chkconfig 進行管理 1 編譯安裝httpd 把httpd編譯安裝在/app/httpd/目錄下。 2 在/e

使用djangovue進行數據交互

com 狀態 method tag comm 文章 json tor 目錄 一、前端請求的封裝 1.將請求地址封裝起來,以便日後修改,在src/assets/js目錄下創建getPath.js文件 export default function getUrl(str) {

三級聯動使用Jquerybootstrap進行布局

head .com ont red app urn 使用 文件內容 ots 網頁的html代碼 <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <titl

使用GDBGEF進行調試

整數 lsp asp 十進制 啟動 space 技術分享 commands 指針 使用GDB進行調試 這是編譯ARM二進制文件和使用GDB進行基本調試的簡單介紹。在您按照教程進行操作時,您可能需要按照自己的習慣使用ARM程序集。在這種情況下,你要麽需要一個備用的A

使用AsyncAwait進行異步編程(C#版 適用於VS2015)

send click cli inpu 成員 出錯 obj aging ros 你可以使用異步編程來避免你的應用程序的性能瓶頸並且加強總體的響應。然而,用傳統的技術來寫異步應用是復雜的,同時編寫,調試和維護都很困難。VS2012介紹了簡單的方法,那就是異步編程,它在.Net

centos7centos6.5環境rpm方式安裝mysql5.7mysql5.6詳解

安裝mysql5.7和mysql5.6詳centos7和centos6.5環境rpm方式安裝mysql5.7和mysql5.6詳解centos環境安裝mysql5.7其實不建議安裝mysql5.7 語法和配置可能和以前的版本區別較大,多坑,慎入1.yum方式安裝(不推薦)a.安裝mysql5.7 yum源ce

xtrabackup全備操作誤刪備份恢復操作

href strong -h 正常 backup 如果 x86 dir 一致性 1.安裝percona源rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_6

NBU恢復oracle時,RACHA不同的恢復設置

描述 文檔 若是 服務器 服務 而在 註意 相同 網上 之前恢復RAC和HA的時候,註意到了兩者在恢復過程中設置的不同,但是沒有記錄下來,今天在恢復的時候竟然有點忘了,趕緊記錄,用於以後恢復測試的時候幫助回憶。RAC和HA的區別和共同點可以在網上有許多文檔敘述,這裏就不贅述