(1)使用 load data 导入数据,首先要准备一个文件,本例是test.txt
mysql> load data local infile '/root/test.txt' ignore into table login character set utf8 fields terminated by ',' lines terminated by ';';
test.txt里有对应数据库表login的所有列,以下是这条命令的一些说明(详细解释传送文末参考链接)
ignore -> 代表已有该记录则跳过(比如插库的时候程序写了一条操作记录)
character set utf8 -> 为了解决中文字符问题(具体应该和数据库表设置的一致)
fields terminated by ',' -> 每条记录中以逗号分隔列
lines terminated by ';' -> 记录以分号分隔记录
# tail -3 test.txt
11145,test1,登录,2019-06-03 14:53:30,FAIL,密码错误,188.188.188.188;
11146,test1,登录,2019-06-03 14:53:30,FAIL,密码错误,188.188.188.188;
11147,test1,登录,2019-06-03 14:53:30,FAIL,密码错误,188.188.188.188
注意:最后一条记录结尾不能带分号,否则会多插入一条记录,仅有主键(该例是id自增)其它列值为NULL
但是这样处理,插入数据后,最后条记录的最后一列的边线会往前错位(不影响功能……但是强迫症看着很不舒服……)
解决:各种试,发现导入的文件最后一行结尾加个逗号(列分隔符)就好啦,如下
# tail -3 test.txt
11145,test1,登录,2019-06-03 14:53:30,FAIL,密码错误,188.188.188.188;
11146,test1,登录,2019-06-03 14:53:30,FAIL,密码错误,188.188.188.188;
11147,test1,登录,2019-06-03 14:53:30,FAIL,密码错误,188.188.188.188,
(2)生成导入的文件
因为不要求内容,只需要一定的数据量,写了以下shell脚本生成重复数据到需要导入的文件test.txt
#!/bin/bash
MYSQL_PATH=/opt/mysql
USERNAME="somename"
PASSWORD="somepasswd"
DBNAME="somedb"
result=`${MYSQL_PATH}/bin/mysql -u${USERNAME} -p${PASSWORD} ${DBNAME} -e"select id from login order by id DESC limit 1;"`
#echo $result #eg: id 1122
pri_id=`echo ${result} | awk '{print $2}'`
#echo $pri_id #看现在到哪个id了 eg: 1122
id=$(($pri_id+1))
#echo $id #eg: 1123
rm -rf /root/test.txt
num=5 # 想生成多少条记录写在这,这种方法导入几万条很快的
for((i=1;i<num;id++,i++))
do echo "$id,test1,登录,2019-06-10 08:53:30,SUCCESS,\N,188.188.188.188;" >> /root/test.txt # "\N"对应表里的"NULL"
done
echo "$id,test1,登录,2019-06-03 14:53:30,FAIL,密码错误,188.188.188.188," >> /root/test.txt
${MYSQL_PATH}/bin/mysql -u${USERNAME} -p${PASSWORD} ${DBNAME} -e"load data local infile '/root/test.txt' ignore into table login character set utf8 fields terminated by ',' lines terminated by ';';"
(附)数据库表login结构示例,其中ip是胡乱写的:
mysql> select * from login limit 2;
+----+----------+---------+---------------------+-----------+--------------+-----------------+
| id | username | logname | logtime | logresult | opcontent | ip |
+----+----------+---------+---------------------+-----------+--------------+-----------------+
| 1 | test1 | 登录 | 2019-06-10 09:46:03 | FAIL | 密码错误 | 188.188.188.188 |
| 2 | test1 | 登录 | 2019-06-10 09:46:10 | SUCCESS | NULL | 188.188.188.188 |
+----+----------+---------+---------------------+-----------+--------------+-----------------+
2 rows in set (0.00 sec)
mysql
————————————————
版权声明:本文为CSDN博主「wy_hhxx」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wy_hhxx/article/details/90900117