溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

MySQL數(shù)據(jù)庫(kù)備份的安全與重要性

發(fā)布時(shí)間:2020-04-22 15:07:19 來(lái)源:億速云 閱讀:729 作者:三月 欄目:系統(tǒng)運(yùn)維

下文內(nèi)容主要給大家?guī)?lái)MySQL數(shù)據(jù)庫(kù)備份的安全與重要性,所講到的知識(shí),與書(shū)籍略有不同,都是億速云專(zhuān)業(yè)技術(shù)人員在與用戶(hù)接觸過(guò)程中,總結(jié)出來(lái)的,具有一定的經(jīng)驗(yàn)分享價(jià)值,希望給廣大讀者帶來(lái)幫助。

數(shù)據(jù)備份的重要性

  • 在生產(chǎn)環(huán)境中,數(shù)據(jù)的安全性是至關(guān)重要的,任何數(shù)據(jù)的丟失都可能產(chǎn)生嚴(yán)重的后果
  • 造成數(shù)據(jù)丟失的原因
    • 程序錯(cuò)誤
    • 人為錯(cuò)誤
    • 計(jì)算機(jī)失敗
    • 磁盤(pán)失敗
    • 災(zāi)難(如起火、地震)和偷竊

數(shù)據(jù)庫(kù)備份的分類(lèi)

從物理與邏輯的角度,備份可分為
  • 物理備份:對(duì)數(shù)據(jù)庫(kù)操作系統(tǒng)的物理文件(如數(shù)據(jù)文件、日志文件等)的備份
    • 物理備份又可以分為脫機(jī)備份(冷備份)和聯(lián)機(jī)備份(熱備份)
    • 冷備份:是關(guān)閉數(shù)據(jù)庫(kù)的時(shí)候進(jìn)行的
    • 熱備份:數(shù)據(jù)庫(kù)處于運(yùn)行狀態(tài),這種備份方法依賴(lài)于數(shù)據(jù)庫(kù)的日志文件
  • 邏輯備份:對(duì)數(shù)據(jù)庫(kù)邏輯組件(如表等數(shù)據(jù)庫(kù)對(duì)象)的備份
從數(shù)據(jù)庫(kù)的備份策略角度,備份可分為
  • 完全備份:每次對(duì)數(shù)據(jù)進(jìn)行完整的備份
  • 差異備份:備份那些自從上次完全備份之后被修改過(guò)的文件
  • 增量備份:只有那些在上次完全備份或者增量備份后修改的文件才會(huì)被備份
  • MySQL數(shù)據(jù)庫(kù)備份的安全與重要性

MySQL完全備份

  • 完全備份是對(duì)整個(gè)數(shù)據(jù)庫(kù)的備份、數(shù)據(jù)庫(kù)結(jié)構(gòu)和文件結(jié)構(gòu)的備份
  • 完全備份保存的是備份完成時(shí)刻的數(shù)據(jù)庫(kù)
  • 完全備份是增量備份的基礎(chǔ)
完全備份的優(yōu)點(diǎn)
  • 備份與恢復(fù)操作簡(jiǎn)單方便
完全備份的缺點(diǎn)
  • 數(shù)據(jù)存在大量的重復(fù)
  • 占用大量的備份空間
  • 備份與恢復(fù)時(shí)間長(zhǎng)
mysqldump備份數(shù)據(jù)庫(kù)
  • MySQL數(shù)據(jù)庫(kù)的備份可以采用多種方式
    • 直接打包數(shù)據(jù)庫(kù)文件夾,如/usr/local/mysql/data
    • 使用專(zhuān)用備份工具 mysqldump
  • mysqldump命令
    • MySQL自帶的備份工具,相當(dāng)方便對(duì)MySQL進(jìn)行備份
    • 通過(guò)該命令工具可以將指定的庫(kù)、表或全部的庫(kù)導(dǎo)出為SQL0腳本,在需要恢復(fù)時(shí)可進(jìn)行數(shù)據(jù)恢復(fù)
  • mysqldump命令對(duì)單個(gè)庫(kù)進(jìn)行完全備份
    • `mysqldump -u 用戶(hù)名 -p [密碼] [選項(xiàng)] [數(shù)據(jù)庫(kù)名] > /備份路徑/備份文件名
  • 單庫(kù)備份例子
    • mysqldump -u root -p auth > /backup/auth.sql
  • mysqldump命令對(duì)多個(gè)庫(kù)進(jìn)行完全備份
    • mysqldump -u 用戶(hù)名 -p [密碼] [選項(xiàng)] --databases 庫(kù)名 1 [庫(kù)名2] ... > /備份路徑/備份文件名
  • 多庫(kù)備份例子
    • mysqldump -u root -p --databases autth mysql > /backup/databases-auth-mysql.sql
  • 對(duì)所有庫(kù)進(jìn)行完全備份
    • mysqldump -u 用戶(hù)名 -p [密碼] [選項(xiàng)] --all-databases > /備份路徑/備份文件名
  • 所有庫(kù)備份例子
    • mysqldump -u root -p --opt --all-databases > /backup/all-data.sql
mysqldump備份表
  • 在實(shí)際生產(chǎn)環(huán)境中,存在對(duì)某個(gè)特定表的維護(hù)操作,此時(shí)mysqldump同樣發(fā)揮重大作用
  • 使用mysqldump備份表的操作
    • mysqldump -u 用戶(hù)名 -p [密碼] [選項(xiàng)] 數(shù)據(jù)庫(kù)名 表命 > /備份路徑/備份文件名
  • 備份表的例子
    • mysqldump -u root -p mysql user > /backup/mysql-user.sql
恢復(fù)數(shù)據(jù)庫(kù)
  • 使用mysqldump命令導(dǎo)出的SQL備份腳本,在進(jìn)行數(shù)據(jù)恢復(fù)時(shí)可以使用以下方法導(dǎo)入
    • source命令
    • mysql命令
  • 使用source恢復(fù)數(shù)據(jù)庫(kù)的步驟
    • 登錄到MySQL數(shù)據(jù)庫(kù)
    • 執(zhí)行source 備份sql腳本的路徑
  • source恢復(fù)例子
    • MySQL[(none)]>source /backup/all-data.sql
  • 使用mysql命令恢復(fù)數(shù)據(jù)
    • mysql -u 用戶(hù)名 -p [密碼] < 庫(kù)備份腳本的路徑
  • mysql命令恢復(fù)例子
    • mysql -u root -p < /backup/all-data.sql
恢復(fù)表的操作
  • 恢復(fù)表時(shí)同樣可以使用source或者mysq|命令進(jìn)行
  • source恢復(fù)表的操作與恢復(fù)庫(kù)的操作相同
  • 當(dāng)備份文件中只包含表的備份,而不包括創(chuàng)建庫(kù)的語(yǔ)句時(shí),必須指定庫(kù)名,且目標(biāo)庫(kù)必須存在
    • mysq| -u 用戶(hù)名 -p [密碼] < 表備份腳本的路徑
    • mysql -u root -P mysq| < /backup/mysql-user.sql
  • 在生產(chǎn)環(huán)境中,可以使用shell腳本自動(dòng)實(shí)現(xiàn)定期備份
MySQL備份思路
  • 定期實(shí)施備份,制定備份計(jì)劃或者策略,并嚴(yán)格遵守
  • 除了進(jìn)行完全備份,開(kāi)啟MySQL云服務(wù)器的日志功能是很重要的
    • 完全備份加上日志,可以對(duì)MySQL進(jìn)行最大化還原
  • 使用統(tǒng)一的和易理解的備份文件名稱(chēng)
    • 不要使用backup1、backup2等這樣沒(méi)有意義的名字
    • 不推薦使用庫(kù)名或者表名加上時(shí)間的命名規(guī)則

MySQL增量備份

使用mysqldump進(jìn)行完全備份的存在的問(wèn)題
  • 備份數(shù)據(jù)中有重復(fù)數(shù)據(jù)
  • 備份時(shí)間與恢復(fù)時(shí)間長(zhǎng)
增量備份就是備份自.上一次備份之后增加或變化的文件或者內(nèi)容
增量備份的特點(diǎn)
  • 沒(méi)有重復(fù)數(shù)據(jù),備份量不大,時(shí)間短
  • 恢復(fù)麻煩:需要上次完全備份及完全備份之后所有的增量備份才能恢復(fù),而且要對(duì)所有增量備份進(jìn)行逐個(gè)反推恢復(fù)
MySQL沒(méi)有提供直接的增量備份方法
可以通過(guò)MySQL提供的二進(jìn)制日志(binary logs)間接實(shí)現(xiàn)增量備份
MySQL二進(jìn)制日志對(duì)備份的意義
  • 二進(jìn)制日志保存了所有更新或者可能更新數(shù)據(jù)庫(kù)的操作
  • 二進(jìn)制日志在啟動(dòng)MySQL服務(wù)器后開(kāi)始記錄,并在文件達(dá)到max_binlog_size所設(shè)置的大小或者接收到flush logs命令后重新創(chuàng)建新的日志文件
  • 只需定時(shí)執(zhí)行flush logs方法重新創(chuàng)建新的日志,生成二進(jìn)制文件序列,并及時(shí)把這些舊的日志保存到安全的地方就完成了一個(gè)時(shí)間段的增量備份
MySQL數(shù)據(jù)庫(kù)增量恢復(fù)
  • 一般恢復(fù)

    • 添加數(shù)據(jù)→進(jìn)行完全備份→錄入新的數(shù)據(jù)→進(jìn)行增量備份→模擬故障→恢復(fù)操作
  • 基于位置恢復(fù)
    • 就是將某個(gè)起始時(shí)間的二進(jìn)制日志導(dǎo)入數(shù)據(jù)庫(kù)中,c從而跳過(guò)某個(gè)發(fā)生錯(cuò)誤的時(shí)間點(diǎn)實(shí)現(xiàn)數(shù)據(jù)的恢復(fù)
  • 基于時(shí)間點(diǎn)的恢復(fù)

    • 使用基于時(shí)間點(diǎn)的恢復(fù),可能會(huì)出現(xiàn)在一個(gè)時(shí)間點(diǎn)里既同時(shí)存在正確的操作又存在錯(cuò)誤的操作,所以我們需要一種更為精確的恢復(fù)方式
    增量恢復(fù)的方法
    • 一般恢復(fù)
    • mysqlbinlog [--no-defaults] 增量備份文件 | mysql -u 用戶(hù)名 -p
  • 基于位置的恢復(fù)
    • 恢復(fù)數(shù)據(jù)到指定位置
    • mysqlbinlog --stop-position='操作 id' 二進(jìn)制日志 | mysql -u 用戶(hù)名 -p 密碼
    • 從指定的位置開(kāi)始恢復(fù)數(shù)據(jù)
    • mysqlbinlog --start-position='操作 id' 二進(jìn)制日志 | mysql -u 用戶(hù)名 -p 密碼
  • 基于時(shí)間點(diǎn)的恢復(fù)
    • 從日志開(kāi)頭截止到某個(gè)時(shí)間點(diǎn)的恢復(fù)
    • mysqlbinlog [-no-defaults] --stop-datetime='年-月-日 小時(shí):分鐘:秒' 二進(jìn)制日志 | mysql -u 用戶(hù)名-p 密碼
    • 從某個(gè)時(shí)間點(diǎn)到日志結(jié)尾的恢復(fù)
    • mysqlbinlog [-no-defaults] --start-datetime='年-月-日 小時(shí):分鐘:秒' 二進(jìn)制日志 | mysql -u 用戶(hù)名 -p 密碼
    • 從某個(gè)時(shí)間點(diǎn)到某個(gè)時(shí)間點(diǎn)的恢復(fù)
    • mysqlbinlog [-no-defaults] --start-datetime='年-月-日 小時(shí):分鐘:秒' --stop-datetime='年-月-日 小時(shí):分鐘:秒' 二進(jìn)制日志 | mysql -u 用戶(hù)名 -p 密碼

操作實(shí)例

創(chuàng)建數(shù)據(jù)庫(kù)、編輯表數(shù)據(jù)
[root@master2 ~]# mysql -uroot -p      //進(jìn)入數(shù)據(jù)庫(kù)
Enter password: 

mysql> create database school;      //創(chuàng)建數(shù)據(jù)庫(kù)
Query OK, 1 row affected (0.01 sec)

mysql> use school;     //使用數(shù)據(jù)庫(kù)
Database changed
mysql> create table info(       //創(chuàng)建表
        -> id int(3) not null primary key auto_increment,
        -> name varchar(10) not null,
        -> score decimal(4,1) not null);
Query OK, 0 rows affected (0.02 sec)

mysql> desc info;       //查看表結(jié)構(gòu)
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(3)       | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)  | NO   |     | NULL    |                |
| score | decimal(4,1) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> insert into info (name,score) values ('stu01',88),('stu02',77);        //插入表數(shù)據(jù)
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from info;      //查看表內(nèi)容
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.01 sec)

mysql> select * from info limit 1;      //只顯示表中的前1行
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
+----+-------+-------+
1 row in set (0.00 sec)
完整備份操作
[root@master2 ~]# cd /usr/local/mysql/data/     //切換到數(shù)據(jù)庫(kù)的數(shù)據(jù)目錄下
[root@master2 data]# ls
auto.cnf        ibdata1      ib_logfile1  mysql               school  test
ib_buffer_pool  ib_logfile0  ibtmp1       performance_schema  sys
[root@master2 data]# cd school/
[root@master2 school]# ls         //數(shù)據(jù)中的文件
db.opt  info.frm  info.ibd
[root@master2 school]# cd ..
[root@master2 data]# tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/    //用xz格式壓縮
[root@master2 data]# cd /opt/
[root@master2 opt]# ls
mysql-2019-11-26.tar.xz  mysql-5.7.20  rh
單個(gè)數(shù)據(jù)庫(kù)邏輯備份
[root@master2 opt]# mysqldump -uroot -p school > /opt/school.sql    //邏輯備份單個(gè)數(shù)據(jù)庫(kù)
Enter password: 
[root@master2 opt]# ls
mysql-2019-11-26.tar.xz  mysql-5.7.20  rh  school.sql
[root@master2 opt]# vim school.sql        //查看備份數(shù)據(jù)庫(kù)腳本

...
CREATE TABLE `info` (
    `id` int(3) NOT NULL AUTO_INCREMENT,
    `name` varchar(10) NOT NULL,
    `score` decimal(4,1) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
...
LOCK TABLES `info` WRITE;
/*!40000 ALTER TABLE `info` DISABLE KEYS */;
INSERT INTO `info` VALUES (1,'stu01',88.0),(2,'stu02',77.0);
...
多個(gè)數(shù)據(jù)庫(kù)備份
[root@master2 opt]# mysqldump -uroot -p --databases school mysql > /opt/db_school_mysql.sql    //備份多個(gè)數(shù)據(jù)庫(kù)
Enter password: 
[root@master2 opt]# ls
db_school_mysql.sql  mysql-2019-11-26.tar.xz  mysql-5.7.20  rh  school.sql
完全備份
[root@master2 opt]# mysqldump -uroot -p --opt --all-databases > /opt/all.sql    //完全備份
Enter password: 
[root@master2 opt]# ls
all.sql              mysql-2019-11-26.tar.xz  rh
db_school_mysql.sql  mysql-5.7.20             school.sql
數(shù)據(jù)庫(kù)中的表進(jìn)行備份
[root@master2 opt]# mysqldump -uroot -p school info > /opt/school_info.sql     //對(duì)數(shù)據(jù)庫(kù)中的表進(jìn)行備份
Enter password: 
[root@master2 opt]# ls
all.sql              mysql-2019-11-26.tar.xz  rh               school.sql
db_school_mysql.sql  mysql-5.7.20             school_info.sql
表結(jié)構(gòu)備份
[root@master2 opt]# mysqldump -uroot -p -d school info > /opt/school_info_desc.sql    //對(duì)表結(jié)構(gòu)進(jìn)行備份
Enter password: 
[root@master2 opt]# ls
all.sql                  mysql-5.7.20          school_info.sql
db_school_mysql.sql      rh                    school.sql
mysql-2019-11-26.tar.xz  school_info_desc.sql
基于腳本恢復(fù)數(shù)據(jù)庫(kù)
[root@master2 opt]# mysql -uroot -p     //進(jìn)入數(shù)據(jù)庫(kù)
Enter password: 

mysql> show databases;   //查看數(shù)據(jù)庫(kù)
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> use school;    //使用數(shù)據(jù)庫(kù)
Database changed

mysql> show tables;   //查看表
+------------------+
| Tables_in_school |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)

mysql> drop table info;    //刪除表
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;        //查看表
Empty set (0.00 sec)

mysql> source /opt/school.sql  //恢復(fù)數(shù)據(jù)庫(kù)腳本文件

mysql> show tables;    //查看表
+------------------+
| Tables_in_school |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)
基于外部MySQL命令恢復(fù)數(shù)據(jù)庫(kù)
mysql> drop table info;   //刪除表
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;   //查看表
Empty set (0.00 sec)

mysql> quit   //退出
Bye
[root@master2 opt]# mysql -uroot -p123123 school < /opt/school.sql   //利用mysql命令進(jìn)行恢復(fù)
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master2 opt]# mysql -uroot -p123123    //進(jìn)入數(shù)據(jù)庫(kù)
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> use school;    //使用數(shù)據(jù)庫(kù)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;   //查看表
+------------------+
| Tables_in_school |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)
MySQL增量備份及恢復(fù)
開(kāi)啟二進(jìn)制日志文件
[root@master2 opt]# vim /etc/my.cnf  //開(kāi)啟二進(jìn)制日志文件
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysql.pid
socket = /usr/local/mysql/mysql.sock
log-bin=mysql-bin   //開(kāi)啟二進(jìn)制日志文件
server-id = 1
[root@master2 opt]# systemctl restart mysqld.service   //重啟mysql服務(wù)
[root@master2 opt]# cd /usr/local/mysql/data/   //切換到mysql站點(diǎn)
[root@master2 data]# ls     //查看二進(jìn)制日志文件
auto.cnf        ib_logfile0  mysql             performance_schema  test
ib_buffer_pool  ib_logfile1  mysql-bin.000001  school
ibdata1         ibtmp1       mysql-bin.index   sys
進(jìn)行完全備份
[root@master2 data]# mysqldump -uroot -p123123 school > /opt/school.sql      //一次完全備份
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master2 data]# ls
auto.cnf        ib_logfile0  mysql             performance_schema  test
ib_buffer_pool  ib_logfile1  mysql-bin.000001  school
ibdata1         ibtmp1       mysql-bin.index   sys
[root@master2 data]# mysqladmin -uroot -p123123 flush-logs    //刷新二進(jìn)制日志文件
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@master2 data]# ls         //生成新的二進(jìn)制日志文件,接下來(lái)的操作會(huì)保存在mysql-bin.000002中
auto.cnf        ib_logfile0  mysql             mysql-bin.index     sys
ib_buffer_pool  ib_logfile1  mysql-bin.000001  performance_schema  test
ibdata1         ibtmp1       mysql-bin.000002  school
模擬誤操作
[root@master2 data]# mysql -uroot -p123123  ##進(jìn)入數(shù)據(jù)庫(kù)
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> use school;    ##使用數(shù)據(jù)庫(kù)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from info;      //查看表
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | st01 |  88.0 |
|  2 | st02 |  77.0 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> insert into info (name,score) values ('by01',66);   //正確操作
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | st01 |  88.0 |
|  2 | st02 |  77.0 |
|  3 | by01 |  66.0 |
+----+------+-------+
3 rows in set (0.00 sec)

mysql> delete from info where name='st01';   //錯(cuò)誤操作
Query OK, 1 row affected (0.00 sec)

mysql> insert into info (name,score) values ('by02',99);      //正確操作
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;
+----+------+-------+
| id | name | score |
+----+------+-------+
|  2 | st02 |  77.0 |
|  3 | by01 |  66.0 |
|  4 | by02 |  99.0 |
+----+------+-------+
3 rows in set (0.00 sec)

[root@master2 data]# mysqladmin -uroot -p123123 flush-logs    //刷新二進(jìn)制日志文件        
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@master2 data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000002 > /opt/bak.txt           //用64位解碼器查看二進(jìn)制日志文件,并生成一個(gè)文件
[root@master2 data]# cd /opt/
[root@master2 opt]# ls
bak.txt  mysql-5.7.20  rh  school.sql
[root@master2 opt]# vim bak.txt    //查看二進(jìn)制日志文件

# at 1084
#191127 20:14:01 server id 1  end_log_pos 1132 CRC32 0xdcc90eb5         Write_rows: table id 221 flags: STMT_END_F
### INSERT INTO `school`.`info`   //第一次正確操作的時(shí)間和位置
### SET
###   @1=3
###   @2='by01'
###   @3=66.0
...
# at 1302   //停止位置點(diǎn)
#191127 20:14:46 server id 1  end_log_pos 1357 CRC32 0x6648509a         Table_map: `school`.`info` mapped to number 221
# at 1357
#191127 20:14:46 server id 1  end_log_pos 1405 CRC32 0x1eeb752b         Delete_rows: table id 221 flags: STMT_END_F
### DELETE FROM `school`.`info`   //第二次執(zhí)行錯(cuò)誤操作的時(shí)間和位置191127 20:14:46
### WHERE
###   @1=1
###   @2='st01'
###   @3=88.0
# at 1405  //開(kāi)始位置點(diǎn)
#191127 20:14:46 server id 1  end_log_pos 1436 CRC32 0xf1c8d903         Xid = 54
...
# at 1630
#191127 20:15:16 server id 1  end_log_pos 1678 CRC32 0x08d9b0f4         Write_rows: table id 221 flags: STMT_END_F
### INSERT INTO `school`.`info`   //第二次正確操作的時(shí)間和位置191127 20:15:16
### SET
###   @1=4
###   @2='by02'
###   @3=99.0
基于時(shí)間點(diǎn)進(jìn)行斷點(diǎn)恢復(fù)
[root@master2 opt]# mysql -uroot -p123123   //進(jìn)入數(shù)據(jù)庫(kù)
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> use school;  //進(jìn)入庫(kù)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> drop table info;   //刪除數(shù)據(jù)庫(kù)
Query OK, 0 rows affected (0.01 sec)

mysql> select * from info;   //查看表
ERROR 1146 (42S02): Table 'school.info' doesn't exist
mysql> source /opt/school.sql   //恢復(fù)完全備份數(shù)據(jù)庫(kù)腳本
...
mysql> show tables;   //查看表
+------------------+
| Tables_in_school |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)

mysql> select * from info;  //查看表數(shù)據(jù)
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | st01 |  88.0 |
|  2 | st02 |  77.0 |
+----+------+-------+
2 rows in set (0.00 sec)

[root@master2 opt]# mysqlbinlog --no-defaults --stop-datetime='2019-11-27 20:14:46' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123          //恢復(fù)bin.000002中前一個(gè)正確的執(zhí)行語(yǔ)句(從第二個(gè)錯(cuò)誤語(yǔ)句時(shí)間點(diǎn)停止)
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master2 opt]# mysql -uroot -p123123      //進(jìn)入數(shù)據(jù)庫(kù)
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> use school;  //使用數(shù)據(jù)庫(kù)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from info;       //查看表數(shù)據(jù),恢復(fù)了第一次正確操作
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | st01 |  88.0 |
|  2 | st02 |  77.0 |
|  3 | by01 |  66.0 |
+----+------+-------+
3 rows in set (0.00 sec)

[root@master2 opt]# mysqlbinlog --no-defaults --start-datetime='2019-11-27 20:15:16' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123        //跳過(guò)錯(cuò)誤節(jié)點(diǎn),恢復(fù)最后一個(gè)正確的操作(從最后一個(gè)正確的操作時(shí)間點(diǎn)開(kāi)始)
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master2 opt]# mysql -uroot -p123123  //進(jìn)入數(shù)據(jù)庫(kù)
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> use school;   //使用數(shù)據(jù)庫(kù)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from info;      //查看表數(shù)據(jù),恢復(fù)了第二次正確操作,跳過(guò)了錯(cuò)誤的操作
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | st01 |  88.0 |
|  2 | st02 |  77.0 |
|  3 | by01 |  66.0 |
|  4 | by02 |  99.0 |
+----+------+-------+
4 rows in set (0.00 sec)
基于位置點(diǎn)進(jìn)行斷點(diǎn)恢復(fù)
mysql> delete from info where name='by01';    //為實(shí)驗(yàn)方便直接刪除
Query OK, 1 row affected (0.01 sec)

mysql> delete from info where name='by02';    //刪除
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;    //完全備份的初始狀態(tài)
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | st01 |  88.0 |
|  2 | st02 |  77.0 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> quit
Bye

[root@master2 opt]# mysqlbinlog --no-defaults --stop-position='1302' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123                 //跳過(guò)錯(cuò)誤操作的位置點(diǎn)從上一個(gè)位置點(diǎn)開(kāi)始
[root@master2 opt]# mysql -uroot -p123123   //進(jìn)入數(shù)據(jù)庫(kù)
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> use school;    //使用數(shù)據(jù)庫(kù)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from info;   //查看表數(shù)據(jù),恢復(fù)了第一次正確的操作
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | st01 |  88.0 |
|  2 | st02 |  77.0 |
|  3 | by01 |  66.0 |
+----+------+-------+
3 rows in set (0.00 sec)

mysql> quit
Bye

[root@master2 opt]# mysqlbinlog --no-defaults --start-position='1405' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123      //從錯(cuò)誤的位置后一個(gè)位置點(diǎn)開(kāi)始,跳過(guò)錯(cuò)誤操作的位置點(diǎn)
[root@master2 opt]# mysql -uroot -p123123   //進(jìn)入數(shù)據(jù)庫(kù)
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> use school;   ##使用數(shù)據(jù)庫(kù)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from info;    //查看表數(shù)據(jù),跳過(guò)錯(cuò)誤操作,恢復(fù)第二次正確操作數(shù)據(jù)
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | st01 |  88.0 |
|  2 | st02 |  77.0 |
|  3 | by01 |  66.0 |
|  4 | by02 |  99.0 |
+----+------+-------+
4 rows in set (0.00 sec)
增量備份全部恢復(fù)

[root@master2 opt]# mysqlbinlog --no-defaults  /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p123123       //全部增量恢復(fù)

對(duì)于以上關(guān)于MySQL數(shù)據(jù)庫(kù)備份的安全與重要性,如果大家還有更多需要了解的可以持續(xù)關(guān)注我們億速云的行業(yè)推新,如需獲取專(zhuān)業(yè)解答,可在官網(wǎng)聯(lián)系售前售后的,希望該文章可給大家?guī)?lái)一定的知識(shí)更新。


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI