中小型mysql数据库的备份与恢复
#转载请联系
- 备份到桌面
cd /home/chichung/Desktop # 切换到桌面
mysqldump -u root -p db_jingdong>jd.sql # 重定向写入
jd.sql里面是什么东西呢?我们打开看看
-- MySQL dump 10.13 Distrib 5.7.23, for Linux (x86_64) -- -- Host: localhost Database: db_jingdong -- ------------------------------------------------------ -- Server version 5.7.23-0ubuntu0.16.04.1 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `goods` -- DROP TABLE IF EXISTS `goods`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `goods` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(150) NOT NULL, `cate_id` int(10) unsigned NOT NULL, `brand_name` varchar(40) NOT NULL, `price` decimal(10,3) NOT NULL DEFAULT '0.000', `is_show` bit(1) NOT NULL DEFAULT b'1', `is_saleoff` bit(1) NOT NULL DEFAULT b'0', PRIMARY KEY (`id`), KEY `cate_id` (`cate_id`), CONSTRAINT `goods_ibfk_1` FOREIGN KEY (`cate_id`) REFERENCES `goods_cates` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `goods` -- LOCK TABLES `goods` WRITE; /*!40000 ALTER TABLE `goods` DISABLE KEYS */; INSERT INTO `goods` VALUES (1,'r510vc 15.6英寸笔记本',1,'华硕',3399.000,_binary '',_binary '\0'),(2,'y400n 14.0英寸笔记本电脑',1,'联想',4999.000,_binary '',_binary '\0'),(3,'g150th 15.6英寸游戏本',2,'雷神',8499.000,_binary '',_binary '\0'),(4,'x550cc 15.6英寸笔记本',1,'华硕',2799.000,_binary '',_binary '\0'),(5,'x240 超极本',3,'联想',4880.000,_binary '',_binary '\0'),(6,'u330p 13.3英寸超极本',3,'联想',4299.000,_binary '',_binary '\0'),(7,'svp13226scb 触控超极本',3,'索尼',7999.000,_binary '',_binary '\0'),(8,'ipad mini 7.9英寸平板电脑',4,'苹果',1998.000,_binary '',_binary '\0'),(9,'ipad air 9.7英寸平板电脑',4,'苹果',3388.000,_binary '',_binary '\0'),(10,'ipad mini 配备 retina 显示屏',4,'苹果',2788.000,_binary '',_binary '\0'),(11,'ideacentre c340 20英寸一体电脑 ',5,'联想',3499.000,_binary '',_binary '\0'),(12,'vostro 3800-r1206 台式电脑',5,'戴尔',2899.000,_binary '',_binary '\0'),(13,'imac me086ch/a 21.5英寸一体电脑',5,'苹果',9188.000,_binary '',_binary '\0'),(14,'at7-7414lp 台式电脑 linux )',5,'宏碁',3699.000,_binary '',_binary '\0'),(15,'z220sff f4f06pa工作站',6,'惠普',4288.000,_binary '',_binary '\0'),(16,'poweredge ii服务器',6,'戴尔',5388.000,_binary '',_binary '\0'),(17,'mac pro专业级台式电脑',6,'苹果',28888.000,_binary '',_binary '\0'),(18,'hmz-t3w 头戴显示设备',7,'索尼',6999.000,_binary '',_binary '\0'),(19,'商务双肩背包',7,'索尼',99.000,_binary '',_binary '\0'),(20,'x3250 m4机架式服务器',6,'ibm',6888.000,_binary '',_binary '\0'),(21,'商务双肩背包',7,'索尼',99.000,_binary '',_binary '\0'); /*!40000 ALTER TABLE `goods` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `goods_cates` -- DROP TABLE IF EXISTS `goods_cates`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `goods_cates` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(40) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `goods_cates` -- LOCK TABLES `goods_cates` WRITE; /*!40000 ALTER TABLE `goods_cates` DISABLE KEYS */; INSERT INTO `goods_cates` VALUES (1,'笔记本'),(2,'游戏本'),(3,'超级本'),(4,'平板电脑'),(5,'台式机'),(6,'服务器/工作站'),(7,'笔记本配件'),(12,'游戏机'); /*!40000 ALTER TABLE `goods_cates` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2018-09-06 11:07:48
可以见到,原来备份的是该数据库的SQL语句。所以mysqldump是一个逻辑备份,他备份的是SQL语句,所以不适合大型数据库使用。(大型数据库备份的SQL语句,上百万条,插入插到什么时候......)比较适合中小型数据库使用。同时,mysqldump备份在数据库工作的状态就可以进行备份,所以是热备份。
- 恢复数据到数据库
如果你看了上面导出的SQL语句,你会发现并没有创建数据库的语句。所以在恢复数据之前,你需要创建一个数据库!
进入mysql创建一个数据库,test;
mysql -u -p test<jd.sql;
代码成就万世基积沙镇海,梦想永在凌云意意气风发。