天天看点

MySQL综合实验:备份与恢复

实验前准备:

1.找一个数据库副本做测试(这里找3306实例)
mysqldump -uroot -p -S /usr/local/mysql/mysql.sock pldb > /data/multi-mysql/pldb.sql

2.登陆3306实例
mysql -uroot -p -S /data/multi-mysql/3307/mysql.sock 
Enter password: 
mysql> create database pldb;
mysql> use pldb;
mysql> source /data/multi-mysql/pldb.sql

3.日志记录参数是否打开?
mysql> show variables like '%bin%'
|log_bin         |OFF            |  
mysql> exit;

4. 修改实例配置文件
vim /data/multi-mysql/3307/my.cnf 
[mysqld]
...
log-bin = /data/multi-mysql/3307/mysql-bin

5.关闭,重启,登陆服务
mysqladmin -uroot -p -S /data/multi-mysql/3307/mysql.sock  shutdown
Enter password: 

mysqld_safe --defaults-file=/data/multi-mysql/3307/my.cnf  >/dev/null 2>&1 &
[18] 6658

mysql -uroot -p -P 3307 --protocol tcp
Enter password: 
mysql> 

6.日志记录参数是否打开?
show variables like '%bin%';
| log_bin             | ON |   

---------------------------------
开始进行测试备份恢复实验:

7.备份
mysqldump -uroot -p -B pldb --master-data=2 -S /data/multi-mysql/3307/mysql.sock > /data/multi-mysql/3307/pldb.sql

8. 查看备份最新位置编号:120
cat pldb.sql |grep MASTER_LOG_POS
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=120;

9.准备插入数据(且不做备份)
mysql -uroot -p -P 3307 --protocol tcp
Enter password: 
mysql> use pldb;
...
mysql> select * from student;
+----+-------+-----+------+--------+
| id | name  | age | dept | sex    |
+----+-------+-----+------+--------+
|  1 | pgone |  22 | cs   | male   |
|  2 | pg    |  12 | cs   | female |
|  3 | hehe  |  26 | net  | female |
|  4 | haha  |  24 | net  | male   |
|  5 | bi    |  23 | eng  | male   |
|  7 | ba    |  21 | cs   | male   |
|  8 | bu    |  23 | net  | female |
| 10 | nome  |  27 | net  | female |
| 11 | babe  |  28 | enf  | male   |
| 13 | nano  |  21 | cs   | male   |
| 14 | baki  |  28 | eng  | male   |
| 19 | baki  |  28 | enf  | male   |
+----+-------+-----+------+--------+
12 rows in set (0.00 sec)
mysql> insert into student values(15,'cici',25,'eng','female'),(17,'gucci',27,'cs','male');
mysql> select * from student;                                          
+----+-------+-----+------+--------+
| id | name  | age | dept | sex    |
+----+-------+-----+------+--------+
|  1 | pgone |  22 | cs   | male   |
|  2 | pg    |  12 | cs   | female |
|  3 | hehe  |  26 | net  | female |
|  4 | haha  |  24 | net  | male   |
|  5 | bi    |  23 | eng  | male   |
|  7 | ba    |  21 | cs   | male   |
|  8 | bu    |  23 | net  | female |
| 10 | nome  |  27 | net  | female |
| 11 | babe  |  28 | enf  | male   |
| 13 | nano  |  21 | cs   | male   |
| 14 | baki  |  28 | eng  | male   |
| 15 | cici  |  25 | eng  | female |
| 17 | gucci |  27 | cs   | male   |
| 19 | baki  |  28 | enf  | male   |
+----+-------+-----+------+--------+
14 rows in set (0.00 sec)

10.删库,假装数据库损坏(上一次备份开始到最新的插入这段时间 所有数据库操作数据丢失);
mysql> drop database pldb;

11.导入不完整的备份数据库
mysql> source /data/multi-mysql/3307/pldb.sql;
mysql> use pldb;
mysql> show tables;
+----------------+
| Tables_in_pldb |
+----------------+
| student        |
+----------------+
1 row in set (0.00 sec)

mysql> select * from student;
+----+-------+-----+------+--------+
| id | name  | age | dept | sex    |
+----+-------+-----+------+--------+
|  1 | pgone |  22 | cs   | male   |
|  2 | pg    |  12 | cs   | female |
|  3 | hehe  |  26 | net  | female |
|  4 | haha  |  24 | net  | male   |
|  5 | bi    |  23 | eng  | male   |
|  7 | ba    |  21 | cs   | male   |
|  8 | bu    |  23 | net  | female |
| 10 | nome  |  27 | net  | female |
| 11 | babe  |  28 | enf  | male   |
| 13 | nano  |  21 | cs   | male   |
| 14 | baki  |  28 | eng  | male   |
| 19 | baki  |  28 | enf  | male   |
+----+-------+-----+------+--------+
12 rows in set (0.00 sec)
mysql> exit;

12.从上次备份的文件里看到是120位置(120位置已知也记录在binlog日志里),从120开始便出现数据丢失,所以通过binlog日志导出这段数据为sql文件,再将sql文件导入到数据库中:

写法1:(356后是我对这个数据库做的无效操作)
mysqlbinlog mysql-bin.000001 --start-position=120 --stop-position=356 > /data/multi-mysql/3307/pldb_exp.sql
写法2:
mysqlbinlog mysql-bin.000001 --start-position=120 > /data/multi-mysql/3307/pldb_exp.sql

13.导入

mysql -uroot -p -P 3307 --protocol tcp
Enter password: 
mysql> use pldb;
mysql> source /data/multi-mysql/3307/pldb_exp.sql
mysql> select * from student;
+----+-------+-----+------+--------+
| id | name  | age | dept | sex    |
+----+-------+-----+------+--------+
|  1 | pgone |  22 | cs   | male   |
|  2 | pg    |  12 | cs   | female |
|  3 | hehe  |  26 | net  | female |
|  4 | haha  |  24 | net  | male   |
|  5 | bi    |  23 | eng  | male   |
|  7 | ba    |  21 | cs   | male   |
|  8 | bu    |  23 | net  | female |
| 10 | nome  |  27 | net  | female |
| 11 | babe  |  28 | enf  | male   |
| 13 | nano  |  21 | cs   | male   |
| 14 | baki  |  28 | eng  | male   |
| 15 | cici  |  25 | eng  | female |
| 17 | gucci |  27 | cs   | male   |
| 19 | baki  |  28 | enf  | male   |
+----+-------+-----+------+--------+
14 rows in set (0.00 sec)


#成功
           

继续阅读