網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序制作、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了西湖免費建站歡迎大家使用!
在生產環(huán)境中,數(shù)據(jù)的安全性是至關重要的,任何數(shù)據(jù)的丟失都可能產生嚴重的后果
造成數(shù)據(jù)丟失的原因
程序錯誤
人為錯誤(常事)
計算機失敗
磁盤失敗
災難
物理備份
對數(shù)據(jù)庫操作系統(tǒng)的物理文件(如數(shù)據(jù)文件,日志文件等)的本份
物理備份又可以分為脫機備份(冷備份)和聯(liá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ù)庫結構和文件結構的備份
完全備份保存的是備份完成時刻的數(shù)據(jù)庫
完全備份是增量備份的基礎
完全備份的優(yōu)缺點
優(yōu)點:備份與恢復操作簡單方便
缺點:數(shù)據(jù)存在大量的重復,占用大量的備份空間,備份與恢復時間長
Mysql數(shù)據(jù)庫的備份可以采用多種方式
直接打包數(shù)據(jù)庫文件夾,如/usr/local/mysal/data
使用專用備份工具mysqldump
mysqldump命令
mysql自帶的備份工具,相當方便對mysql進行備份
通過命令工具可以將指定額庫,表或全部的庫導出為SQL腳本,在需要恢復時可進行數(shù)據(jù)恢復
特點
沒有重復數(shù)據(jù),備份量不大,時間短
恢復麻煩:需要上次完全備份及完全備份之后所有的增量備份才能恢復,而且要對所有增量備份進行逐個反推恢復
可以通過mysql提供的二進制日志(binary logs) 間接實現(xiàn)增量備份
一般恢復
添加數(shù)據(jù)——進行完全備份——錄入新的數(shù)據(jù)——進行增量備份——模擬故障——恢復操作基于位置恢復
就是將某個起始時間的二進制日志導入數(shù)據(jù)庫中,從而跳過某個發(fā)生錯誤的時間點實現(xiàn)數(shù)據(jù)的恢復基于時間點恢復
使用基于時間點的恢復,可能會出現(xiàn)在一個時間點里既同時存在正確的操作又存在錯誤的操作,所以我們需要一種更為精確的恢復方式
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; #查看表結構
+-------+--------------+------+-----+---------+----------------+
| 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
[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
[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.
[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)
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.
[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
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 #產生增量備份的日志文件
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)
[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ù)進行操作
[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
[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
[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
[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)
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結束
at 672 --start-position #從672開始到結束
[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.
[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
[root@localhost opt]# mysqlbinlog --no-defaults --start-position='672' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p123123
[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)
分享名稱:Mysql數(shù)據(jù)庫的備份與恢復
地址分享:http://www.rwnh.cn/article16/pcoigg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、企業(yè)網(wǎng)站制作、營銷型網(wǎng)站建設、網(wǎng)站收錄、網(wǎng)站排名、ChatGPT
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)