Mysql數(shù)據(jù)庫的備份與恢復

Mysql數(shù)據(jù)庫的備份與恢復

網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序制作、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了西湖免費建站歡迎大家使用!

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

在生產(chǎn)環(huán)境中,數(shù)據(jù)的安全性是至關(guān)重要的,任何數(shù)據(jù)的丟失都可能產(chǎn)生嚴重的后果
造成數(shù)據(jù)丟失的原因
程序錯誤
人為錯誤(常事)
計算機失敗
磁盤失敗
災難

數(shù)據(jù)庫備份的分類

物理備份
對數(shù)據(jù)庫操作系統(tǒng)的物理文件(如數(shù)據(jù)文件,日志文件等)的本份
物理備份又可以分為脫機備份(冷備份)和聯(lián)機備份(熱備份)
冷備份:實在關(guān)閉數(shù)據(jù)庫的時候進行的
熱備份:數(shù)據(jù)庫處于運行狀態(tài),這種備份方法依賴于數(shù)據(jù)庫的日志文件
邏輯備份:對數(shù)據(jù)庫邏輯組件(如表等數(shù)據(jù)庫對象)的備份
從數(shù)據(jù)庫的備份策略角度,備份可分為
完全備份:每次對數(shù)據(jù)進行完整的備份
差異備份:備份那些自從上次完全備份之后被修改的文件
增量備份:只有那些在上次完全備份或者增量備份后被修改的文件才會被備份
增量備份,第一點在完全備份基礎之上,a,b,c增量修改之后會就b+,c+增量備份。在這兩個文件增量備份之后,再備份修改,b+b+

完全備份

完全備份是對整個數(shù)據(jù)庫的備份,數(shù)據(jù)庫結(jié)構(gòu)和文件結(jié)構(gòu)的備份
完全備份保存的是備份完成時刻的數(shù)據(jù)庫
完全備份是增量備份的基礎
完全備份的優(yōu)缺點
優(yōu)點:備份與恢復操作簡單方便
缺點:數(shù)據(jù)存在大量的重復,占用大量的備份空間,備份與恢復時間長

MySQLdump備份庫

Mysql數(shù)據(jù)庫的備份可以采用多種方式
直接打包數(shù)據(jù)庫文件夾,如/usr/local/mysal/data
使用專用備份工具mysqldump
mysqldump命令
mysql自帶的備份工具,相當方便對mysql進行備份
通過命令工具可以將指定額庫,表或全部的庫導出為SQL腳本,在需要恢復時可進行數(shù)據(jù)恢復

mysql增量備份

特點
沒有重復數(shù)據(jù),備份量不大,時間短
恢復麻煩:需要上次完全備份及完全備份之后所有的增量備份才能恢復,而且要對所有增量備份進行逐個反推恢復
可以通過mysql提供的二進制日志(binary logs) 間接實現(xiàn)增量備份

mysql數(shù)據(jù)庫增量恢復

一般恢復
添加數(shù)據(jù)——進行完全備份——錄入新的數(shù)據(jù)——進行增量備份——模擬故障——恢復操作

基于位置恢復
就是將某個起始時間的二進制日志導入數(shù)據(jù)庫中,從而跳過某個發(fā)生錯誤的時間點實現(xiàn)數(shù)據(jù)的恢復

基于時間點恢復
使用基于時間點的恢復,可能會出現(xiàn)在一個時間點里既同時存在正確的操作又存在錯誤的操作,所以我們需要一種更為精確的恢復方式

我們先創(chuàng)建一個數(shù)據(jù)庫和表格來做實驗

mysql> create database shcool; #創(chuàng)建shcool庫
Query OK, 1 row affected (0.51 sec)

mysql> use shcool; #進入shcool庫中
Database changed
mysql> create table info (  #創(chuàng)建info表
    -> id int(4) not null primary key auto_increment,
    -> name varchar(10) not null,
    -> score decimal(4,1) not null);
Query OK, 0 rows affected (0.21 sec)

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

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

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

mysql> select * from info limit 1; #查看數(shù)據(jù)表的開頭的一行
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
+----+-------+-------+
1 row in set (0.00 sec)

mysql> quit #退出來

完整備份

物理備份

[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# ls
bin      COPYING-test  docs     lib  mysqld.pid  mysql.sock.lock  README       share          usr
COPYING  data          include  man  mysql.sock  mysql-test       README-test  support-files
[root@localhost mysql]# cd data/
[root@localhost data]# ls
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  mysql  performance_schema  shcool  sys
[root@localhost data]# cd shcool/
[root@localhost shcool]# cd ../../
[root@localhost mysql]# tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/
tar: Removing leading `/' from member names
/usr/local/mysql/data/
/usr/local/mysql/data/ibdata1
/usr/local/mysql/data/ib_logfile1
[root@localhost mysql]# ls /opt/
mysql-2019-11-24.tar.xz  mysql-5.7.20  rh

邏輯備份

[root@localhost mysql]# cd data/
[root@localhost data]# mysqldump -u root -p shcool > /opt/shcool.sql
Enter password: 
[root@localhost data]# ls /opt/
mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql

對多個數(shù)據(jù)庫進行完整性備份

[root@localhost opt]# mysqldump -u root -p123123 --databases shcool mysql > /opt/db_shcool_mysql.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]# ls
db_shcool_mysql.sql  mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql

對所有數(shù)據(jù)庫進行備份

[root@localhost opt]# mysqldump -u root -p123123 --opt --all-databases > /opt/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]# ls
all.sql  db_shcool_mysql.sql  mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql

針對某一張表進行備份

[root@localhost opt]# mysqldump -u root -p123123 shcool info > /opt/shcool_info.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

對某張表的結(jié)構(gòu)進行備份

[root@localhost opt]# mysqldump -u root -p123123 -d shcool info > /opt/shcool_info_secret.sql

完整備份恢復

mysql> use shcool;
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;
Query OK, 0 rows affected (0.03 sec)
mysql> source /opt/shcool.sql;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+------------------+
| Tables_in_shcool |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)
mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

在控制臺下恢復數(shù)據(jù)庫

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

mysql> show tables;
Empty set (0.00 sec)
mysql> quit

[root@localhost opt]# mysql -u root -p123123 shcool < /opt/shcool.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

進數(shù)據(jù)庫去驗證一下

[root@localhost opt]# mysql -u root -p123123

mysql> use shcool;
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_shcool |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

增量備份和恢復

我們先把原來的刪掉

[root@localhost opt]# rm -rf *.sql
[root@localhost opt]# ls
mysql-2019-11-24.tar.xz  mysql-5.7.20  rh

先要開啟二進制日志文件

[root@localhost opt]# vim /etc/my.cnf

我們在[mysqld]區(qū)域中加上這一行開啟二進制日志文件

log-bin=mysql-bin

[root@localhost opt]# systemctl restart mysqld.service 
[root@localhost opt]# cd /usr/local/mysql/data/
[root@localhost data]# ls
auto.cnf        ibdata1      ib_logfile1  mysql             mysql-bin.index     shcool
ib_buffer_pool  ib_logfile0  ibtmp1       mysql-bin.000001  performance_schema  sys
[root@localhost data]# mysqldump -u root -p123123 shcool > /opt/shcool.sql #增量備份是建立在完整備份的基礎上
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]# ls /opt/
mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql
[root@localhost data]# mysqladmin -uroot -p123123 flush-logs  #產(chǎn)生增量備份的日志文件
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]# ls #02就是我們的二進制日志文件,你接下來的操作會生成到02當中
auto.cnf        ibdata1      ib_logfile1  mysql             mysql-bin.000002  performance_schema  sys
ib_buffer_pool  ib_logfile0  ibtmp1       mysql-bin.000001  mysql-bin.index   shcool

[root@localhost data]# mysql -u root -p123123

mysql> use shcool;
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 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

mysql> insert into info (name,score) values ('test01',66); #正常插入數(shù)據(jù)
Query OK, 1 row affected (0.01 sec)

mysql> delete from info where name='stu01'; #這是一個誤操作,我們不小心刪了一行數(shù)據(jù)
Query OK, 1 row affected (0.01 sec)

mysql> insert into info (name,score) values ('test02',99); #正常插入數(shù)據(jù)
Query OK, 1 row affected (0.00 sec)
mysql> select * from info;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
|  4 | test02 |  99.0 |
+----+--------+-------+
3 rows in set (0.00 sec)

我們想恢復stu01數(shù)據(jù),它正好在兩條正常的操作語句中間

[root@localhost data]# mysqladmin -uroot -p123123 flush-logs 
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]# ls #我們剛才所有的操作都在02中包括誤操作
auto.cnf        ibdata1      ib_logfile1  mysql             mysql-bin.000002  mysql-bin.index     shcool
ib_buffer_pool  ib_logfile0  ibtmp1       mysql-bin.000001  mysql-bin.000003  performance_schema  sys
[root@localhost data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000002 > /opt/bak.txt
要用64解碼器輸出,按行進行讀取,顯示出來生成到bak.txt文件中
[root@localhost data]# cd /opt/
[root@localhost opt]# ls
bak.txt  mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql
以時間點進行恢復

去日志文件中找到刪除的那條語句,把時間寫下來

191124 19:18:21    --stop-datetime 代表從日志文件頭部操作開始到這個時間點,后面不在執(zhí)行操作
191124 19:18:33    --start-datatime 代表從這個時間點開始繼續(xù)進行操作

我們進數(shù)據(jù)庫先不進行時間點的恢復,我們嘗試一下增量備份的恢復

[root@localhost opt]# mysql -u root -p123123
mysql> drop table info;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from info;
ERROR 1146 (42S02): Table 'shcool.info' doesn't exist
mysql> source /opt/shcool.sql;
mysql> show tables;
+------------------+
| Tables_in_shcool |
+------------------+
| info             |
+------------------+
1 row in set (0.00 sec)

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)

mysql> quit
Bye

第一個時間點恢復,我們那個時間點有一個插入了一行數(shù)據(jù)

[root@localhost opt]# mysqlbinlog --no-defaults --stop-datetime='2019-11-24 19:18:21' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p123123

我們?nèi)?shù)據(jù)庫驗證一下

[root@localhost opt]# mysql -u root -p123123

mysql> use shcool;
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 | stu01  |  88.0 |
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
+----+--------+-------+
3 rows in set (0.00 sec)

mysql> quit

第二個時間點恢復,我們等于跳過我們之前的誤操作

[root@localhost opt]# mysqlbinlog --no-defaults --start-datetime='2019-11-24 19:18:33' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p123123

我們再去數(shù)據(jù)庫驗證一下

[root@localhost opt]# mysql -u root -p123123
mysql> use shcool;
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 | stu01  |  88.0 |
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
|  4 | test02 |  99.0 |
+----+--------+-------+
4 rows in set (0.00 sec)

按照at位置恢復

先進行誤操作把test的刪掉

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

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

mysql> select * from info;
+----+-------+-------+
| id | name  | score |
+----+-------+-------+
|  1 | stu01 |  88.0 |
|  2 | stu02 |  77.0 |
+----+-------+-------+
2 rows in set (0.00 sec)
[root@localhost opt]# cd /opt/
[root@localhost opt]# ls
bak.txt  mysql-2019-11-24.tar.xz  mysql-5.7.20  rh  shcool.sql
[root@localhost opt]# vim bak.txt 

at 568  --stop-position   #跟前面一樣,568是從日志頭部到568結(jié)束
at 672  --start-position  #從672開始到結(jié)束

568位置恢復

[root@localhost opt]# mysqlbinlog --no-defaults --stop-position='568' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p123123mysql: [Warning] Using a password on the command line interface can be insecure.

我們?nèi)?shù)據(jù)庫驗證一下

[root@localhost opt]# mysql -u root -p123123
mysql> use shcool;
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 | stu01  |  88.0 |
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
+----+--------+-------+
3 rows in set (0.00 sec)

mysql> quit
Bye

672位置恢復,就跳過了我們之前的誤操作

[root@localhost opt]# mysqlbinlog --no-defaults --start-position='672' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p123123

再去數(shù)據(jù)庫驗證一下

[root@localhost opt]# mysql -u root -p123123
mysql> use shcool;
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 | stu01  |  88.0 |
|  2 | stu02  |  77.0 |
|  3 | test01 |  66.0 |
|  4 | test02 |  99.0 |
+----+--------+-------+
4 rows in set (0.00 sec)

以上就是我們?nèi)康膬?nèi)容了,謝謝收看

分享名稱:Mysql數(shù)據(jù)庫的備份與恢復
地址分享:http://bm7419.com/article16/pcoigg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、企業(yè)網(wǎng)站制作、營銷型網(wǎng)站建設、網(wǎng)站收錄、網(wǎng)站排名、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作