shell 操作mysql

卡死20分钟以上的任务

#!/bin/bash

HOST="127.0.0.1"
PORT="3306"
USERNAME="root"
PASSWORD="123456"
DBNAME="fofapro"

#定时删除user_id = 100063403 卡死20分钟以上的任务
update_sql="
update enterprise_ip_tasks set state = 'failure',updated_at = DATE_FORMAT(NOW(),'%Y-%m-%d %T') where id in (
  SELECT * FROM (
    (SELECT id FROM enterprise_ip_tasks where TIMESTAMPDIFF(MINUTE,created_at,DATE_FORMAT(NOW(),'%Y-%m-%d %T')) >=20  and user_id in (100063403) and result_file = '' and state = 'init') 
  as t
 )
);
"
mysql -h${HOST}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${update_sql}"

导出指定条件的ip列表

#!/bin/bash
echo "start"

HOST="127.0.0.1"
PORT="3306"
USERNAME="root"
PASSWORD="123456"
DBNAME="order"
userid="100039371"

mysql_export_ip="./dataip.txt"

tmp_file_data="./query333.json"
file_data="./esResultDate.json"

#导出数据user_id = 100009001 
selectsql="
SELECT ip from enterprise_task_assets where user_id=$userid and date(created_at) = date_sub(curdate(),interval 1 day); 
"
#mysql -h${HOST}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} --default-character-set=utf8 -e "${select}"
MYSQL="mysql -h${HOST}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} --default-character-set=utf8 -A -N"
#这里面有两个参数,-A、-N,-A的含义是不去预读全部数据表信息,这样可以解决在数据表很多的时候卡死的问题
#-N,很简单,Don't write column names in results,获取的数据信息省去列名称
result="$($MYSQL -e "$selectsql")"
echo "$result"
dump_data=$mysql_export_ip
>$dump_data
echo -e "$result" > $dump_data
#这里要额外注意,echo -e "$result" > $dump_data的时候一定要加上双引号,不让导出的数据会挤在一行

[Haima的博客] http://www.cnblogs.com/haima/
posted @ 2021-09-13 17:14  HaimaBlog  阅读(6)  评论(0编辑  收藏  举报