分类: Mysql/postgreSQL
2007-08-29 11:34:52
最近在坛子里看到有人用mysqlimport的问题很多,自己写了点,希望各位指正。
我的环境是:windowsXP sp2,奔D双核2.8G,1G DDR2 533MHZ内存,MYSQL5.1
mysqlimport位于mysql/bin目录中,是mysql的一个载入(或者说导入)数据的一个非常有效的工具。这是一个命令行工具。
其实说白了就是LOAD DATA INFILE SQL语句的命令行接口。
1、请确定MYSQL已经启动:
C:\>net start mysql5
MySQL5 服务已经启动成功。
2、进入MYSQL
C:\>mysql_init.bat
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.1.17-beta-community-nt-debug MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use test
Database changed
mysql> select * from temp2;
+--------+--------+--------+--------+--------+--------+--------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+--------+--------+--------+--------+--------+--------+--------+
| "1" | "1" | "1" | "2" | "2" | "2" | "3" |
| "10" | "15" | "20" | "80" | "100" | "60" | "500" |
| "aaaa" | "bbbb" | "cccc" | "aaaa" | "bbbb" | "dddd" | "dddd" |
| "1" | "2" | "3" | "4" | "5" | "6" | "7" |
+--------+--------+--------+--------+--------+--------+--------+
4 rows in set (0.00 sec)
mysql> select * from temp2 into outfile 'c:\\temp2.sql' fields terminated by ','
lines terminated by '\n';
Query OK, 4 rows affected (0.00 sec)
mysql> exit
Bye
C:\>mysqlimport -uroot -p******** --fields-terminated-by=, --lines-terminated-b
y=\n test c:\\temp2.sql
test.temp2: Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
C:\>mysql_init.bat
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.1.17-beta-community-nt-debug MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use test
Database changed
mysql> select * from temp2;
+--------+--------+--------+--------+--------+--------+--------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+--------+--------+--------+--------+--------+--------+--------+
| "1" | "1" | "1" | "2" | "2" | "2" | "3" |
| "10" | "15" | "20" | "80" | "100" | "60" | "500" |
| "aaaa" | "bbbb" | "cccc" | "aaaa" | "bbbb" | "dddd" | "dddd" |
| "1" | "2" | "3" | "4" | "5" | "6" | "7" |
| "1" | "1" | "1" | "2" | "2" | "2" | "3" |
| "10" | "15" | "20" | "80" | "100" | "60" | "500" |
| "aaaa" | "bbbb" | "cccc" | "aaaa" | "bbbb" | "dddd" | "dddd" |
| "1" | "2" | "3" | "4" | "5" | "6" | "7" |
+--------+--------+--------+--------+--------+--------+--------+
8 rows in set (0.00 sec)
mysql>exit
Bye
3、文本文件的格式。
用户MYSQLIMPORT的文件必须具备有一定的格式才可以,而且最重要的是文件的名字必须同表的名字一摸一样。
还有就是提前要在服务器上有这个文件存在(LINUX:cat /dev/null > temp2.sql,至于WINDOWS上的就不用说了,这个人人都知道).
这里用SELECT INTO OUTFILE语句建立这个文件:
mysql> select * from temp2;
+--------+--------+--------+--------+--------+--------+--------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+--------+--------+--------+--------+--------+--------+--------+
| "1" | "1" | "1" | "2" | "2" | "2" | "3" |
| "10" | "15" | "20" | "80" | "100" | "60" | "500" |
| "aaaa" | "bbbb" | "cccc" | "aaaa" | "bbbb" | "dddd" | "dddd" |
| "1" | "2" | "3" | "4" | "5" | "6" | "7" |
| "1" | "1" | "1" | "2" | "2" | "2" | "3" |
| "10" | "15" | "20" | "80" | "100" | "60" | "500" |
| "aaaa" | "bbbb" | "cccc" | "aaaa" | "bbbb" | "dddd" | "dddd" |
| "1" | "2" | "3" | "4" | "5" | "6" | "7" |
+--------+--------+--------+--------+--------+--------+--------+
8 rows in set (0.00 sec)
mysql> select distinctrow * from temp2 into outfile 'c:\\temp2.sql' fields termi
nated by ',' lines terminated by '\n'
-> ;
Query OK, 4 rows affected (0.00 sec)
或者要去掉重复的行。
mysql> select * from temp2 into outfile 'c:\\temp2.sql' fields terminated by ','
lines terminated by '\n'
-> ;
Query OK, 8 rows affected (0.00 sec)
4、mysqlimport的常用选项介绍:
最最基本的是:
mysqlimport -v 显示版本。
比如我的:
C:\>mysqlimport -V -uroot -p********
mysqlimport Ver 3.6 Distrib 5.1.17-beta, for Win32 (ia32)
选项 功能
--character-sets-dir=path 字符集的安装目录
--default-character-set=charset_name
导入数据前默认的字符集,这个通常可以避免好多乱码问题。
-d or --delete 新数据导入数据表中之前清空表数据。
-f or --force 不管是否遇到错误,mysqlimport将强制继续插入数据
-i or --ignore mysqlimport跳过或者忽略那些有相同唯一
关键字的行, 导入文件中的数据将被忽略。
-l or -lock-tables 数据被插入之前锁住表,这样就防止了, 你在更新数据库时,用户的查询和更新受到影响。
-r or -replace 这个选项与-i选项的作用相反;此选项将替代 表中有相同唯一关键字的记录。
--fields-enclosed- by= char 指定文本文件中数据的记录时以什么括起的, 很多情况下 数据以双引号括起。 默认的情况下数据是没有被字符括起的。
--fields-terminated- by=char 指定各个数据的值之间的分隔符,在句号分隔的文件中, 分隔符是句号。您可以用此选项指定数据之间的分隔符。 默认的分隔符是跳格符(Tab)
--lines-terminated- by=str 此选项指定文本文件中行与行之间数据的分隔字符串 或者字符。 默认的情况下mysqlimport以newline为行分隔符。 您可以选择用一个字符串来替代一个单个的字符: 一个新行或者一个回车。