Maison >base de données >tutoriel mysql >导入数据到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/ | +--------------------------+----------------------------+
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)