在使用Mysql数据库的过程中,经常需要使用到数据库的备份和导出功能,对于mysql初学者来说,如何进行这个操作,本文将分享经验,和大家共同学习进步
1:物理备份 文件
2:逻辑备份 库,表—》数据库sql脚本
1)完整性备份
2)增量备份
3)差异备份
物理备份:对数据库操作系统的物理文件(如数据文件、
日志文件等)的备份
◆物理备份方法
冷备份(脱机备份) :是在关闭数据库的时候进行的
热备份(联机备份) :数据库处于运行状态,依赖于数据库的日志文件
温备份:数据库锁定表格(不可写入但可读)的状态下进行备份操作
逻辑备份:对数据库逻辑组件(如: 表等数据库对象)的
备份
程序错误
认为操作错误
运算错误
磁盘故障
灾难(如火灾、地址)和盗窃
●备份时数据库处于关闭状态,直接打包数据库文件
●备份速度快,恢复时也是最简单的
●mysqldump常用的逻辑备份工具
●mysqlhotcopy仅拥有备份MyISAM和ARCHIVE表
●进行增量备份,需要刷新_二进制日志
●免费的MySQl热备份软件Percona XtraBackup
mysqldumo备份数据库
五:数据库完全备份分类
5.21:物理冷备份
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# mkdir /backup
[root@localhost ~]# tar zvcf /backup/mysql ll-$(date + %F).tar.gz /usrlocal/mysql/dat
5.22:数据恢复
[root@localhost ~]# mkdir bak
[root@localhost ~]# mv /usr/local/mysqI/data/ /bak/
[root@localhost ~]# mkdir restore
[root@localhost ~]# tar zxf /backup/mysql all-2020-01-02.tar.gz -C restore/
[root@localhost ~]# mv restore/usr/local/mysql/data/ /usr/local/mysq/
[root@localhost ~]# systemctl start mysql
musqldump -u 用户名 -p【密码】【选项】【数据库名】 > 备份路径/备份文件名
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shuai |
| sys |
+--------------------+
5 rows in set (0.00 sec)
[root@localhost ~]# mysqldump -u root -p shuai > /opt/shuai.sql
#shuai.sql就是导出的备份文件
[root@localhost opt]# ls
mysql-5.7.20 mysql-boost-5.7.20.tar.gz shuai.sql
mysql_all_2020-08-19.tar.gz rh
mysqldump -u 用户名 -p 【密码】【选项】 --databases 库名1 【库名2】.. > /备份路径/备份文件名
多库备份的示例
#显示所有数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mei |
| mysql |
| performance_schema |
| shuai |
| sys |
+--------------------+
6 rows in set (0.01 sec)
#进行多库备份
[root@localhost opt]# mysqldump -uroot -pabc123 --databases shuai mei > /opt/shuai.mei
mysqldump: [Warning] Using a password on the command line interface can be insecure.
musqldump -u 用户名 -p 【密码】【选项】 -add-databases > /备份路径/备份文件名
所有库备份的示例
[root@localhost opt]# mysqldump -uroot -pabc123 --all-databases > /opt/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
musqldump可针对库内特定的表进行备份
使用mysqldump备份表的操作
mysqldump -u 用户名 -p 【密码】【选项】选项库名 表名 > /备份路径/备份文件名
示例:
#进入shuai库
mysql> use shuai ;
#显示数据表
mysql> show tables;
+-----------------+
| Tables_in_shuai |
+-----------------+
| tom |
+-----------------+
1 row in set (0.00 sec)
#查询表内容
mysql> select * from tom;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | zhangsan | banjing |
| 2 | lisi | suzhou |
+----+----------+---------+
2 rows in set (0.00 sec)
#复制tom表 name字段 张三内容 生成一张新表pp
mysql> create table pp as select * from tom where name='zhangsan';
mysql> show tables;
+-----------------+
| Tables_in_shuai |
+-----------------+
| pp |
| tom |
+-----------------+
2 rows in set (0.00 sec)
#新生成表
mysql> select * from pp;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | zhangsan | banjing |
+----+----------+---------+
1 row in set (0.00 sec)
备份shuai库pp表
[root@localhost ~]# mysql -uroot -p123123 shuai pp > /opt/pp.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
#查看备份
[root@localhost ~]# ls /opt
mysql-5.7.20 mysql-boost-5.7.20.tar.gz pp.sql rh
source命令【作用于mysql模式下】
mysql命令【作用于于linux模式下】
登录到mysql数据库
执行source备份sql脚本的路径
source恢复的示例
MYSQL[(none)]> source /backup/all-data.sql
#进入到shuai数据库
mysql> use shuai;
Database changed
#删除tom表
mysql> drop table tom;
Query OK, 0 rows affected (0.01 sec)
#删除pp表
mysql> drop table pp;
Query OK, 0 rows affected (0.01 sec)
#显示所有表 是空的
mysql> show tables;
Empty set (0.01 sec)
#恢复
mysql> source /opt/all.sql;
..省略内容
mysql> use shuai;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_shuai |
+-----------------+
| pp |
| tom |
+-----------------+
2 rows in set (0.00 sec)
#已经恢复 这边我们是恢复所有数据库
#也可以单独的对标进行备份恢复
mysqbinlog [--no-defaults] 增量备份文件 | mysql -u 用户名 -p
示例
首先进行完全备份数据库
#显示数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shuai |
| sys |
+--------------------+
5 rows in set (0.00 sec)
#进入shuai数据库
mysql> use shuai;
Database changed
#显示数据库里所有表
mysql> show tables;
+-----------------+
| Tables_in_shuai |
+-----------------+
| pp |
| tom |
+-----------------+
2 rows in set (0.00 sec)
#查看数据表
mysql> select * from tom;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | zhangsan | banjing |
| 2 | lisi | suzhou |
+----+----------+---------+
2 rows in set (0.00 sec)
#进行完全备份
[root@localhost opt]# mysqldump -uroot -p123123 shuai tom > /opt/tom.sql
[root@localhost ~]# vim /etc/my.cnf
#在[mysqld]项中加入配置 log-bin=mysql-bin'
...省略内容
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
log-bin=mysql-bin #添加此句 lob-bin:二进制文件功能 生成的二进制文件是以mysql-bin为开头
#重启服务
[root@localhost data]# systemctl restart mysqld.service
#使用shuai库
mysql> use shuai;
#插入数据内容 执行正确操作
mysql> insert into tom (name,address) values ('liuge','nanjing');
Query OK, 1 row affected (0.00 sec)
#显示表内容
mysql> select * from tom;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | zhangsan | banjing |
| 2 | lisi | suzhou |
| 3 | liuge | nanjing |
+----+----------+---------+
3 rows in set (0.00 sec)
#进行误操作 删除张三
mysql> delete from tom where id=1;
Query OK, 1 row affected (0.00 sec)
#显示表数据内容
mysql> select * from tom;
+----+-------+---------+
| id | name | address |
+----+-------+---------+
| 2 | lisi | suzhou |
| 3 | liuge | nanjing |
+----+-------+---------+
2 rows in set (0.00 sec)
#再次进行正确操作 添加shuaige
mysql> insert into tom (name,address) values ('shuaige','shanghai');
Query OK, 1 row affected (0.00 sec)
mysql> select * from tom;
+----+---------+----------+
| id | name | address |
+----+---------+----------+
| 2 | lisi | suzhou |
| 3 | liuge | nanjing |
| 4 | shuaige | shanghai |
+----+---------+----------+
3 rows in set (0.00 sec)
#错误操作在两个正确操作中间 下一步进行增量恢复
#进行增量备份
[root@localhost data]# mysqladmin -uroot -p123123 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root
[root@localhost data]# pwd
/usr/local/mysql/data
#多出一个 mysql-bin.000002
[root@localhost data]# ls
auto.cnf ib_logfile0 mysql mysql-bin.index sys
ib_buffer_pool ib_logfile1 mysql-bin.000001 performance_schema
ibdata1 ibtmp1 mysql-bin.000002 shuai
直接mysqlbinlog出来的 文件
执行sql部分的sql显示为base64编码格式
固生成sql记录的时候 不能用常规的办法去生成 需要加上相应的参数才能显示出sql语句
–base64-output=decode-rows -v
[root@localhost data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000001 > /opt/bk02.txt
#解码就可以看生成的日志文件内容
[root@localhost data]# cd /opt
[root@localhost opt]# ls
bk01.txt bk02.txt mysql-5.7.20 mysql-boost-5.7.20.tar.gz rh tom.sql
1.正常操作语句
2.错误语句 568为错误语句开始的位置id 200800 16:07:35为错误语句开始的位置时间 也就是上一次正确语句的结束
3.正确语句 847是正确语句开始的地址id 也是上一次语句的结束
mysql> select * from tom;
+----+---------+----------+
| id | name | address |
+----+---------+----------+
| 2 | lisi | suzhou |
| 3 | liuge | nanjing |
| 4 | shuaige | shanghai |
+----+---------+----------+
3 rows in set (0.00 sec)
3删除tom表
mysql> drop table tom;
Query OK, 0 rows affected (0.00 sec)
#tom表已经没了
mysql> show tables;
+-----------------+
| Tables_in_shuai |
+-----------------+
| pp |
+-----------------+
1 row in set (0.00 sec)
#进行恢复
mysql> source /opt/tom.sql;
#显示所有表 已经恢复了
mysql> show tables;
+-----------------+
| Tables_in_shuai |
+-----------------+
| pp |
| tom |
+-----------------+
2 rows in set (0.00 sec)
#恢复成功
mysql> select * from tom;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | zhangsan | banjing |
| 2 | lisi | suzhou |
+----+----------+---------+
2 rows in set (0.00 sec)
mysqlbinlog --stop-position='操作id' 二进制日志 | mysql -u 用户名 -p 密码
mysqlbinlog --start-position=‘操作‘ 二进制日志 | mysql -u 用户名 -p密码
#错误的开始是621 所有正确的一值到621之前结束
[root@localhost opt]# mysqlbinlog --no-defaults --stop-position='568' /usr/local/mysql/data/mysql-bin.000001 | mysql -uroot -p123123
mysql: [Warning] Using a password on the command line interface can be insecure.
#liuge已经恢复
mysql> select * from tom;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | zhangsan | banjing |
| 2 | lisi | suzhou |
| 3 | liuge | nanjing |
+----+----------+---------+
3 rows in set (0.00 sec)
#恢复shuaighe 添加此句
只要绕过错误的语句id就行 错误id为621 从错误的id下面的678开始就可恢复
[root@localhost opt]# mysqlbinlog --no-defaults --start-position='678' /usr/local/mysql/data/mysql-bin.000001 | mysql -uroot -p123123
mysql: [Warning] Using a password on the command line interface can be insecure.
#恢复shuaige成功
mysql> select * from tom;
+----+----------+----------+
| id | name | address |
+----+----------+----------+
| 1 | zhangsan | banjing |
| 2 | lisi | suzhou |
| 3 | liuge | nanjing |
| 4 | shuaige | shanghai |
+----+----------+----------+
4 rows in set (0.00 sec)
#断点的恢复避免了完全恢复 也把错误操作恢复的优点 生产环境运用较多
mysqlbinlog [--no-defaults]--stop-datetime='年-月-日小时:分钟:秒二进制
mysqlbinlog [--no-defaults]--start-datetime='年-月-日小时:分钟:秒二进制
mysqlbinlog [--no-defaults] --start-datetime='年-月-日小时:分钟:秒--stop-
datetime='年-月-日小时:分钟:秒'二进制日志|mysql-u用户名-p密码
模拟删除数据
#删除tombiao
mysql> drop table tom;
Query OK, 0 rows affected (0.01 sec)
#已经没了
mysql> show tables;
+-----------------+
| Tables_in_shuai |
+-----------------+
| pp |
+-----------------+
1 row in set (0.00 sec)
#进行一次完整恢复
mysql> source /opt/tom.sql;
mysql> show tables;
+-----------------+
| Tables_in_shuai |
+-----------------+
| pp |
| tom |
+-----------------+
2 rows in set (0.00 sec)
mysql> select * from tom;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | zhangsan | banjing |
| 2 | lisi | suzhou |
+----+----------+---------+
2 rows in set (0.00 sec)
基于时间恢复
[root@localhost opt]# mysqlbinlog --no-defaults --stop-datetime='2020-08-22 16:07:35' /usr/local/mysql/data/mysql-bin.000001 | mysql -uroot -p123123
#查看表 liuge恢复完毕
mysql> select * from tom;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | zhangsan | banjing |
| 2 | lisi | suzhou |
| 3 | liuge | nanjing |
+----+----------+---------+
3 rows in set (0.00 sec)
[root@localhost opt]# mysqlbinlog --no-defaults --start-datetime='2020-08-22 16:12:05' /usr/local/mysql/data/mysql-bin.000001 | mysql -uroot -p123123
#恢复成功
mysql> select * from tom;
+----+----------+----------+
| id | name | address |
+----+----------+----------+
| 1 | zhangsan | banjing |
| 2 | lisi | suzhou |
| 3 | liuge | nanjing |
| 4 | shuaige | shanghai |
+----+----------+----------+
4 rows in set (0.00 sec)