This article mainly introduces how to ensure data consistency between MySQL backup and recovery. if you are interested, refer to the hot copy (Hot Copy of MySQL backup and recovery) mentioned in the previous article ), hot copy is to back up data using the mysqlhotcopy command when MySQL or other database services are running. This article explains how to ensure data consistency. Now let's assume that there is a situation where we always back up the database in the early hours of the morning, assuming that a database exception occurs after the early hours of the morning and data loss occurs. In this way, we have backed up the data before the early morning, but the data from the early morning to the abnormal time will be lost (without binlog ). Fortunately, the InnoDB storage engine supports transactions and Binlog. data generated during the period from the early morning to the time when an exception occurs can be backed up through log files. Therefore, log files are very important and critical. We need to not only back up data, but also back up binary files if conditions permit. Of course, after you back up the data, you can clear the binary file. but for the long term, for example, if the recovered data is not what we want, we need to back up the binary file. Remember that data recovery must be transferred to the test database instead of in the production environment. If there is no problem in the Test Database, do it in the production environment.
Data consistency simulation
Step 1: verify the data
[root@serv01 databackup]# rm -rf *[root@serv01 databackup]# lsmysql> use larrydb;Database changedmysql> show tables;+-------------------+| Tables_in_larrydb |+-------------------+| class || stu |+-------------------+2 rows in set (0.00 sec)mysql> select * from class;+------+--------+| cid | cname |+------+--------+| 1 | linux || 2 | oracle |+------+--------+2 rows in set (0.00 sec)mysql> select * from stu;+------+---------+------+| sid | sname | cid |+------+---------+------+| 1 | larry01 | 1 || 2 | larry02 | 2 |+------+---------+------+2 rows in set (0.00 sec)
Step 2: back up data
[root@serv01 databackup]# mysqldump -uroot -p123456 --database larrydb > larrydb.sql[root@serv01 databackup]# ll larrydb.sql -rw-r--r--. 1 root root 2613 Sep 10 19:34 larrydb.sql
Step 3: clear logs. because the logs have been backed up, the previous logs are not needed.
mysql> show binary logs;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000001 | 27320 || mysql-bin.000002 | 1035309 || mysql-bin.000003 | 1010 || mysql-bin.000004 | 22809 || mysql-bin.000005 | 9860 || mysql-bin.000006 | 5659 || mysql-bin.000007 | 126 || mysql-bin.000008 | 10087 || mysql-bin.000009 | 8293 || mysql-bin.000010 | 476 || mysql-bin.000011 | 218 || mysql-bin.000012 | 126 || mysql-bin.000013 | 1113 || mysql-bin.000014 | 1171 || mysql-bin.000015 | 126 || mysql-bin.000016 | 107 || mysql-bin.000017 | 107 || mysql-bin.000018 | 13085 |+------------------+-----------+18 rows in set (0.00 sec)mysql> reset master;Query OK, 0 rows affected (0.01 sec)mysql> show binary logs;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000001 | 107 |+------------------+-----------+1 row in set (0.00 sec)
Step 4: update data
mysql> insert into class values(3,'Devel');Query OK, 1 row affected (0.01 sec)mysql> update class set cname="dab" where cid=2;Query OK, 1 row affected (0.01 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from class;+------+-------+| cid | cname |+------+-------+| 1 | linux || 2 | dab || 3 | Devel |+------+-------+3 rows in set (0.00 sec)mysql> select * from stu;+------+---------+------+| sid | sname | cid |+------+---------+------+| 1 | larry01 | 1 || 2 | larry02 | 2 |+------+---------+------+2 rows in set (0.00 sec)mysql> delete from stu where cid=2;Query OK, 1 row affected (0.00 sec)mysql> update stu set sname="larry007" where sid=1;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from stu;+------+----------+------+| sid | sname | cid |+------+----------+------+| 1 | larry007 | 1 |+------+----------+------+1 row in set (0.00 sec)[root@serv01 data]# dateTue Sep 10 19:38:24 CST 2013
Step 5: simulate data loss and delete the database
[Root @ serv01 data] # rm-rf/usr/local/mysql/data/larrydb/mysql> show databases; + -------------------- + | Database | + -------------------- + | information_schema | game | hello | mnt | mysql | performance_schema | test | + ------------------ + 7 rows in set (0.00 sec) [root @ serv01 data] # cd/usr/local/mysql/data/[root @ serv01 data] # lltotal 28736drwx ------. 2 mysql 4096 Sep 10 gamedrwx ------. 2 mysql 4096 Sep 7 hello-rw ----. 1 mysql 18874368 Sep 10 ibdata1-rw-rw ----. 1 mysql 5242880 Sep 10 ib_logfile0-rw-rw ----. 1 mysql 5242880 Sep 4 ib_logfile1drwxr-xr-x. 2 mysql 4096 Sep 10 mntdrwxr-xr-x. 2 mysql 4096 Sep 4 mysql-rw ----. 1 mysql 998 Sep 10 mysql-bin.000001-rw-rw ----. 1 mysql 19 Sep 10 mysql-bin.indexdrwx ------. 2 mysql 4096 Sep 4 performance_schema-rw-r -----. 1 mysql root 26371 Sep 10 serv01.host.com. err-rw ----. 1 mysql 5 Sep 10 18:36 serv01.host.com. piddrwx ------. 2 mysql 4096 Sep 7 test # you can use the mysqlbinlog command to view the log file [root @ serv01 data] # mysqlbinlog mysql-bin.000001mysql> show databases; + -------------------- + | Database | + -------------------- + | information_schema | game | hello | mnt | mysql | performance_schema | test | + ------------------ + 7 rows in set (0.00 sec) mysql> drop database larrydb; Query OK, 0 rows affected (0.01 sec)
Step 6: import the updated data
[root@serv01 databackup]# mysql -uroot -p123456 < larrydb.sql ERROR 1050 (42S01) at line 33: Table '`larrydb`.`class`' already exists[root@serv01 databackup]# mysql -uroot -p123456 < larrydb.sql mysql> use larrydb;Database changedmysql> select * from stu;+------+---------+------+| sid | sname | cid |+------+---------+------+| 1 | larry01 | 1 || 2 | larry02 | 2 |+------+---------+------+2 rows in set (0.00 sec)mysql> select * from class;+------+--------+| cid | cname |+------+--------+| 1 | linux || 2 | oracle |+------+--------+2 rows in set (0.00 sec)
Step 7: restore data based on logs
[Root @ serv01 data] # mysqlbinlog -- stop-datetime "19:37:45" mysql-bin.000001 | mysql-uroot-p123456mysql> select * from stu; + ------ + --------- + ------ + | sid | sname | cid | + ------ + --------- + ------ + | 1 | larry01 | 1 | + ------ + --------- + ------ + 1 row in set (0.00 sec) mysql> select * from class; + ------ + ------- + | cid | cname | + ------ + ------- + | 1 | linux | 2 | dab | 3 | Devel | + ------ + ------- + 3 Rows in set (0.00 sec) # rule: the recovery time point (or the time point after Commit) is the accident point plus a second. [Root @ serv01 data] # mysqlbinlog -- stop-datetime "19:37:46" mysql-bin.000001 | mysql-uroot-p123456mysql> select * from stu; + ------ + ---------- + ------ + | sid | sname | cid | + ------ + ---------- + ------ + | 1 | larry007 | 1 | + ------ + ---------- + ------ + 1 row in set (0.00 sec) mysql> select * from class; + ------ + ------- + | cid | cname | + ------ + ------- + | 1 | linux | 2 | dab | 3 | Devel | 3 | Devel | + ------ + ------- + 4 rows in set (0.00 sec) [root @ serv01 data] # mysqlbinlog mysql-bin.000001 # at 7131 #130910 19:37:45 server id 1 end_log_pos 7240 Query thread_id = 20 exec_time = 996 error_code = 0 set timestamp = 1378813065 /*! */; Update stu set sname = "larry007" where sid = 1 /*! */; # At 7240 #130910 19:37:45 server id 1 end_log_pos 7312 Query thread_id = 20 exec_time = 996 error_code = 0 set timestamp = 1378813065 /*! */; COMMIT /*! */; DELIMITER; # End of log fileROLLBACK/* added by mysqlbinlog */;/*! 50003 SET COMPLETION_TYPE = @ OLD_COMPLETION_TYPE */;
The above is all the content of this article. I don't know if you have any gains. please contact the previous articles for understanding, so the learning effect will be better.