标签:mysqldump mysql备份恢复 mysql数据库
数据库: oldcat
数据库表:t_oldcat
1、首先在opt目录下创建备份目录
[root@Base ~]# mkdir -p /opt/mysql/data_bak
2、查询数据库表:
[root@Base ~]# mysql -uoldcat -poldcat123 -e "use oldcat;select * from t_oldcat" +----+--------+ | id | name | +----+--------+ | 1 | cfl | | 2 | heheh | | 3 | xx | | 4 | 小明 | | 5 | 阿里 | | 6 | aa | +----+--------+ 6 rows in set (0.00 sec)
3、备份数据库并检查备份文件
[root@Base ~]# mysqldump -uoldcat -poldcat123 > /opt/mysql/data_bak/oldcat_bak_B.sql [root@Base ~]# ll /opt/mysql/data_bak/ 总用量 4 -rw-r--r--. 1 root root 203 12月 4 21:16 oldcat_bak_B.sql [root@Base ~]# egrep -v "#|\*|--|^$" /opt/mysql/data_bak/oldcat_bak_B.sql USE `oldcat`; DROP TABLE IF EXISTS `t_oldcat`; CREATE TABLE `t_oldcat` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; LOCK TABLES `t_oldcat` WRITE; INSERT INTO `t_oldcat` VALUES (1,‘cfl‘),(2,‘heheh‘),(3,‘xx‘),(4,‘小明‘),(5,‘阿里‘),(6,‘aa‘); UNLOCK TABLES;
4、将oldcat数据库删除(或者只将oldcat数据库中的t_oldcat表删除)
[root@Base ~]# mysql -uoldcat -poldcat123 -e "show databases;drop database oldcat;show databases" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | oldcat | | performance_schema | | test | +--------------------+ +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+
5、执行恢复命并检查恢复结果
[root@Base ~]# mysql -uoldcat -poldcat123 < /opt/mysql/data_bak/oldcat_bak_B.sql [root@Base ~]# mysql -uoldcat -poldcat123 -e "show databases" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | oldcat | | performance_schema | | test | +--------------------+
6、查询oldcat数据库中t_oldcat表
[root@Base ~]# mysql -uoldcat -poldcat123 -e "use oldcat;select * from t_oldcat;" +----+--------+ | id | name | +----+--------+ | 1 | cfl | | 2 | heheh | | 3 | xx | | 4 | 小明 | | 5 | 阿里 | | 6 | aa | +----+--------+
标签:mysqldump mysql备份恢复 mysql数据库
原文地址:http://oldcat1981.blog.51cto.com/10670523/1719523