因為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/ | +--------------------------+----------------------------+
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)