| [root@master data]# pwd |
| /home/data |
| [root@master data]# ls |
| user.csv |
| |
| # 开始导入 |
| [root@master data]# mysqlimport -h127.0.0.1 -P3306 -uroot -p123456 db_test --fields-terminated-by=',' user.csv --columns='id,name,sex,birthday,grades,address' --ignore-lines=1 --local |
| mysqlimport: [Warning] Using a password on the command line interface can be insecure. |
| db_test.user: Records: 6 Deleted: 0 Skipped: 0 Warnings: 0 |
-
查看导入的数据

-
导入时报错
| [root@master data]# mysqlimport -h127.0.0.1 -P3306 -uroot -p123456 db_test |
| mysqlimport: [Warning] Using a password on the command line interface can be insecure. |
| mysqlimport: Error: 3948, Loading local data is disabled; this must be enabled on both the client and server sides, when using table: user |
| |
| # 解决方案:登录mysql,将local_infile设置为NO |
| [root@master data]# mysql -u root -p |
| Enter password: |
| mysql> show global variables like 'local_infile'; |
| + |
| | Variable_name | Value | |
| + |
| | local_infile | OFF | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> set global local_infile=1; |
| Query OK, 0 rows affected (0.00 sec) |
使用ssh工具登录虚拟机服务器,登录mysql,导出为csv
| # 报错如下,指定目录没有权限 |
| mysql> select * from user into outfile '/home/data/user.csv' FIELDS TERMINATED BY ','; |
| ERROR 1290 (HY000): The MySQL server is running with the |
| |
| # 查看有权限的目录 |
| mysql> show global variables like '%secure_file_priv%'; |
| + |
| | Variable_name | Value | |
| + |
| | secure_file_priv | /var/lib/mysql-files/ | |
| + |
| 1 row in set (0.00 sec) |
| |
| # 再次测试,成功 |
| mysql> select * from student into outfile '/var/lib/mysql-files/student.csv' FIELDS TERMINATED BY ','; |
| Query OK, 4 rows affected (0.00 sec) |
本地安装mysql,使用本地mysql.exe将本地csv文件导入远程mysql
| |
| |
| |
| ychen@DESKTOP-49HGBQ1 MINGW64 /c/work |
| $ pwd |
| /c/work |
| ychen@DESKTOP-49HGBQ1 MINGW64 /c/work |
| $ ls |
| user.csv |
| |
| |
| ychen@DESKTOP-49HGBQ1 MINGW64 /c/work |
| $ mysqlimport -h192.168.128.78 -P3306 -uroot -p123456 db_test --fields-terminated-by=',' user.csv --columns='id,name,sex,birthday,grades,address' --ignore-lines=1 --local |
| mysqlimport: [Warning] Using a password on the command line interface can be insecure. |
| db_test.user: Records: 6 Deleted: 0 Skipped: 0 Warnings: 0 |

查看详情
| # 报错 |
| C:\work>mysqlimport -h192.168.128.78 -P3306 -uroot -p123456 db_test --fields-terminated-by= |
| mysqlimport: [Warning] Using a password on the command line interface can be insecure. |
| mysqlimport: Error: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near |
| |
| # 测试1 |
| C:\work>mysqlimport -h192.168.128.78 -P3306 -uroot -p123456 db_test --fields-terminated-by= |
| mysqlimport: [Warning] Using a password on the command line interface can be insecure. |
| db_test.user: Records: 6 Deleted: 0 Skipped: 0 Warnings: 36 |
| |
| # 测试2 |
| C:\work>mysqlimport -h192.168.128.78 -P3306 -uroot -p123456 db_test --fields-terminated-by= |
| mysqlimport: [Warning] Using a password on the command line interface can be insecure. |
| db_test.user: Records: 6 Deleted: 0 Skipped: 0 Warnings: 0 |


| # 创建表,表中没有数据 |
| CREATE TABLE `user` ( |
| `id` int NULL DEFAULT NULL, |
| `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, |
| `sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, |
| `birthday` timestamp NULL DEFAULT NULL, |
| `grades` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, |
| `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL |
| ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; |
| |
| # 将user.csv文件上传到服务器的/var/lib/mysql-files/目录 |
| # 使用ssh工具登录到虚拟机的服务器,登录mysql |
| [root@master mysql-files]# mysql -u root -p |
| Enter password: |
| |
| # 切换库 |
| mysql> use db_test; |
| |
| # 导入成功 |
| mysql> LOAD DATA LOCAL INFILE '/var/lib/mysql-files/user.csv' |
| -> INTO TABLE user |
| -> FIELDS TERMINATED BY ',' |
| -> ENCLOSED BY '"' |
| -> LINES TERMINATED BY '\n' |
| -> IGNORE 1 ROWS; |
| Query OK, 6 rows affected (0.00 sec) |
| Records: 6 Deleted: 0 Skipped: 0 Warnings: 0 |
| |
| # 复制如下 |
| LOAD DATA LOCAL INFILE '/var/lib/mysql-files/user.csv' |
| INTO TABLE user |
| FIELDS TERMINATED BY ',' |
| ENCLOSED BY '"' |
| LINES TERMINATED BY '\n' |
| IGNORE 1 ROWS; |
| |
| |
| show global variables like 'local_infile'; |
| set global local_infile=1; |
| |
| |
| |
| mysql -u root -p --local-infile |
使用本地mysql.exe将本地csv文件导入远程mysql
| # 远程表中没有数据 |
| # 打开cmd,登录mysql |
| C:\work>mysql -h 192.168.128.78 -P 3306 -u root -p |
| Enter password: ****** |
| |
| # 切换库 |
| mysql> use db_test |
| Database changed |
| |
| # 执行导入 |
| mysql> LOAD DATA LOCAL INFILE 'C:\\work\\user.csv' |
| -> INTO TABLE user |
| -> FIELDS TERMINATED BY ',' |
| -> ENCLOSED BY '"' |
| -> LINES TERMINATED BY '\n' |
| -> IGNORE 1 ROWS; |
| Query OK, 6 rows affected (0.00 sec) |
| Records: 6 Deleted: 0 Skipped: 0 Warnings: 0 |
| |
| # 复制代码 |
| LOAD DATA LOCAL INFILE 'C:\\work\\user.csv' |
| INTO TABLE user |
| FIELDS TERMINATED BY ',' |
| ENCLOSED BY '"' |
| LINES TERMINATED BY '\n' |
| IGNORE 1 ROWS; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-12-07 spring boot导出word
2021-12-07 Redis入门(四):springboot整合redis