环境要求: 1.逻辑备份工具 mysqldump 2.使用mysql 恢复数据库
**步骤一:使用mysqldump进行逻辑备份
1)备份MySQL服务器上的所有库
将所有的库备份为mysql-all.sql文件:
[root@MySQL50 ~]# mysqldump -uroot -p --all-databases > /root/alldb.sql
Enter password:
同时备份db1、db5库,保存为data/db.sql文件:
[root@MySQL50 ~]# mysqldump -u root -p -B db1 db5 > mydata/db.sql
Enter password:
查看备份文件userdb.sql的部分内容:
[root@MySQL50 ~]# grep '^CREATE DATA' mydata/db.sql
CREATE DATABASE /!32312 IF NOT EXISTS/ db1
/!40100 DEFAULT CHARACTER SET latin1 /;
CREATE DATABASE /!32312 IF NOT EXISTS/ db5
/*!40100 DEFAULT CHARACTER SET latin1 */;
步骤二:使用mysql命令从备份中恢复数据库、表
1)创建名为db1cp的新库
mysql> create database db1cp;
Query OK, 1 row affected (0.09 sec)
[root@MySQL50 ~]# mysqldump -uroot -p db1 > mydata/dbcp1.sql
Enter password:
2)导入备份文件,在新库中重建表及数据
[root@MySQL50 ~]# mysql -uroot -p db1cp < mydata/dbcp1.sql
Enter password:
3)确认新库正常,启用新库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db1cp |
| db5 |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.00 sec)
mysql> show tables;
+-----------------+
| Tables_in_db1cp |
+-----------------+
| t1 |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from t1; +------+------+------+ | id | name | age | +------+------+------+ | 1 | sky | 19 | | 2 | tom | 20 | +------+------+------+ 2 rows in set (0.00 sec) **4)废弃或删除旧库 ysql> drop database db1; Query OK, 1 row affected (0.18 sec)