最近在罈子裏看到有人用mysqlimport的問題不少,本身寫了點,但願各位指正。
個人環境是:windowsXP sp2,奔D雙核2.8G,1G DDR2 533MHZ內存,MYSQL5.1
mysqlimport位於mysql/bin目錄中,是mysql的一個載入(或者說導入)數據的一個很是有效的工具。這是一個命令行工具。
其實說白了就是LOAD DATA INFILE SQL語句的命令行接口。
一、請肯定MYSQL已經啓動:
C:\>net start mysql5
MySQL5 服務已經啓動成功。
二、進入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
三、文本文件的格式。
用戶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)
四、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爲行分隔符。 您能夠選擇用一個字符串來替代一個單個的字符: 一個新行或者一個回車。
因此前面的語句能夠這樣寫:
C:\>mysqlimport -uroot -p******** --fields-terminated-by=, --lines-terminated-b
y=\n test c:\\temp2.sql -dlr
test.temp2: Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
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