通过mysqlimport定时将远程文本文件导入mysql


文本文件存放在服务器a上,且文本文件的命名方式为yyyymmddhh,每小时导入24小时前生成的文件,而mysql安装在b上
首先在a上,从现有的文本文件截取所需的列(使用cut命令),然后传输到b。
b这台服务器既不能连接ftp服务器,也不能与其他机器配置用户等价性,因此向它传输文件只能使用scp,为了避免人机交互输入密码,采用了autoexpect命令
[root@justin]# autoexpect scp /var/www/data/info.txt root@b:/var/lib/mysql/market
autoexpect started, file is script.exp
root@b's password:
生成script.exp


[root@justin]# more generate_mysql_info.sh
#!/bin/bash

folder=/var/www/data/

#get the past time value
date1=`date --date='24 hour ago'  +%Y%m%d%H`

cat "$folder""$date1" | cut -d '"' -f 19,20,21,31,32,33,34,35,36,37,41,42,43,61,62,63,64 | cut -d ',' -f 2,4,5,6,8,10 > info.txt

cd $folder
./script.exp

[root@justin]# crontab -l | grep generate
#generate and scp the info.txt to b for testing
30 * * * * sh /var/www/data/generate_mysql_info.sh >> /var/www/data/generate_mysql_info.log 2>&1

传输到64之后,然后调用mysqlimport导入mysql
字段中包含中文,数据库采用utf8字符集,为避免乱码,mysql建表语句后面加上 DEFAULT CHARSET=utf8
[root@db market]# more import_into_mysql.sh
#!/bin/bash
cd /var/lib/mysql/market
mysqlimport --user=root --password=******* --fields-enclosed-by='"' --fields-terminated-by=',' --default-character-set='utf8'  market info.txt  

[root@db market]# crontab -l
#import the info.txt into market.info
50 * * * * sh /var/lib/mysql/market/import_into_mysql.sh >> /var/lib/mysql/market/import_into_mysql.log 2>&1
至此job完成

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15480802/viewspace-694353/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15480802/viewspace-694353/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值