备份数据库资料 7.1 问题 本例要求使用mysqldump工具对数据库进行备份,熟悉单库、多库的不同备份用法,完成下列任务:
1)备份studb库,保存为/root/studb.sql文件
2)备份studb库和zabbix库,保存为/root/mydata.sql文件
7.2 步骤 实现此案例需要按照如下步骤进行。
步骤一:备份studb库,保存为/root/studb.sql文件
[root@svr7 ~]# mysqldump -uroot -ppwd@123 studb > /root/studb.sql步骤二:备份studb库和zabbix库,保存为/root/mydata.sql文件
[root@svr7 ~]# mysqldump -uroot -ppwd@123 --databases studb zabbix > /root/mydata.sql8 案例8:恢复数据库 8.1 问题 本例要求使用mysql工具恢复数据库资料,熟悉恢复单库、多库的不同方法,完成下列任务:
1)确保已经为 studb 库做好备份文件 /root/studb.sql
2)删除名为 studb 的库,检查结果
3)重建名为 studb 的空库
4)将备份文件 /root/studb.sql 导入名为 studb 的库
5)检查 studb 库中的表格数据
8.2 步骤 实现此案例需要按照如下步骤进行。
步骤一:确保已经为 studb 库做好备份文件 /root/studb.sql
[root@svr7 ~]# ls -lh /root/studb.sql -rw-r--r--. 1 root root 11K 10月 24 20:20 /root/studb.sql步骤二:删除名为 studb 的库,检查结果
1)登入数据库服务器
[root@svr7 ~]# mysql -uroot -ppwd@123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 31 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>2)删除studb库
MariaDB [(none)]> DROP DATABASE studb; Query OK, 9 rows affected (0.00 sec) MariaDB [(none)]> 步骤三:重建名为 studb 的空库 MariaDB [(none)]> CREATE DATABASE studb; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> MariaDB [(none)]> quit Bye [root@svr7 ~]#步骤四:将备份文件 /root/studb.sql 导入名为 studb 的库
若目标库studb已丢失,则必须提前建好空库
[root@svr7 ~]# mysql -uroot -ppwd@123 studb < /root/studb.sql [root@svr7 ~]#步骤五:检查 studb 库中的表格数据
[root@svr7 ~]# mysql -uroot -ppwd@123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 33 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> SELECT * FROM stuinfo; ERROR 1046 (3D000): No database selected MariaDB [(none)]> SELECT * FROM studb.stuinfo; +---------------+-----------+--------+--------------+--------------------------+ | 学号 | 姓名 | 性别 | 联系电话 | 通信地址 | +---------------+-----------+--------+--------------+--------------------------+ | NTD2020110001 | 郭靖 | 男 | 13145201314 | 东海桃花岛 | | NTD2020110002 | 黄蓉 | 女 | 13145201413 | 东海桃花岛 | | NTD2020110003 | 华筝 | 女 | 13705666777 | 蒙古大营 | | NTD2020110004 | 洪七 | 男 | 13888888888 | 太湖北丐帮总舵 | | NTD2020110005 | 欧阳锋 | 男 | 18777777777 | 西域白驼山庄 | | NTD2020110006 | 黄药师 | 男 | 13566778899 | 东海桃花岛 | | NTD2020110007 | 周伯通 | 男 | 17012341234 | 昆嵛山全真教总部 | | NTD2020110008 | 王重阳 | 男 | 17012340001 | 昆嵛山全真教总部 | | NTD2020110009 | 段王爷 | 男 | 17566666666 | 云南大理桃源山 | +---------------+-----------+--------+--------------+--------------------------+ 9 rows in set (0.00 sec) MariaDB [(none)]> quit Bye [root@svr7 ~]#