另外关于load data还有几个小细节以及实验过程如下:
--local-infile选项
0 启动mysqld从服务器端禁用所有LOAD DATA LOCAL命令;1 启用LOAD DATA LOCA
如果LOAD DATA LOCAL INFILE在服务器或客户端被禁用,试图执行该语句的客户端将收到下面的错误消息:
ERROR 1148: The used command is not allowed with this MySQL version
(出现以上报错的时候就要检查下是否设置了--local-infile=1了,默认是0的)
1、在需要远程导入数据的主机上授权用户:
[MM-Writable@v031014.sqa.cm4 ~]
grant select,insert,update,delete on test.* to 'qianwei'@'%' identified by 'qianwei';
(远程登录的时候必须要有密码的帐号)
2、测试表结构如下:
[MM-Writable@v031014.sqa.cm4 ~]
root@test 12:01:06>show create table tmp_qw_test\G
*************************** 1. row ***************************
Table: tmp_qw_test
Create Table: CREATE TABLE `tmp_qw_test` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`user_id` bigint(20) NOT NULL,
`target_id` bigint(20) NOT NULL,
`group_id` bigint(20) NOT NULL,
`common_friend_count` int(11) NOT NULL,
`common_friends` varchar(512) NOT NULL,
`gmt_create` datetime NOT NULL,
`gmt_modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
root@test 12:09:00>select * from tmp_qw_test;
Empty set (0.00 sec)
4、远程导数据命令:
[MM-Writable@dev031033.sqa.cm4 /tmp]
$mysql -uqianwei -pqianwei test -h10.232.31.14 --local-infile=1 --show-warnings -v -v -v -e "load data LOCAL infile '/tmp/tmp_qw_test.txt' REPLACE into table tmp_qw_test fields terminated by ',' ENCLOSED BY '\"' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends)";
--------------
load data LOCAL infile '/tmp/tmp_qw_test.txt' REPLACE into table tmp_qw_test fields terminated by ',' ENCLOSED BY '"' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends)
--------------
5、文件中的双引号不进行过滤时,时间字段会被截断变为0:
[MM-Writable@dev031033.sqa.cm4 /tmp]
$mysql -uqianwei -pqianwei test -h10.232.31.14 --local-infile=1 --show-warnings -v -v -v -e "load data LOCAL infile '/tmp/tmp_qw_test.txt' REPLACE into table tmp_qw_test fields terminated by ',' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends)";
--------------
load data LOCAL infile '/tmp/tmp_qw_test.txt' REPLACE into table tmp_qw_test fields terminated by ',' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends)
--------------
Warning (Code 1364): Field 'gmt_modified' doesn't have a default value
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 1
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 2
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 3
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 4
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 5
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 6
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 7
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 8
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 9
Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 10
Bye
6、设置gmt_modified字段取值和gmt_create一致:
[MM-Writable@dev031033.sqa.cm4 /tmp]
$mysql -uqianwei -pqianwei test -h10.232.31.14 --local-infile=1 --show-warnings -v -v -v -e "load data LOCAL infile '/tmp/tmp_qw_test.txt' REPLACE into table tmp_qw_test fields terminated by ',' ENCLOSED BY '\"' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends) set gmt_modified=gmt_create";
--------------
load data LOCAL infile '/tmp/tmp_qw_test.txt' REPLACE into table tmp_qw_test fields terminated by ',' ENCLOSED BY '"' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends) set gmt_modified=gmt_create
--------------