目录
方法一:使用source命令导入
方法二:使用mysql客户端导入
方法一:使用source命令导入
有时候我们需要从 SQL 文件执行一些 SQL 语句,比如要把一个数据库从一台服务器 A 复制到另一台服务器 B 上,那么可以先从服务器 A 导出数据到 SQL 文件,然后在服务器 B 执行这个 SQL 文件。
用 help 命令查看命令列表,可以看到有一个 source 命令如下所示:
mysql> help
# 省略...source (\.) Execute an SQL script file. Takes a file name as an argument.
# 省略...
下面我们演示⼀下source命令的使用方法:
1. 准备要执行的.sql文件,名为 test_db.sql,内容如下:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP DATABASE IF EXISTS `test_db`;
CREATE DATABASE `test_db` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `test_db`;
-- ----------------------------
-- Table structure for classes
-- ----------------------------
DROP TABLE IF EXISTS `classes`;
CREATE TABLE `classes` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL
DEFAULT NULL,`desc` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL
DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE =
utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of classes
-- ----------------------------
INSERT INTO `classes` VALUES (1, '计算机系2019级1班', '学习了计算机原理、C和Java语
⾔、数据结构和算法');
INSERT INTO `classes` VALUES (2, '中⽂系2019级3班', '学习了中国传统⽂学');
INSERT INTO `classes` VALUES (3, '⾃动化2019级5班', '学习了机械⾃动化');
-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL
DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE =
utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES (1, 'Java');
INSERT INTO `course` VALUES (2, '中国传统⽂化');
INSERT INTO `course` VALUES (3, '计算机原理');
INSERT INTO `course` VALUES (4, '语⽂');
INSERT INTO `course` VALUES (5, '⾼阶数学');
INSERT INTO `course` VALUES (6, '英⽂');
-- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (`score` decimal(3, 1) NULL DEFAULT NULL,`student_id` int(11) NULL DEFAULT NULL,`course_id` int(11) NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci
ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES (70.5, 1, 1);
INSERT INTO `score` VALUES (98.5, 1, 3);
INSERT INTO `score` VALUES (33.0, 1, 5);
INSERT INTO `score` VALUES (98.0, 1, 6);
INSERT INTO `score` VALUES (60.0, 2, 1);
INSERT INTO `score` VALUES (59.5, 2, 5);
INSERT INTO `score` VALUES (33.0, 3, 1);
INSERT INTO `score` VALUES (68.0, 3, 3);
INSERT INTO `score` VALUES (99.0, 3, 5);
INSERT INTO `score` VALUES (67.0, 4, 1);
INSERT INTO `score` VALUES (23.0, 4, 3);
INSERT INTO `score` VALUES (56.0, 4, 5);
INSERT INTO `score` VALUES (72.0, 4, 6);
INSERT INTO `score` VALUES (81.0, 5, 1);
INSERT INTO `score` VALUES (37.0, 5, 5);
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (`id` int(11) PRIMARY KEY AUTO_INCREMENT,`sn` int(11) NOT NULL COMMENT '学号',`name` varchar(20) NOT NULL COMMENT '姓名',`mail` varchar(20) COMMENT 'QQ邮箱'
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci
ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, 50001, '张三', 'zs@bit.com');
INSERT INTO `student` VALUES (2, 50002, '李四', 'ls@bit.com');
INSERT INTO `student` VALUES (3, 50003, '王五', 'ww@bit.com');
INSERT INTO `student` VALUES (4, 50004, '赵六', 'zl@bit.com');
INSERT INTO `student` VALUES (5, 50005, '钱七', 'qq@bit.com');
SET FOREIGN_KEY_CHECKS = 1;
2. 确定.sql文件的绝对路径:/home/zyq/database/test_db.sql
3. 连接数据库查看已有数据库
4. 使用source命令执行.sql文件的SQL语句
mysql> source /home/zyq/database/test_db.sql
Query OK, 0 rows affected (0.00 sec)
....
Query OK, 0 rows affected (0.00 sec)
5. 查看数据库并查询数据,验证导入是否成功
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_db |
+--------------------+
5 rows in set (0.01 sec)mysql> use test_db;
Database changed
mysql> select * from student;
+----+-------+--------+------------+
| id | sn | name | mail |
+----+-------+--------+------------+
| 1 | 50001 | 张三 | zs@bit.com |
| 2 | 50002 | 李四 | ls@bit.com |
| 3 | 50003 | 王五 | ww@bit.com |
| 4 | 50004 | 赵六 | zl@bit.com |
| 5 | 50005 | 钱七 | qq@bit.com |
+----+-------+--------+------------+
5 rows in set (0.00 sec)mysql>
方法二:使用mysql客户端导入
直接使用mysql客户端程序导入.sql文件并执行相应的SQL语句,可以使用以下命令:
# 在指定的数据库下执⾏SQL,前提是数据库必须提前建⽴好
mysql db_name < text_file # 不指定数据库.sql中必须有USE [database_name],来指定要操作的数据库,数据库可以在文件中创建也可以自己创建
mysql < text_file
zyq@iZm5egpp4a85g2tfliaeikZ:~/database$ mysql < /home/zyq/database/test_db.sqlmysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_db |
+--------------------+
5 rows in set (0.01 sec)mysql>