使用shell脚本连接mysql shell脚本执行mysql语句

#!/bin/bash
# 连接mysql数据库,并查询结果,将结果输出到文件中
# 1.定义连接变量
mysql_u="root"
mysql_p="vaca1234!"
save_f="mysql.txt"
save_p="/root/bin/"
mysql_sq="select * from dwd.purchase_order_schedule
where date (order_create_time) = date_sub(current_date(),interval 1 day) limit 20; "
#export MYSQL_PWD=${mysql_p}
echo "开始链接数据库..."
# 2.连接数据库
result=`/usr/local/mysql8026/bin/mysql --socket=/tmp/mysql8026.sock -u$mysql_u -p$mysql_p << EOF
use dwd;
$mysql_sq;
quit
EOF`
# 判断是否连接成功
if [ $? = 0 ]
then
echo "连接成功。。。"
else
echo "连接失败。。。"
exit
fi
echo "写入查询结果..."
# 将结果写入文本
echo "$result" >> $save_p$save_f
echo "写入完成..."  

  

posted @ 2021-09-24 10:17  SailorG  阅读(4861)  评论(0编辑  收藏  举报