18.6 數(shù)據(jù)庫的備份及恢復(fù)

2022-06-01 16:37 更新

前文提到,本書的技術(shù)主線是Linux系統(tǒng)的運(yùn)維方向,不會(huì)對(duì)數(shù)據(jù)庫管理系統(tǒng)的操作進(jìn)行深入的講解,因此大家掌握了上面這些基本的數(shù)據(jù)庫操作命令之后就足夠了。下面要講解的是數(shù)據(jù)庫的備份以及恢復(fù),這些知識(shí)比較實(shí)用,希望大家能夠掌握。

mysqldump命令用于備份數(shù)據(jù)庫數(shù)據(jù),格式為“mysqldump [參數(shù)] [數(shù)據(jù)庫名稱]”。其中參數(shù)與mysql命令大致相同,-u參數(shù)用于定義登錄數(shù)據(jù)庫的賬戶名稱,-p參數(shù)代表密碼提示符。下面將linuxprobe數(shù)據(jù)庫中的內(nèi)容導(dǎo)出成一個(gè)文件,并保存到root管理員的家目錄中:

    [root@linuxprobe ~]# mysqldump -u root -p linuxprobe > /root/linuxprobeDB.dump
    Enter password:此處輸入root管理員在數(shù)據(jù)庫中的密碼

然后進(jìn)入MariaDB數(shù)據(jù)庫管理系統(tǒng),徹底刪除linuxprobe數(shù)據(jù)庫,這樣mybook數(shù)據(jù)表單也將被徹底刪除。然后重新建立linuxprobe數(shù)據(jù)庫:

    MariaDB [(none)]> DROP DATABASE linuxprobe;
    Query OK, 1 row affected (0.04 sec)
    MariaDB [(none)]> SHOW databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.02 sec)
    MariaDB [(none)]> CREATE DATABASE linuxprobe;
    Query OK, 1 row affected (0.00 sec)

接下來是見證數(shù)據(jù)恢復(fù)效果的時(shí)刻!使用輸入重定向符把剛剛備份的數(shù)據(jù)庫文件導(dǎo)入到mysql命令中,然后執(zhí)行該命令。接下來登錄到MariaDB數(shù)據(jù)庫,就又能看到linuxprobe數(shù)據(jù)庫以及mybook數(shù)據(jù)表單了。數(shù)據(jù)庫恢復(fù)成功!

    [root@linuxprobe ~]# mysql -u root -p linuxprobe < /root/linuxprobeDB.dump 
    Enter password: 此處輸入root管理員在數(shù)據(jù)庫中的密碼值
    [root@linuxprobe ~]# mysql -u root -p
    Enter password: 此處輸入root管理員在數(shù)據(jù)庫中的密碼值
    MariaDB [(none)]> use linuxprobe;
    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
    MariaDB [linuxprobe]> SHOW tables;
    +----------------------+
    | Tables_in_linuxprobe |
    +----------------------+
    | mybook               |
    +----------------------+
    1 row in set (0.05 sec)
    MariaDB [linuxprobe]> DESCRIBE mybook;
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | name  | char(15) | YES  |     | NULL    |       |
    | price | int(11)  | YES  |     | NULL    |       |
    | pages | int(11)  | YES  |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    3 rows in set (0.02 sec)
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)