MySQL备份与恢复(6)source命令恢复和mysql恢复数据

一、恢复数据库实践

    1、利用source命令恢复数据库

      进入mysql数据库控制台,mysql -uroot -p登录后

      mysql>use 数据库

      然后使用source命令,后面参数为脚本文件(如这里用到的 .sql)

      mysql>source oldboy_db.sql #这个文件是系统路径,默认是登陆mysql前的系统路径

[root@localhost ~]# mysql -uroot -pdubin 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 75
Server version: 5.5.32-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy             |
| oldboy_gbk         |
| oldboy_utf8        |
| performance_schema |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database oldboy;
Query OK, 4 rows affected (7.34 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy_gbk         |
| oldboy_utf8        |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)

mysql> system ls /opt
all_bak.sql.gz    mysql_bak_B_compact.sql  mysql_bak.sql.gz     oldboy_bak1.sql    oldboy.sql.gz
a.sql.gz    mysql_bak_B.sql         mysqlbin_oldboy.000001  oldboy_bak.sql     oldboy_utf8.sql.gz
bak        mysql_bak.sql         mysql.sql.gz         oldboy_gbk.sql.gz  table.sql
mysql> source /opt/mysql_bak_B.sql
Query OK, 0 rows affected (0.00 sec)

…………

Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy             |
| oldboy_gbk         |
| oldboy_utf8        |
| performance_schema |
+--------------------+
6 rows in set (0.00 sec)

mysql> select *from oldboy.test;
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
|  3 | inca    |
|  4 | zuma    |
|  5 | kaka    |
+----+---------+
5 rows in set (0.00 sec)

 

     2、利用mysql命令恢复(标准)

MySQL备份与恢复(6)source命令恢复和mysql恢复数据

 

     问题:分库分表备份的数据如何快速恢复呢?

    还是通过脚本指定的库和表,调用mysql命令恢复。

[root@localhost ~]# cd /opt/
[root@localhost opt]# ll bak/
总用量 156
-rw-r--r--. 1 root root 144569 9月  21 12:12 mysql.sql.gz
-rw-r--r--. 1 root root    534 9月  21 12:12 oldboy_gbk.sql.gz
-rw-r--r--. 1 root root   1370 9月  21 12:12 oldboy.sql.gz
-rw-r--r--. 1 root root    533 9月  21 12:12 oldboy_utf8.sql.gz
[root@localhost opt]# mysql -uroot -pdubin 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 77
Server version: 5.5.32-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy             |
| oldboy_gbk         |
| oldboy_utf8        |
| performance_schema |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database oldboy;
Query OK, 4 rows affected (0.12 sec)

mysql> drop database mysql;
Query OK, 24 rows affected, 2 warnings (0.14 sec)

mysql> drop database oldboy_gbk;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> drop database oldboy_utf8;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
+--------------------+
2 rows in set (0.00 sec)

mysql> quit
Bye
[root@localhost opt]# cd bak/
[root@localhost bak]# ll
总用量 156
-rw-r--r--. 1 root root 144569 9月  21 12:12 mysql.sql.gz
-rw-r--r--. 1 root root    534 9月  21 12:12 oldboy_gbk.sql.gz
-rw-r--r--. 1 root root   1370 9月  21 12:12 oldboy.sql.gz
-rw-r--r--. 1 root root    533 9月  21 12:12 oldboy_utf8.sql.gz
[root@localhost bak]# ls *.gz|awk -F "_" '{print $1}'
mysql.sql.gz
oldboy
oldboy.sql.gz
oldboy
[root@localhost bak]# ls *.gz|awk -F "." '{print $1}'  #得到数据库名
mysql
oldboy_gbk
oldboy
oldboy_utf8
[root@localhost bak]#
[root@localhost bak]# gzip -d *                #解压数据库
[root@localhost bak]# ls *.sql|awk -F "." '{print $1}'
mysql
oldboy_gbk
oldboy
oldboy_utf8

[root@localhost bak]# for dbname in `ls *.sql|awk -F "." '{print $1}'`; do mysql -uroot -pdubin < ${dbname}.sql;done           #脚本批量恢复
[root@localhost bak]# mysql -uroot -pdubin -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oldboy |
| oldboy_gbk |
| oldboy_utf8 |
| performance_schema |
+--------------------+

 

上一篇:SQL server backup


下一篇:centos7 yum源更新