1、使用mysqldump 命令备份一个数据库
mysqldump -u username -p dbname>BackName.sql
dbname 表示数据库名称
backname.sql 表示文件的名称 命名结尾通常为sql
例如: 使用root用备份test数据库
mysqldump -uroot -p school > c:\sql\school.sql
查看备份完毕的sql文件:
root@9068267d0668:/# cat test.sql -- MySQL dump 10.13 Distrib 8.0.19, for Linux (x86_64) -- -- Host: localhost Database: test -- ------------------------------------------------------ -- Server version8.0.19 /*!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 */; /*!50503 SET NAMES utf8mb4 */; /*!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 `t_class` -- DROP TABLE IF EXISTS `t_class`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `t_class` ( `classno` int NOT NULL AUTO_INCREMENT, `cname` varchar(20) DEFAULT NULL, `loc` varchar(40) DEFAULT NULL, `stucount` int DEFAULT NULL, PRIMARY KEY (`classno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `t_class` -- LOCK TABLES `t_class` WRITE; /*!40000 ALTER TABLE `t_class` DISABLE KEYS */; /*!40000 ALTER TABLE `t_class` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `t_student` -- DROP TABLE IF EXISTS `t_student`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `t_student` ( `stuno` int NOT NULL, `sname` varchar(20) DEFAULT NULL, `sage` int DEFAULT NULL, `sgender` varchar(4) DEFAULT NULL, PRIMARY KEY (`stuno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `t_student` -- LOCK TABLES `t_student` WRITE; /*!40000 ALTER TABLE `t_student` DISABLE KEYS */; /*!40000 ALTER TABLE `t_student` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `t_student_pk` -- DROP TABLE IF EXISTS `t_student_pk`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `t_student_pk` ( `stuno` int NOT NULL, `sname` varchar(20) DEFAULT NULL, `sage` int DEFAULT NULL, `sgender` varchar(4) DEFAULT NULL, PRIMARY KEY (`stuno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `t_student_pk` -- LOCK TABLES `t_student_pk` WRITE; /*!40000 ALTER TABLE `t_student_pk` DISABLE KEYS */; /*!40000 ALTER TABLE `t_student_pk` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `tt_json` -- DROP TABLE IF EXISTS `tt_json`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `tt_json` ( `name` varchar(20) DEFAULT NULL, `age` int DEFAULT NULL, `id` int NOT NULL AUTO_INCREMENT, `json_col` json DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `tt_json` -- LOCK TABLES `tt_json` WRITE; /*!40000 ALTER TABLE `tt_json` DISABLE KEYS */; /*!40000 ALTER TABLE `tt_json` 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 2020-04-30 8:32:05
文件中以“—”开头的都是SQL语句的注释
以“/!”开头、“/”结尾的语句为可执行的MySQL注释,这些语句可以被MySQL执行,但在其他数据库管理系统中被作为注释忽略,这可以提高数据库的可移植性。
是备份账户的名称和主机信息,以及备份的数据库的名称;最后是MySQL服务器的版本号,在这里为8.0.12。
最后是MySQL服务器的版本号,在这里为8.0.12。