匯入資料到mysql的一種簡單的方法,匯入資料mysql
因為ubuntu預設內建的mysql版本為5.5,並不能使用load data infile這種進階的功能,因此我們寫了一個通用的指令碼來上傳檔案
shell指令碼
cat ./employee.csv | while read LINE do eval $( echo $LINE | awk -F ',' '{print "ds="$1 ";id="$2 ";name="$3}' ) echo ds=$ds id=$id name=$name mysql -uroot -p655453 test --default-character-set=utf8 -e "replace into wechat_employee values('$ds','$id','$name')"done
要匯入資料到Mysql最重要的一點就是要三碼合一,即client和server的編碼還有表的編碼要一致,server的編碼可以在/etc/mysql找一下設定檔修改,用戶端的編碼在insert之前可以設定一下,可以忽略
show variables like '%char%';+--------------------------+----------------------------+| Variable_name | Value |+--------------------------+----------------------------+| character_set_client | utf8 || character_set_connection | utf8 || character_set_database | utf8 || character_set_filesystem | binary || character_set_results | utf8 || character_set_server | utf8 || character_set_system | utf8 || character_sets_dir | /usr/share/mysql/charsets/ |+--------------------------+----------------------------+
查看錶的編碼,不是utf8的話要修改
mysql> show create table wechat_employee;+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| wechat_employee | CREATE TABLE `wechat_employee` ( `ds` varchar(20) DEFAULT NULL, `femployeeid` int(20) DEFAULT NULL, `femployeename` varchar(256) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 |+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
接著匯入,大功告成
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。