shell脚本导出mysql数据
如何实时查看数据信息呢,方式多的去了,但是还是脚本最为方便。
脚本导出到文件中demo示例
新建export.sh
#!/bin/bash
# 用户名
MYSQL_USER=root
# 密码
MYSQL_PWD=123456
# 库名
MYSQL_NAME=center
# 主机
MYSQL_HOST=172.0.0.1
# 端口号
MYSQL_PORT=3306
mysql -u${MYSQL_USER} -p${MYSQL_PWD} -P${MYSQL_PORT} -h${MYSQL_HOST} -D${MYSQL_NAME} << EOF > $2
set character_set_client = utf8;
set character_set_results = utf8;
set character_set_connection = utf8;
$1
EOF
执行脚本
$! $2 就是命令后面加的参数
sh export.sh 'select id,name from sport;' './export.txt'
搭配钉钉报警
钉钉机器人文档
https://developers.dingtalk.com/document/app/custom-robot-access
新建dingding.sh
#!/bin/bash
MYSQL_USER=root
MYSQL_PWD=123456
MYSQL_NAME=center
MYSQL_HOST=172.0.0.1
MYSQL_PORT=3306
SQL_DATA=`mysql -u${MYSQL_USER} -p${MYSQL_PWD} -P${MYSQL_PORT} -h${MYSQL_HOST} -D${MYSQL_NAME} << EOF
set character_set_client = utf8;
set character_set_results = utf8;
set character_set_connection = utf8;
select id,name from sport;
EOF`
echo $SQL_DATA
DATA="$(date +'%Y-%m-%d %T')
$SQL_DATA"
MSG='{"msgtype": "text",
"text": {
"content": "hello world chy
'$DATA'"
}
}'
echo $MSG
curl 'https://oapi.dingtalk.com/robot/send?access_token=abc' \
-H 'Content-Type: application/json' \
-d "$MSG
配合linux的定时任务
crontab -e
编辑新增(linux最小单位是分钟,如下表示1分钟执行一次)
*/1 * * * * sh /usr/local/shell/dingding.sh
perfect!!!
面朝大海```春暖花开