(1 Introduction
(2) Backup and recovery of damaged machines
Prerequisite: Full and incremental backups (binary log files) are required
1) Backup
You need to enable binary logging in advance
#vim /etc/my.cnf log-bin=/data/mydata/mysql-bin/master server-id=1 #mkdir /data/mydata/mysql-bin #chown -R mysql.mysql /data/mydata/mysql-bin #systemctl restart mysqld
Prepare data
mysql> create database testdb; mysql> create table testdb.test(id int); mysql> insert into testdb.test values(1);
full backup
Generate directory: ll /backup/2018-04-26-00-mysql-all.sqlmysqldump -uroot -pMysql@123 -A -R --single-transaction --master-data=1 --flush-logs >/backup/$(date +%F-%H)-mysql-all.sql \\需要提前创建好备份目录
Generate some data for recovery using binary logs
mysql> insert into testdb.test values(2); mysql> flush logs; mysql> insert into testdb.test values(2); mysql> insert into testdb.test values(3); mysql> flush logs; mysql> insert into testdb.test values(4); mysql> flush logs; mysql> select * from testdb.test; +------+ | id | +------+ | 1 | | 2 | | 2 | | 3 | | 4 | +------+
- Copy the binary files to other directories, I will put them in the /root directory first
cp -ar /data/mydata/mysql-bin/ /root Simulate database failure
rm -rf /var/lib/mysql/* systemctl stop mysqld chown -R mysql.mysql /var/lib/mysql systemctl start mysqld \\无法启动 rm -rf /var/lib/mysql/* \\再删除数据 systemctl start mysqld \\再次可以启动了 grep "password" /var/log/mysqld.log 2018-04-25T10:01:13.953346Z 1 [Note] A temporary password is generated for root@localhost: 9Mf3.k)AOgEd mysqladmin -uroot -p'9Mf3.k)AOgEd' password 'Mysql@123';
2) Recovery: pay attention to closing the binary log when recovering to avoid unnecessary IO operations
Turn off the binary log, and restore the full backup
#mysqladmin -uroot -p'Mysql@123' flush-logs mysql> set sql_log_bin=0; mysql> system mysql -uroot -predhat </backup/2018-04-26-00-mysql-all.sql
Get the log_file file and pos location point of the backup record
# grep -i change /backup/2018-04-26-00-mysql-all.sql | head -1 CHANGE MASTER TO MASTER_LOG_FILE='master.000003', MASTER_LOG_POS=154;
Incremental backup recovery
mysql> system mysqlbinlog --start-position=154 master.000003 master.000004 master.000005 master.000006 | mysql -uroot -p'Mysql@123'
verify the data
mysql> select * from testdb.test; +------+ | id | +------+ | 1 | | 2 | | 2 | | 3 | | 4 | +------+ 5 rows in set (0.01 sec)