MySQLimport位于MySQL/bin目录中,是MySQL的一个载入(或者说导入)数据的一个非常有效的命令行工具。
使用mysqlimport -?命令,可以查看mysqlimport的具体参数及详细说明。下表是一些常见的选项:
-c, --columns=name | Use only these columns to import the data to. Give the column names in a comma separated list. This is same as giving columns to LOAD DATA INFILE. | 该选项采用用逗号分隔的列名作为其值。列名的顺序指示如何匹配数据文件列和表列。 |
-C, --compress | Use compression in server/client protocol. | 压缩在客户端和服务器之间发送的所有信息(如果二者均支持压缩) |
-d, --delete | First delete all rows from table. | 新数据导入数据表中之前删除数据数据表中的所有信息 |
--fields-terminated-by=name | Fields in the textfile are terminated by … | 指定数据之间的分隔符。默认的分隔符是跳格符(Tab) |
--fields-enclosed-by=name | Fields in the importfile are enclosed by ... | 指定文本文件中数据的记录是以什么括起的, 很多情况下数据以双引号括起。 默认的情况下数据是没有被字符括起的 |
--fields-optionally-enclosed-by=name | Fields in the i.file are opt. enclosed by … | 字段包括符,只用在CHAR和VERCHAR字段上 |
--fields-escaped-by=name | Fields in the i.file are escaped by ... | 转义字符 |
-f, --force | Continue even if we get an sql-error. | 不管是否遇到错误,MySQLimport将强制继续插入数据 |
-?, --help | Displays this help and exits. | 显示帮助消息并退出 |
-h, --host=name | Connect to host. | 将数据导入给定主机上的MySQL服务器。默认主机是localhost |
-i, --ignore | If duplicate unique key was found, keep old row. | 跳过或者忽略那些有相同唯一关键字的行, 导入文件中的数据将被忽略 |
--ignore-lines=# | Ignore first n lines of data infile. | 忽视数据文件的前n行 |
--lines-terminated-by=name | Lines in the i.file are terminated by ... | 行记录分隔符。 默认的情况下MySQLimport以newline为行分隔符 |
-L, --local | Read all files through the client. | 从本地客户端读入输入文件 |
-l, --lock-tables | Lock all tables for write (this disables threads). | 数据被插入之前锁住表,防止在更新数据库时,用户的查询和更新受到影响 |
--low-priority | Use LOW_PRIORITY when updating the table. | 低优先级 |
-p, --password[=name] | Password to use when connecting to server. If password is not given it's asked from the tty. | 提示输入密码 |
-W, --pipe | Use named pipes to connect to server. | 使用命名管道连接服务器 |
-P, --port=# | Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306). | 用于连接的TCP/IP端口号 |
--protocol=name | The protocol of connection (tcp,socket,pipe,memory). | 连接使用的协议 |
-r, --replace | If duplicate unique key was found, replace old row. | 与-i选项的作用相反;此选项将替代表中有相同唯一关键字的记录 |
--shared-memory-base-name=name | Base name of shared memory. | 共享内存连接名。该选项只用于Windows |
-s, --silent | Be more silent. | 沉默模式。只有出现错误时才输出 |
-S, --socket=name | Socket file to use for connection. | 当连接localhost时使用的套接字文件(为默认主机) |
--use-threads=# | Load files in parallel. The argument is the number of threads to use for loading data. | 并行多线程导入个数 |
-u, --user=name | User for login if not current user. | 连接服务器时MySQL使用的用户名 |
-v, --verbose | Print info about the various stages. | 冗长模式。打印出程序操作的详细信息 |
-V, --version | Output version information and exit. | 显示版本(version) |
示例:
C:\Users\qxl>mysql -e "create table test.test_import(user_id int,user_name varchar(20))"
ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
C:\Users\qxl>mysql -uroot -e "create table test.test_import(user_id int,user_name varchar(20))"
C:\Users\qxl>type h:\test_import.sql
'1','Roses'
C:\Users\qxl>mysqlimport -uroot --fields-terminated-by=, --fields-enclosed-by=' test h:\test_import.sql
test.test_import: Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
C:\Users\qxl>mysql -uroot -e "select * from test.test_import"
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 1 | Roses |
+---------+-----------+