++++++++++++++++++++++++++++++++++++++++++++++++++++++
秀脱linux实战笔记之mysql快速批量导入文本数据实战篇:
QQ: 6541657
EMAIL: xiutuo@gmail.com
---------------------------------------------
环境:
centos5.1
mysql5.0.82:安装在/usr/local/mysql目录下
windows下有一文本文件testlog.txt,数据格式是以tab分割,行之间用换行符分割。数据量有数千万条数据,需要导入mysql数据库中,
测试用的数据库xiutuotestdb,表xiutuolog,该库和该表字符集编码都是utf8编码,使用连接用户root,密码123456
难点:mysql库时采用utf8编码,表也是utf8编码,导入报错,有乱码,把该文本文件转化成utf8编码后,导入成功。
++++++++++++++++++++++++++++++++++++++++++++++++++++++
一.文件字符集转化工具enca,千万别用iconv去转(百分比转不成功)
1.下载enca,下载的是enca-1.9-1.el4.src.rpm放在/home/xiutuo/software
2.安装enca
# cd /home/xiutuo/software
# rpm -i enca-1.9-1.el4.src.rpm
# cd /usr/src/redhat/SPECS
# rpmbuild -bb enca.spec
# cd /usr/src/redhat/RPMS/i386
# rpm -ivh enca-1.9-1.i386.rpm
3.转化文本文件编码,先把该文件上传到linux系统中/home/xiutuo/software目录下(上传方法自选)
# cd /home/xiutuo/software
# 转化编码,转化后的文件名跟转化前是一样的。
# enca -L zh_cn -x utf8 testlog.txt
# 查看编码,出现utf8字样就说明转化成功了
# enca -L zh_cn -d testlog.txt
2.批量导入文本数据的mysql命令:
# /usr/local/mysql/bin/mysql -h localhost -uroot -p123456
mysql> use xiutuotestdb;
mysql> load data local infile "/home/xiutuo/software/testlog.txt" into table xiutuolog fields terminated by '\t' lines terminated by '\r\n';
命令说明:
# mysql安装目录/bin/mysql -h localhost -uUSER -pUSER_PASSWORD
Your MySQL connection id is 669
Server version: 5.0.82-log MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> load data local infile "要导入的文本文件" into table 表名 fields terminated by '\t' lines terminated by '\r\n';
其中fields terminated by 表示字段分隔符
lines terminated by 表示行分隔符(就是行与行之间的分隔符)