Mysql backup and recovery

Source: Internet
Author: User
To ensure data security, data must be backed up regularly. There are many backup methods and the effects are different. Once an error occurs in the data in the database, you must use the backup data for restoration. In this way

To ensure data security, data must be backed up regularly. There are many backup methods and the effects are different. Once an error occurs in the data in the database, you must use the backup data for restoration. In this way

2.3 Process Implementation

(1) Mysqldump full backup

Because the Mysql database uses the MyISAM storage engine by default, it only uses warm backup (Backup only supports read requests). Therefore, we need to add read locks for all databases.

[Root @ stu18 ~] # Mysqldump-uroot-pmypass -- lock-all-tables -- master-data = 2 -- events -- routines -- all-databases>/zhao/database _ 'date + % F'. SQL

Resolution: -- lock-all-tables indicates that the read lock is applied to all tables; -- master-data = 2 indicates that the current binary log location is recorded in the backup file; -- events indicates the code of the Time Scheduler for backing up data at the same time; -- routines indicates the stored procedure and storage function for backing up data at the same time; -- all-databases indicates backing up all databases.

[Root @ stu18 zhao] # less database_2013-08-13. SQL -- # indicate annotation item -- Position to start replication or point-in-time recovery from ---- CHANGE MASTER TO MASTER_LOG_FILE = 'mysql-bin.000001 ', MASTER_LOG_POS = 14203; # Here it indicates that the Current is in the binary log of the mysql-bin.000001, the event is 14203 which is generated through -- master-data = 2 ---- Current Database: 'hellodb' -- create database /*! 32312 if not exists */'hellodb '/*! 40100 default character set utf8 */;

(2) binary full backup

Method 1: Export binary log file content

[Root @ stu18 data] # mysqlbinlog mysql-bin.000001>/zhao/binlog _ 'date + % F'. SQL

Method 2: copy a file by rolling logs

Mysql> flush logs; # Rolling logs [root @ stu18 data] # cp mysql-bin.000001/zhao/mysql-bin.000001 # copy and export binary files

(3) binary Incremental Backup

First add data information

Mysql> use hellodb; mysql> insert into students (Name, Age, Gender, ClassID, TeacherID) values ('yang kang ', 22, 'M', 3, 3 );

Then add binary data

[Root @ stu18 data] # mysqlbinlog -- start-position = 14203 -- stop-position = 14527 mysql-bin.000001>/zhao/binlog _ 'date + % F _ % H'. SQL

Resolution: -- start-position = 14203 indicates the location of the binary event after the last full backup; -- stop-position = 14527 indicates the location of the binary event on the last day.

2.4 simulate database corruption to achieve recovery

Mysql> drop database hellodb; # delete a database ########### the following processes must be executed offline ############# mysql> SET SQL _log_bin = 0; # First disable the binary log mysql> flush logs; # Rolling log [root @ stu18 ~] # Mysql-uroot-pmypass </zhao/database_2013-08-13. SQL # import database backup file [root @ stu18 ~] # Mysql-uroot-pmypass </zhao/binlog_2013-08-13_19. SQL # import Incremental backup file [root @ stu18 ~] # Mysql-uroot-pmypass # log on to the system and check whether mysql> SET SQL _log_bin = 1; # enable binary log.

(1) Create and mount a logical volume. This process is not demonstrated here.

(2) initialize mysql to direct its data directory to/mydata/data

[Root @ stu18 ~] # Cd/usr/local/mysql/[root @ stu18 mysql] # scripts/mysql_install_db -- user = mysql -- datadir =/mydata/data

(3) edit and view the configuration file and restart the service.

[Root @ stu18 mysql] # vim/etc/my. cnfdatadir =/mydata/data # check whether this item defines the data directory location sync_binlog = 1 # add this item. When each transaction is committed, the transaction log is written from the cache to the log file, and refresh the log file data to the disk. [root @ stu18 mysql] # service mysqld start

3.4 process display

(1) Ensure that the transaction log and data file must be on the same volume

[Root @ stu18 ~] # Ls/mydata/data/hellodbmyclassmysql-bin.000003 stu18.magedu.com. erribdata1mysqlmysql-bin.000004 stu18.magedu.com. pidib_logfile0 mysql-bin.000001 mysql-bin.indexstudentib_logfile1 mysql-bin.000002 performance_schema test

Resolution: ib_logfile0 and ib_logfile1 are log files.

(2) apply a global lock and scroll logs

Mysql> flush tables with read lock; mysql> flush logs;

(3) view and save the currently used binary log and the location of the currently executed binary log (very important)

Mysql> show master status; + metric + ---------- + -------------- + ---------------- + | File | Position | Binlog_Do_DB | usage | + usage + ---------- + -------------- + usage + | mysql-bin.000004 | 187 | + usage + -------- + -------------- + ------------------ + [root @ stu18 zhao] # mysql-uroot-pmypass-e 'show master status; '>/zhao/lvmback-2013-08-14/binlog.txt

(4) create a snapshot volume

[Root @ stu18 zhao] # lvcreate-L 100 M-s-p r-n mydata-lvm/dev/vg1/mydata

(5) Immediately switch the terminal to release the lock

Mysql> unlock tables;

(6) Back up data

[Root @ stu18 data] # cp-a */zhao/lvmback-2013-08-14/

(7) binary Incremental Backup

Mysql> use hellodb; # specify the default Database changedmysql> create table testtb (id int, name CHAR (10); # CREATE a TABLE Query OK, 0 rows affected (0.35 sec) mysql> insert into testtb VALUES (1, 'Tom '); # add data Query OK, 1 row affected (0.09 sec) [root @ stu18 data] # mysqlbinlog -- start-position = 187 mysql-bin.000004>/zhao/lvmlogbin_2013-08-14/binlog. SQL # incremental log backup

(8) simulate database crash

[Root @ stu18 ~] # Service mysqld stop [root @ stu18 ~] # Cd/mydata/data/[root @ stu18 data] # rm-rf *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.