修改参数 ,开启into outfile的功能
secure_file_priv='/home/backups/mysql_outfile'
重启数据库是参数生效
按条件导出MySQL数据
select * from receipt_receive_log where gmt_create >= '2020-04-13 00:00:00' and gmt_create<= '2020-07-13 00:00:00' INTO OUTFILE '/home/backups/mysql_outfile/receipt_receive_log_2020_q1.csv' FIELDS TERMINATED BY ',' ENCLOSED BY "'" LINES TERMINATED BY '\n';
FIELDS TERMINATED BY ',' 字段用逗号分割
ENCLOSED BY "'" 每个字段的数据用引号
LINES TERMINATED BY '\n' 每行数据换行
导出表头
注意表头的字段不能加引号,否则导入到MongoDB的时候字段名也会有引号
SELECT column_name FROM information_schema.columns WHERE table_schema = 'workingplatform_prod' AND table_name = 'receipt_receive_log' INTO OUTFILE '/home/backups/mysql_outfile/receipt_receive_log_tableHead.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY ',';
创建导入账号
在要导入数据的MongoDB数据库下创建一个账号
db.createUser( {user: "user_name",pwd: "123",roles: [ { role: "readWrite", db: "db_name" }] })
使用mongoimport导入
mongoimport --headerline --type=csv -d workingplatform_test -c receipt_receive_log_2020_q1 -h --port=27017 -u workingplatform_test_rw -p 123 --file=receipt_receive_log_q1.csv -vv
导入报错
Failed: read error on entry #231885: line 231886, column 7245: bare " in non-quoted-field
'单据转换错误:Unparseable date: ""',
根据报错查看数据 发现该条数据中有引号,引号会被识别为CSV文件类象的分隔符, 导致导入报错。