網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了西湖免費(fèi)建站歡迎大家使用!
在生產(chǎn)環(huán)境中,數(shù)據(jù)的安全性是至關(guān)重要的,任何數(shù)據(jù)的丟失都可能產(chǎn)生嚴(yán)重的后果
造成數(shù)據(jù)丟失的原因
程序錯(cuò)誤
人為錯(cuò)誤(常事)
計(jì)算機(jī)失敗
磁盤失敗
災(zāi)難
物理備份
對(duì)數(shù)據(jù)庫(kù)操作系統(tǒng)的物理文件(如數(shù)據(jù)文件,日志文件等)的本份
物理備份又可以分為脫機(jī)備份(冷備份)和聯(lián)機(jī)備份(熱備份)
冷備份:實(shí)在關(guān)閉數(shù)據(jù)庫(kù)的時(shí)候進(jìn)行的
熱備份:數(shù)據(jù)庫(kù)處于運(yùn)行狀態(tài),這種備份方法依賴于數(shù)據(jù)庫(kù)的日志文件
邏輯備份:對(duì)數(shù)據(jù)庫(kù)邏輯組件(如表等數(shù)據(jù)庫(kù)對(duì)象)的備份
從數(shù)據(jù)庫(kù)的備份策略角度,備份可分為
完全備份:每次對(duì)數(shù)據(jù)進(jìn)行完整的備份
差異備份:備份那些自從上次完全備份之后被修改的文件
增量備份:只有那些在上次完全備份或者增量備份后被修改的文件才會(huì)被備份
增量備份,第一點(diǎn)在完全備份基礎(chǔ)之上,a,b,c增量修改之后會(huì)就b+,c+增量備份。在這兩個(gè)文件增量備份之后,再備份修改,b+b+
完全備份是對(duì)整個(gè)數(shù)據(jù)庫(kù)的備份,數(shù)據(jù)庫(kù)結(jié)構(gòu)和文件結(jié)構(gòu)的備份
完全備份保存的是備份完成時(shí)刻的數(shù)據(jù)庫(kù)
完全備份是增量備份的基礎(chǔ)
完全備份的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):備份與恢復(fù)操作簡(jiǎn)單方便
缺點(diǎn):數(shù)據(jù)存在大量的重復(fù),占用大量的備份空間,備份與恢復(fù)時(shí)間長(zhǎng)
Mysql數(shù)據(jù)庫(kù)的備份可以采用多種方式
直接打包數(shù)據(jù)庫(kù)文件夾,如/usr/local/mysal/data
使用專用備份工具mysqldump
mysqldump命令
mysql自帶的備份工具,相當(dāng)方便對(duì)mysql進(jìn)行備份
通過(guò)命令工具可以將指定額庫(kù),表或全部的庫(kù)導(dǎo)出為SQL腳本,在需要恢復(fù)時(shí)可進(jìn)行數(shù)據(jù)恢復(fù)
特點(diǎn)
沒(méi)有重復(fù)數(shù)據(jù),備份量不大,時(shí)間短
恢復(fù)麻煩:需要上次完全備份及完全備份之后所有的增量備份才能恢復(fù),而且要對(duì)所有增量備份進(jìn)行逐個(gè)反推恢復(fù)
可以通過(guò)mysql提供的二進(jìn)制日志(binary logs) 間接實(shí)現(xiàn)增量備份
一般恢復(fù)
添加數(shù)據(jù)——進(jìn)行完全備份——錄入新的數(shù)據(jù)——進(jìn)行增量備份——模擬故障——恢復(fù)操作基于位置恢復(fù)
就是將某個(gè)起始時(shí)間的二進(jìn)制日志導(dǎo)入數(shù)據(jù)庫(kù)中,從而跳過(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ù)方式
mysql> create database shcool; #創(chuàng)建shcool庫(kù)
Query OK, 1 row affected (0.51 sec)
mysql> use shcool; #進(jìn)入shcool庫(kù)中
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 #退出來(lái)
[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 #增量備份是建立在完整備份的基礎(chǔ)上
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就是我們的二進(jìn)制日志文件,你接下來(lái)的操作會(huì)生成到02當(dāng)中
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'; #這是一個(gè)誤操作,我們不小心刪了一行數(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解碼器輸出,按行進(jìn)行讀取,顯示出來(lái)生成到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
以時(shí)間點(diǎn)進(jìn)行恢復(fù)
191124 19:18:21 --stop-datetime 代表從日志文件頭部操作開始到這個(gè)時(shí)間點(diǎn),后面不在執(zhí)行操作
191124 19:18:33 --start-datatime 代表從這個(gè)時(shí)間點(diǎn)開始繼續(xù)進(jìn)行操作
[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結(jié)束
at 672 --start-position #從672開始到結(jié)束
[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ù)庫(kù)的備份與恢復(fù)
地址分享:http://vcdvsql.cn/article16/pcoigg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、企業(yè)網(wǎng)站制作、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站收錄、網(wǎng)站排名、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)