1. mysqlimport參數
mysqlimport客戶端實際上就是“LOAD DATA”命令的一個包裝實現,所以大部分參數選項與LOAD DATA相同。
文件名與導入表相同,例如patient.txt, patient.text和 patient都將被導入到指定表patient中。
shell> mysqlimport [options] db_name textfile1 [textfile2 ...]
mysqlimport的具體參數如下:
[root@chengyu ~]# mysqlimport --help
mysqlimport Ver 8.0.20 for Linux on x86_64 (Source distribution)
Copyright (c) 2000, 2020, 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.
Loads tables from text files in various formats. The base name of the
text file must be the name of the table that should be used.
If one uses sockets to connect to the MySQL server, the server will open and
read the text file directly. In other cases the client will open the text
file. The SQL command 'LOAD DATA INFILE' is used to import the rows.
Usage: mysqlimport [OPTIONS] database textfile...
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql8/etc/my.cnf /usr/local/mysql8/my.cnf ~/.my.cnf
The following groups are read: mysqlimport client
The following options may be given as the first argument:
--print-defaults 打印默認選項.
--no-defaults 除了登錄文件,不讀取其他選項文件中的任意默認參數.
--defaults-file=# 僅讀取指定文件中的默認參數.
--defaults-extra-file=# global選項文件之後讀取的指定選項文件.
--defaults-group-suffix=# Also read groups with concat(group, suffix)
--login-path=# 從.mylogin.cnf中讀取登錄路徑選項.
--bind-address= 在具有多個網絡接口的計算機上,使用此選項選擇用於連接到MySQL服務器的接口.
--character-sets-dir=name 字符集的安裝目錄.
--default-character-set=name 指定字符集.
-c, --columns=name 指定列名,以逗號分隔.
-C, --compress Use compression in server/client protocol.
-#, --debug[=#] 編寫調試日誌.
--debug-check 程序退出時打印調試信息.
--debug-info 程序退出時打印調試信息,內存和CPU統計信息.
--default-auth=name Default authentication client-side plugin to use.
-d, --delete 導入前清空表數據.
--enable-cleartext-plugin
Enable/disable the clear text authentication plugin.
--fields-terminated-by=name 字段分隔符
--fields-enclosed-by=name 字段包括符
--fields-optionally-enclosed-by=name 列可選包括符
--fields-escaped-by=name 列終止符
-f, --force 忽略SQL error強制執行
-?, --help 幫助信息展示
-h, --host=name 將數據導入給定主機上的MySQL服務器,默認主機爲localhost.
-i, --ignore 數據重複導入時,保存舊的.
--ignore-lines=# 忽略導入文件前n行.
--lines-terminated-by=name 行終止符
-L, --local 讀取客戶端本地數據文件.
-l, --lock-tables 鎖表以寫入
--low-priority Use LOW_PRIORITY when updating the table.
-p, --password[=name] 用戶密碼
--plugin-dir=name 客戶端plugins路徑.
-P, --port=# 連接端口.
--protocol=name protocol名(tcp, socket, pipe, memory).
-r, --replace 數據重複時,替換舊的.
-s, --silent 靜默模式,僅在發生錯誤時才產生輸出.
-S, --socket=name 指定socket.
--server-public-key-path=name 包含RSA公鑰的文件的路徑名.
--ssl-mode=name 與服務器連接的所需SSL
--ssl-ca=name 包含受信任的SSL證書頒發機構列表的文件
--ssl-capath=name 包含受信任的SSL證書頒發機構證書文件的目錄
--ssl-cert=name 包含X.509證書的文件
--ssl-cipher=name 連接加密的允許密碼
--ssl-key=name 包含X.509密鑰的文件
--ssl-crl=name 包含證書吊銷列表的文件
--ssl-crlpath=name 包含證書吊銷列表文件的目錄
--tls-version=name 允許的TLS協議進行加密連接,允許的值有: TLSv1, TLSv1.1, TLSv1.2, TLSv1.3
--ssl-fips-mode=name 是否在客戶端啓用FIPS模式,允許的值有: OFF, ON, STRICT
--tls-ciphersuites=name 允許的TLSv1.3密碼套件用於加密連接
--use-threads=# 多線程執行.
-u, --user=name 用戶名.
-v, --verbose 詳細信息
-V, --version 顯示版本信息並退出
--compression-algorithms=name 用於服務器連接的允許壓縮算法
--zstd-compression-level=# 與使用zstd壓縮的服務器的連接的壓縮級別,有效區間1-22,默認值3.
Variables (--variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
--------------------------------- ----------------------------------------
bind-address (No default value)
character-sets-dir (No default value)
default-character-set auto
columns (No default value)
compress FALSE
default-auth (No default value)
delete FALSE
enable-cleartext-plugin FALSE
fields-terminated-by (No default value)
fields-enclosed-by (No default value)
fields-optionally-enclosed-by (No default value)
fields-escaped-by (No default value)
force FALSE
host (No default value)
ignore FALSE
ignore-lines 0
lines-terminated-by (No default value)
local FALSE
lock-tables FALSE
low-priority FALSE
plugin-dir (No default value)
port 0
replace FALSE
silent FALSE
socket (No default value)
server-public-key-path (No default value)
get-server-public-key FALSE
ssl-ca (No default value)
ssl-capath (No default value)
ssl-cert (No default value)
ssl-cipher (No default value)
ssl-key (No default value)
ssl-crl (No default value)
ssl-crlpath (No default value)
tls-version (No default value)
tls-ciphersuites (No default value)
use-threads 0
user (No default value)
verbose FALSE
compression-algorithms (No default value)
zstd-compression-level 3
2. mysqlimport導入簡單示例
2.1 t1.txt導入到t1表
[root@chengyu ~]# vim /home/t1.txt
chn
sdsa
kjo
hjh
iods
[root@chengyu ~]# mysqlimport -uroot -p dbcy --columns=tname /home/t1.txt
Enter password:
mysqlimport: Error: 1290, The MySQL server is running with the --secure-file-priv option so it cannot execute this statement, when using table: t1
[root@chengyu ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.20 Source distribution
Copyright (c) 2000, 2020, 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 '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_file_priv | NULL |
+------------------+-------+
1 row in set (0.04 sec)
# secure_file_priv 參數是隻讀參數,不能使用set global命令修改
mysql> set global secure_file_priv='/home';
ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable
去my.cnf文件修改一下secure_file_priv參數:
[root@chengyu ~]# vim /usr/local/mysql8/my.cnf
secure_file_priv='/home'
[root@chengyu ~]# systemctl restart mysqld8
[root@chengyu ~]# systemctl status mysqld8
● mysqld8.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld8; bad; vendor preset: disabled)
Active: active (running) since Mon 2020-06-29 10:36:11 CST; 8s ago
Docs: man:systemd-sysv-generator(8)
Process: 29479 ExecStop=/etc/rc.d/init.d/mysqld8 stop (code=exited, status=0/SUCCESS)
Process: 29508 ExecStart=/etc/rc.d/init.d/mysqld8 start (code=exited, status=0/SUCCESS)
Tasks: 40
CGroup: /system.slice/mysqld8.service
├─29521 /bin/sh /usr/local/mysql8/bin/mysqld_safe --datadir=/home/mysql8/data --pid-file=...
└─29713 /usr/local/mysql8/bin/mysqld --basedir=/usr/local/mysql8 --datadir=/home/mysql8/d...
Jun 29 10:36:08 chengyu systemd[1]: Starting LSB: start and stop MySQL...
Jun 29 10:36:11 chengyu mysqld8[29508]: Starting MySQL... SUCCESS!
Jun 29 10:36:11 chengyu systemd[1]: Started LSB: start and stop MySQL.
[root@chengyu ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 Source distribution
Copyright (c) 2000, 2020, 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 '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| secure_file_priv | /home/ |
+------------------+--------+
1 row in set (0.01 sec)
再次執行導入:
[root@chengyu ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.20 Source distribution
Copyright (c) 2000, 2020, 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 '\h' for help. Type '\c' to clear the current input statement.
mysql> use dbcy
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t1;
+-----+-----------+
| tid | tname |
+-----+-----------+
| 1 | 錢鍾書 |
| 2 | 楊絳 |
| 3 | 路遙 |
| 6 | 魯迅 |
| 8 | 三毛 |
+-----+-----------+
5 rows in set (0.01 sec)
[root@chengyu ~]# mysqlimport -uroot -p dbcy --columns=tname /home/t1.txt
Enter password:
dbcy.t1: Records: 5 Deleted: 0 Skipped: 0 Warnings: 0
[root@chengyu ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.20 Source distribution
Copyright (c) 2000, 2020, 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 '\h' for help. Type '\c' to clear the current input statement.
mysql> use dbcy
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t1;
+-----+-----------+
| tid | tname |
+-----+-----------+
| 1 | 錢鍾書 |
| 2 | 楊絳 |
| 3 | 路遙 |
| 6 | 魯迅 |
| 8 | 三毛 |
| 9 | chn |
| 10 | sdsa |
| 11 | kjo |
| 12 | hjh |
| 13 | iods |
+-----+-----------+
10 rows in set (0.00 sec)
2.2 mysqlimport導入包含自增序列的數據
mysql> show variables like 'sql_mode';
+---------------+--------------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------------+
| sql_mode | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+---------------+--------------------------------------------+
1 row in set (0.01 sec)
mysql> desc t1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| tid | int | NO | PRI | NULL | auto_increment |
| tname | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> select * from t1;
+-----+-----------+
| tid | tname |
+-----+-----------+
| 1 | 錢鍾書 |
| 2 | 楊絳 |
| 3 | 路遙 |
| 6 | 魯迅 |
| 8 | 三毛 |
| 9 | chn |
| 10 | sdsa |
| 11 | kjo |
| 12 | hjh |
| 13 | iods |
+-----+-----------+
10 rows in set (0.00 sec)
[root@chengyu ~]# vim /home/t1.txt
14 鍾一
15 趙二
16 張三
17 李四
18 王五
[root@chengyu ~]# mysqlimport -uroot -p dbcy --columns=tid,tname /home/t1.txt
Enter password:
mysqlimport: Error: 1265, Data truncated for column 'tid' at row 1, when using table: t1
Bye
[root@chengyu ~]# vim /home/t1.txt
,鍾一
,趙二
,張三
,李四
,王五
[root@chengyu ~]# mysqlimport -uroot -p --fields-terminated-by="," dbcy --columns=tid,tname /home/t1.txt
Enter password:
mysqlimport: Error: 1366, Incorrect integer value: ' ' for column 'tid' at row 1, when using table: t1
[root@chengyu ~]# vim /home/t1.txt
0,鍾一
0,趙二
0,張三
0,李四
0,王五
[root@chengyu ~]# mysqlimport -uroot -p --fields-terminated-by="," dbcy --columns=tid,tname /home/t1.txt
Enter password:
dbcy.t1: Records: 5 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from t1;
+-----+--------+
| tid | tname |
+-----+--------+
| 16 | 鍾一 |
| 17 | 趙二 |
| 18 | 張三 |
| 19 | 李四 |
| 20 | 王五 |
+-----+--------+
5 rows in set (0.00 sec)
注意:這裏已有的t1表的數據全部被delete後才導入文本文件的新數據,在STRICT_TRANS_TABLES的SQL_MODE下,指定自增長列爲0的作用是生成下一序列號,並不是賦值操作。
再試試空置自增列或賦“null”值自增列的情況:
[root@chengyu ~]# vim /home/t1.txt
null,鍾一
null,趙二
null,張三
null,李四
null,王五
[root@chengyu ~]# mysqlimport -uroot -p --fields-terminated-by="," dbcy --columns=tid,tname /home/t1.txt
Enter password:
mysqlimport: Error: 1366, Incorrect integer value: 'null' for column 'tid' at row 1, when using table: t1
[root@chengyu ~]# vim /home/t1.txt
,鍾一
,趙二
,張三
,李四
,王五
[root@chengyu ~]# mysqlimport -uroot -p --fields-terminated-by="," dbcy --columns=tid,tname /home/t1.txt
Enter password:
mysqlimport: Error: 1366, Incorrect integer value: '' for column 'tid' at row 1, when using table: t1
總言之,包含自增列的數據導入請考量SQL_MODE的情況,另外導入之前做好已有數據的備份和測試工作,方法千千萬,第一條安全備份第一步,小心謹慎!
其他情況,可以使用–replace或者–delete在導入文本文件數據時處理表已有數據,還有分隔符之類的指定跟LOAD DATA一致,這裏就不贅述了,可以看看另一篇https://blog.csdn.net/u010257584/article/details/106895618。
2020年6月29日
十點匆忙從北門滴滴約車趕回單位,司機師傅卻閒散的帶着我繞圈圈遊車河,質疑兩次都被他的“閃爍言辭”略過去了,起步價就到的距離硬生生耗到12元停車,清華路-香園路-宏圖路-城市風景街-莞太路-三元路…一個悄咪咪的繞着,一個靜悄悄的看着,莫名想笑…