实验介绍
数据库备份从物理与逻辑的角度划分可以分为: 物理备份, 逻辑备份. 从数据库的备份策略角度, 可以分为完全备份, 差异备份, 增量备份. 这次实验主要介绍完全备份, 完全备份优点是备份与恢复操作简单方便, 缺点是数据存在大量的重复, 占用大量的备份空间, 备份时间长.
Mysql 完全备份
1, 使用 tar 打包文件夹备份
Mysql 的数据库文件默认都是保存在安装目录下的 data 文件夹下面, 可以直接保存 data 文件夹. 但是占用的空间很大, 可以使用 tar 打包压缩进行保存. 由于数据库文件很大, 可以直接使用压缩率较大的 xz 格式压缩, 所以首先要安装 xz 压缩格式工具.
[root@localhost opt]# yum install xz -y
然后, 对数据库文件夹 / usr/local/mysql/data / 进行打包操作. 注意这里使用 tar 工具打包, 最好使用相对路径, 所以先切换至 / usr/local/mysql 目录下, 然后再打包.
- [root@localhost opt]# cd /usr/local/mysql/
- [root@localhost mysql]# tar Jcf /opt/mysql-$(date +%F).tar.xz data/
- [root@localhost opt]# ls
- mysql-2018-07-02.tar.xz mysql-5.7.17 test.sql
2, 使用 mysqldump 工具备份
(1) 使用 mysqldump 命令对某些表完全备份, 如下图, 数据库中有 test 的库, 里面有一张 yx 的表.
- mysql> show tables;
- +----------------+
- | Tables_in_test |
- +----------------+
- | yx |
- +----------------+
- 1 row in set (0.00 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- | test |
- +--------------------+
- 5 rows in set (0.00 sec)
对 test 库中的 yx 表进行备份.
- [root@localhost opt]# mysqldump -u root -p test yx> /opt/yx1.sql
- Enter password:
- [root@localhost opt]# ls
- mysql-2018-07-02.tar.xz mysql-5.7.17 test.sql yx1.sql
(2) 使用 mysqldump 命令对单个库进行完全备份
- [root@localhost opt]# mysqldump -u root -p test> /opt/test.sql
- Enter password:
- [root@localhost opt]# ls
- mysql-2018-07-02.tar.xz mysql-5.7.17 test.sql yx1.sql
(3) 使用 mysqldump 命令对多个库进行完全备份
- [root@localhost opt]# mysqldump -u root -p --databases test mysql> /opt/test-mysql.sql
- Enter password:
- [root@localhost opt]# ls
- mysql-2018-07-02.tar.xz mysql-5.7.17 test-mysql.sql test.sql yx1.sql
(4) 使用 mysqldump 命令对所有数据库完全备份
- [root@localhost opt]# mysqldump -u root -p --all-databases> /opt/all.sql
- Enter password:
- [root@localhost opt]# ls
- all.sql mysql-2018-07-02.tar.xz mysql-5.7.17 test-mysql.sql test.sql yx1.sql
(5) 使用 mysqldump 命令直接备份表结构
- [root@localhost opt]# mysqldump -u root -p -d test yx> /opt/desc.sql
- Enter password:
- [root@localhost opt]# ls
- all.sql desc.sql mysql-2018-07-02.tar.xz mysql-5.7.17 test-mysql.sql test.sql yx1.sql
Mysql 完全恢复
在需要恢复库的时候, 可以使用 source 命令和 mysql 命令
1,source 命令整库恢复
上面我们已经对 test 库进行了备份, 现在我们删除掉 test 库, 注意在恢复库的时候, 要先创建一个同名的库, 然后再恢复, 否则会报错.
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- | test |
- +--------------------+
- 5 rows in set (0.00 sec)
- mysql> drop database test;
- Query OK, 1 row affected (0.01 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- mysql> create database test;
- Query OK, 1 row affected (0.00 sec)
- mysql> use test;
- Database changed
- mysql> show tables;
- Empty set (0.00 sec)
- mysql> use test;
- Database changed
- mysql> source /opt/test.sql
- mysql> show tables;
- +----------------+
- | Tables_in_test |
- +----------------+
- | yx |
- +----------------+
- 1 row in set (0.00 sec)
test 库就恢复成功了.
2,Mysql 命令整库恢复
在使用 Mysql 命令恢复库之前, 同样要先创建已经被删除的空数据库, 否则会报错, 然后使用下面这条命令.
- [root@localhost opt]# mysqldump -u root -p test </opt/test.sql
- mysql> show tables;
- +----------------+
- | Tables_in_test |
- +----------------+
- | yx |
来源: http://www.bubuko.com/infodetail-2669127.html