国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > MySQL > 正文

MySQL 自動(dòng)備份與數(shù)據(jù)庫(kù)被破壞后的恢復(fù)方法第1/2頁(yè)

2020-01-19 00:16:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、前言:

當(dāng)數(shù)據(jù)庫(kù)服務(wù)器建立好以后,我們首先要做的不是考慮要在這個(gè)支持?jǐn)?shù)據(jù)庫(kù)的服務(wù)器運(yùn)行哪些受MySQL提攜的程序,而是當(dāng)數(shù)據(jù)庫(kù)遭到破壞后,怎樣安然恢復(fù)到最后一次正常的狀態(tài),使得數(shù)據(jù)的損失達(dá)到最小。

或者說(shuō),僅僅是數(shù)據(jù)庫(kù)服務(wù)器的建立,只能說(shuō)明它能做些什么,并不代表它能穩(wěn)定的做些什么。災(zāi)難恢復(fù)的效率及全面性,也是系統(tǒng)的穩(wěn)定性的一個(gè)準(zhǔn)因素,尤其對(duì)于一個(gè)服務(wù)器系統(tǒng)。

這一節(jié),介紹數(shù)據(jù)庫(kù)自動(dòng)備份以及數(shù)據(jù)庫(kù)被破壞后的恢復(fù)的方法。在這里,我們使用mysqlhotcopy,并且定義一段Shell腳本來(lái)實(shí)現(xiàn)數(shù)據(jù)庫(kù)的自動(dòng)備份,并且,讓整個(gè)數(shù)據(jù)自動(dòng)備份與數(shù)據(jù)恢復(fù)過(guò)程都基于Shell。

建立數(shù)據(jù)庫(kù)備份所需條件

[1] 建立自動(dòng)備份腳本

在這里,為了使數(shù)據(jù)庫(kù)備份和恢復(fù)的符合我們的實(shí)際要求,用一段符合要求的Shell腳本來(lái)實(shí)現(xiàn)整個(gè)備份過(guò)程的自動(dòng)化。

[root@CentOS ~]# vi mysql-backup.sh  ← 建立數(shù)據(jù)庫(kù)自動(dòng)備份腳本,如下:

#!/bin/bash

PATH=/usr/local/sbin:/usr/bin:/bin

# The Directory of Backup
BACKDIR=/backup/mysql

# The Password of MySQL
ROOTPASS=********  此處請(qǐng)將星號(hào)替換成MySQL的root密碼

# Remake the Directory of Backup
rm -rf $BACKDIR
mkdir -p $BACKDIR

# Get the Name of Database
DBLIST=`ls -p /var/lib/mysql | grep / | tr -d /`

# Backup with Database
for dbname in $DBLIST
do
mysqlhotcopy $dbname -u root -p $ROOTPASS $BACKDIR | logger -t mysqlhotcopy
done

[2] 運(yùn)行數(shù)據(jù)庫(kù)自動(dòng)備份腳本

[root@CentOS ~]# chmod 700 mysql-backup.sh  改變腳本屬性,讓其只能讓root用戶執(zhí)行
[root@CentOS ~]# ./mysql-backup.sh   運(yùn)行腳本
[root@CentOS ~]# ls -l /backup/mysql/   確認(rèn)一下是否備份成功
total 8
drwxr-x--- 2 mysql mysql 4096 Sep 1 16:54 mysql   已成功備份到/backup/mysql目錄中

[3] 讓數(shù)據(jù)庫(kù)備份腳本每天自動(dòng)運(yùn)行

[root@sample ~]# crontab -e  ← 編輯自動(dòng)運(yùn)行規(guī)則(然后會(huì)出現(xiàn)編輯窗口,操作同vi)
00 03 * * * /root/mysql-backup.sh   添加這一行到文件中,讓數(shù)據(jù)庫(kù)備份每天凌晨3點(diǎn)進(jìn)行

測(cè)試自動(dòng)備份正常運(yùn)轉(zhuǎn)與否(備份恢復(fù)的方法)

這里,以通過(guò)實(shí)際操作的過(guò)程來(lái)介紹問(wèn)題出現(xiàn)后的恢復(fù)方法。

[1] 當(dāng)數(shù)據(jù)庫(kù)被刪除后的恢復(fù)方法

首先建立一個(gè)測(cè)試用的數(shù)據(jù)庫(kù)。

[root@CentOS ~]# mysql -u root -p   ← 用root登錄到MySQL服務(wù)器
Enter password:  ← 輸入MySQL的root用戶密碼
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 8 to server version: 4.1.20

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> create database test;  ← 建立一個(gè)測(cè)試用的數(shù)據(jù)庫(kù)test
Query OK, 1 row affected (0.00 sec)

mysql> use test  ← 連接到這個(gè)數(shù)據(jù)庫(kù)
Database changed

mysql> create table test(num int, name varchar(50));  ← 在數(shù)據(jù)庫(kù)中建立一個(gè)表
Query OK, 0 rows affected (0.07 sec)

mysql> insert into test values(1,'Hello,CentOS');  ← 插入一個(gè)值到這個(gè)表(這里以“Hello,CentOS”為例)
Query OK, 1 row affected (0.02 sec)

mysql> select * from test;  ← 查看數(shù)據(jù)庫(kù)中的內(nèi)容
+------+-----------------+
| num | name |
+------+-----------------+
|1  | Hello,Centos |  ← 確認(rèn)剛剛插入到表中的值的存在
+------+------------------+
1 row in set (0.01 sec)

mysql> exit  ← 退出MySQL服務(wù)器
Bye

然后,運(yùn)行剛才建立的數(shù)據(jù)庫(kù)備份腳本,備份剛剛建立的測(cè)試用的數(shù)據(jù)庫(kù)。

[root@sample ~]# cd ← 回到腳本所在的root用戶的根目錄
[root@sample ~]# ./mysql-backup.sh  ← 運(yùn)行腳本進(jìn)行數(shù)據(jù)庫(kù)備份

接下來(lái),我們?cè)俅蔚卿浀組ySQL服務(wù)器中,刪除剛剛建立的測(cè)試用的數(shù)據(jù)庫(kù)test,以便于測(cè)試數(shù)據(jù)恢復(fù)能否成功。

[root@Centos ~]# mysql -u root -p  ← 用root登錄到MySQL服務(wù)器
Enter password:  ← 輸入MySQL的root用戶密碼
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 13 to server version: 4.1.20

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> use test  ← 連接到測(cè)試用的test數(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> drop table test;  ← 刪除數(shù)據(jù)中的表
Query OK, 0 rows affected (0.04 sec)

mysql> drop database test;  ← 刪除測(cè)試用數(shù)據(jù)庫(kù)test
Query OK, 0 rows affected (0.01 sec)

mysql> show databases;
+---------------+
| Database |
+---------------+
| mysql |  ← 確認(rèn)測(cè)試用的test數(shù)據(jù)庫(kù)已不存在、已被刪除
+---------------+
1 row in set (0.01 sec)

mysql> exit  ← 退出MySQL服務(wù)器
Bye

以上,我們就等于模擬了數(shù)據(jù)庫(kù)被破壞的過(guò)程。接下來(lái),是數(shù)據(jù)庫(kù)被“破壞”后,用備份進(jìn)行恢復(fù)的方法。

[root@Centos ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/  ← 復(fù)制備份的數(shù)據(jù)庫(kù)test到相應(yīng)目錄
[root@Centos ~]# chown -R mysql:mysql /var/lib/mysql/test/  ← 改變數(shù)據(jù)庫(kù)test的歸屬為mysql
[root@Centos ~]# chmod 700 /var/lib/mysql/test/  ← 改變數(shù)據(jù)庫(kù)目錄屬性為700
[root@Centos ~]# chmod 660 /var/lib/mysql/test/*  ← 改變數(shù)據(jù)庫(kù)中數(shù)據(jù)的屬性為660

然后,再次登錄到MySQL服務(wù)器上,看是否已經(jīng)成功恢復(fù)了數(shù)據(jù)庫(kù)。

[root@CentOS ~]# mysql -u root -p  ← 用root登錄到MySQL服務(wù)器
Enter password:  ← 輸入MySQL的root用戶密碼
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 14 to server version: 4.1.20

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> show databases;  ← 查看當(dāng)前存在的數(shù)據(jù)庫(kù)
+-------------+
| Database |
+-------------+
| mysql |
| test  |  ← 確認(rèn)剛剛被刪除的test數(shù)據(jù)庫(kù)已經(jīng)成功被恢復(fù)回來(lái)!
+------------+
2 rows in set (0.00 sec)

mysql> use test  ← 連接到test數(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;  ← 查看test數(shù)據(jù)庫(kù)中存在的表
+-------------------+
| Tables_in_test |
+-------------------+
| test  |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from test;  ← 查看數(shù)據(jù)庫(kù)中的內(nèi)容
+------+---------------------+
| num | name  |
+------+---------------------+
| 1 | Hello,CentOS |  ← 確認(rèn)數(shù)據(jù)表中的內(nèi)容與刪除前定義的“Hello,CentOS”一樣!
+------+---------------------+
1 row in set (0.01 sec)

mysql> exit  ← 退出MySQL服務(wù)器
Bye

以上結(jié)果表示,數(shù)據(jù)庫(kù)被刪除后,用備份后的數(shù)據(jù)庫(kù)成功的將數(shù)據(jù)恢復(fù)到了刪除前的狀態(tài)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 博乐市| 穆棱市| 常熟市| 阳泉市| 安义县| 岳普湖县| 内乡县| 乌鲁木齐县| 海城市| 社旗县| 扶沟县| 五莲县| 申扎县| 乌拉特后旗| 龙游县| 四会市| 遂昌县| 威宁| 吉木萨尔县| 礼泉县| 滨州市| 长岛县| 乐至县| 蛟河市| 沐川县| 安图县| 永川市| 丰台区| 临沂市| 舟山市| 定南县| 岳西县| 阜新市| 济宁市| 和顺县| 巴楚县| 鹤峰县| 新晃| 海城市| 溧阳市| 龙岩市|