作者:杨涛涛
本文介绍 MySQL8.0 shell 子模块 Util 的两个导入特性 importTable/import_table(JS和python 版本的命名差异)、importJson/import_json的使用方法。其中import_table 是通过传统 MySQL 协议来通信,Import_json 是通过 X 插件协议来通信。
MySQL 一直以来提供导入文件 SQL 命令 load data infile(单线程)以及对应的可执行文件 mysqlimport(多线程),比如我导入 100W 行示例数据到表 ytt.tl1, 花了 24 秒。这个已经是 MySQL 默认导入来的最快的。
[root@mysql-dev ytt]# time mysqlimport --login-path=ytt_master --fields-terminated-by=, --use-threads=4 ytt /var/lib/mysql-files/tl1.csv
ytt.tl1: Records: 1048576 Deleted: 0 Skipped: 0 Warnings: 0
real 0m24.815s
user 0m0.013s
sys 0m0.031s
那我们现在看下 mysqlimport 工具的升级版,mysqlshell 的 util 工具集。使用这两个工具之前,必须得临时开启 local_infile 选项。
1.import_table
[root@mysql-dev ytt]# mysqlsh
MySQL Shell 8.0.17
Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
建立 3306 端口的新连接
MySQL JS > \c admin@127.0.0.1:3306
Creating a session to 'admin@127.0.0.1:3306'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 56
Server version: 8.0.17 MySQL Community Server - GPL
No default schema selected; type \use to set one.
我这里切换为 python 模式
MySQL 127.0.0.1:3306 ssl JS > \py
Switching to Python mode...
MySQL 127.0.0.1:3306 ssl Py > \us