codecamp

18.6 数据库的备份及恢复

前文提到,本书的技术主线是Linux系统的运维方向,不会对数据库管理系统的操作进行深入的讲解,因此大家掌握了上面这些基本的数据库操作命令之后就足够了。下面要讲解的是数据库的备份以及恢复,这些知识比较实用,希望大家能够掌握。

mysqldump命令用于备份数据库数据,格式为“mysqldump [参数] [数据库名称]”。其中参数与mysql命令大致相同,-u参数用于定义登录数据库的账户名称,-p参数代表密码提示符。下面将linuxprobe数据库中的内容导出成一个文件,并保存到root管理员的家目录中:

    [root@linuxprobe ~]# mysqldump -u root -p linuxprobe > /root/linuxprobeDB.dump
    Enter password:此处输入root管理员在数据库中的密码

然后进入MariaDB数据库管理系统,彻底删除linuxprobe数据库,这样mybook数据表单也将被彻底删除。然后重新建立linuxprobe数据库:

    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)

接下来是见证数据恢复效果的时刻!使用输入重定向符把刚刚备份的数据库文件导入到mysql命令中,然后执行该命令。接下来登录到MariaDB数据库,就又能看到linuxprobe数据库以及mybook数据表单了。数据库恢复成功!

    [root@linuxprobe ~]# mysql -u root -p linuxprobe < /root/linuxprobeDB.dump 
    Enter password: 此处输入root管理员在数据库中的密码值
    [root@linuxprobe ~]# mysql -u root -p
    Enter password: 此处输入root管理员在数据库中的密码值
    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)
18.5 管理表单及数据
19.1 无人值守系统
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }