First, the database recovery practice
1, using the source command to restore the database
After entering the console mysql database, mysql -uroot -p Login
mysql> use database
Then use the source command, followed by parameters to the script file (as used herein, .sql)
mysql> Source oldboy_db.sql # This file system path, the default path is the system before landing 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, using the mysql command to restore (standard)
Question: data sub-sub-table database backup how to quickly recover it?
Or through the script specified library and table, call the mysql command restored.
[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 # decompression database [BAK the root @ localhost] # LS * .sql | awk -F " . " ' {}. 1 Print $ ' 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 |
+--------------------+