由于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)
接着导入,大功告成