[转帖]Linux—编写shell脚本操作数据库执行sql
Linux—编写shell脚本操作数据库执行sql
修改数据库数据
在升级应用时,我们常常会遇到升级数据库的问题,这就涉及到sql脚本的编写。
一般我们会通过写sql脚本,然后将xxx.sql脚本放到数据库中进行source xxx.sql执行。本篇文章,我们可以通过写shell脚本来执行数据库操作。
配置文件
创建 test_sql.properties 作为shell脚本的外部配置参数修改:
[andya@liunx01 sql_sh]$ vim test_sql.properties
# set parameters start
# 1 db name
dbName="db01"
# 2 the valueof net speeds and requests
netMaxSpeeds=500
netRequests="test.t1"
# 3 database info
## mysql address
MYSQL_ADDRESS="10.127.0.1"
## database name
MYSQL_DATABASE_NAME="db_test"
## 5.3 bdoc connect mysql user name
MYSQL_USER="user01"
## 5.4 bdoc connect mysql user password
MYSQL_PASSWD="123456"
## 5.5 mysql engine
DATABASE_ENGINE=mysql
shell脚本
创建shell脚本test_sql.sh
[andya@liunx01 sql_sh]$ vim test_sql.sh
#!/bin/bash
starttime=$(date +%Y-%m-%d\ %H:%M:%S)
echo "【Start to execute the script】, start time is: " $starttime >> test_sql_sh.log
# 1 read parameters
# ===================================================================
echo "------ test_sql.properties start------" >> test_sql_sh.log
source ./test_sql.properties
echo "Parameters: cat test_sql.properties" >> test_sql_sh.log
while read line
do
echo $line >> test_sql_sh.log ;
done < test_sql.properties
echo "------ test_sql.properties end------" >> test_sql_sh.log
# =================================================================
# 2 update database
# ========================
testSql="
SET @dbId=(SELECT id FROM ${MYSQL_DATABASE_NAME}.\`test_tb01\` WHERE \`NAME\` = \"${dbName}\");
INSERT INTO ${MYSQL_DATABASE_NAME}.\`test_tb02\` (\`NAME\`, \`DB_ID\` ,\`MAX_SPEEDS\`, \`NET_REQUESTS\`) VALUES ('${dbName}', @dbId, '${netMaxSpeeds}', '${netRequests}');
"
echo -e "\nSql: add hbase sql is: "${testSql} >> test_sql_sh.log
id=$(${DATABASE_ENGINE} -h${MYSQL_ADDRESS} -u${MYSQL_USER} -p${MYSQL_PASSWD} -D ${MYSQL_DATABASE_NAME} -e "${testSql}")
echo "Sql: Modify db data successfully, and insert db id is: "${id} >> test_sql_sh.log
endtime=`date +"%Y-%m-%d %H:%M:%S"`
echo "【Execute the script end】, end time is: " ${endtime} >> test_sql_sh.log
echo -e "\n" >> test_sql_sh.log
exit 0
脚本执行
./test_sql.sh
并且可以查看到输出日志test_sql_sh.log
另一种连接方式(待研究)
#!/bin/sh
mysql_engine=`which mysql`
${mysql_engine} -uroot -p123456 <<EOF 1>>test.log
use db01;
select * from tb01 where id = 4;
EOF
exit 0
其中:
1)1>>test.log是重定向标准输出到test.log中,当然,也尝试去掉1,也是可以输出。
2)我们也可以使用2 >/dev/null重定向来屏蔽错误信息,2即为标准错误输出,对于linux来说/dev/null即为空设备,输入进去的数据即为丢弃。
3)EOF表示后续输入作为shell的输入,直到下一个EOF出现,再返回主进程shell中。
[转帖]Linux—编写shell脚本操作数据库执行sql的更多相关文章
- Linux编写Shell脚本
——<Linux就该这么学>笔记Shell脚本命令的工作方式有两种 交互式: 用户每输入一条命令就立即执行 批处理: 由用户事先编写好一个完整的Shell脚本,Shell会一次性执行脚本中 ...
- Shell 脚本操作数据库实战
安装mariadb 数据库 (默认没有密码,直接mysql即可进入数据库管理控制台) yum install mariadb mariadb-server mariadb-libs -y syste ...
- Linux编写Shell脚本入门
一. 一般编写shell需要分3个步骤 1. 新建一个脚本文件,并编写程序 vi hello.sh #!/bin/bash #注释 #输出 printf '%s\n' "Hello Worl ...
- linux脚本错误: line *: [: missing `]',linux编写shell脚本时的注意点
转载:https://www.cnblogs.com/bovenson/p/4548079.html 关于shell的一些注意点,粘贴自拉钩教育精选评论:测试开发核心技术 46 讲-->第6讲 ...
- shell脚本操作数据库
#!/bin/bash HOST_NAME="localhost" PORT=3306 USERNAME="root" PASSWORD="root& ...
- Shell脚本备份数据库
使用crontab 定时备份数据库 1. 编辑crontab 规则,定时执行脚本 2. 在my.cnf 文件中加 [mysqldump] user=root password=密码 3.编写shell ...
- [转帖]编写shell脚本所需的语法和示例
编写shell脚本所需的语法和示例 https://blog.csdn.net/CSDN___LYY/article/details/100584638 在说什么是shell脚本之前,先说说什么是sh ...
- linux 的基本操作(编写shell 脚本)
终于到shell 脚本这章了,在以前笔者卖了好多关子说shell脚本怎么怎么重要,确实shell脚本在linux系统管理员的运维工作中非常非常重要.下面笔者就带你正式进入shell脚本的世界吧. 到现 ...
- shell 脚本操作informix数据库
shell 脚本操作informix数据库的简单模板: functionName(){ dbaccess << ! database 库名; sql语句; ! } 栗子1:更新数据 fun ...
- Linux中编写Shell脚本
目录 Shell Shell脚本的执行 Shell脚本编写规范 Shell 中的变量 变量的算术运算 双小括号 (()) 数值运算命令的用法 let 运算命令的用法 expr 命令的用法 br 命令 ...
随机推荐
- Windows中开启自动dump的方法
@echo off echo 正在启用Dump... reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error ...
- 开源:Taurus.DTC 微服务分布式事务框架,支持 .Net 和 .Net Core 双系列版本
前言: 在经过1年多的深思,十几年的框架编写技术沉淀下,花了近一个月的时间,终于又为 .Net 及 .Net Core 的微服务系列框架贡献当中的一个重要组件. 1.开源地址: https://git ...
- AntDesignBlazor示例——暗黑模式
本示例是AntDesign Blazor的入门示例,在学习的同时分享出来,以供新手参考. 示例代码仓库:https://gitee.com/known/BlazorDemo 1. 学习目标 暗黑模式切 ...
- flutter中去除导航栏与状态栏
方法一 SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom]); // ...
- spring-mvc 系列:拦截器和异常处理器(HandlerInterceptor、HandlerExceptionResolver)
目录 一.拦截器的配置 二.拦截器的三个抽象方法 三.多个拦截器的执行顺序 四.基于配置的异常处理器 五.基于注解的异常处理器 一.拦截器的配置 SpringMVC中的拦截器用于拦截控制器方法的执行 ...
- spring-mvc 系列:HttpMessageConverter(@RequestBody、RequestEntity、@ResponseBody、@RestController、ResponseEntity、文件上传下载)
目录 一.@RequestBody 二.RequestEntity 三.@ResponseBody 四.SpringMVC处理json 五.@RestController 六.ResponseEnti ...
- Langchain-Chatchat项目:3-Langchain计算器工具Agent思路和实现
本文主要讨论Langchain-Chatchat项目中自定义Agent问答的思路和实现.以"计算器工具"为例,简单理解就是通过LLM识别应该使用的工具类型,然后交给相应的工具( ...
- AppCube视角浅析: 艾瑞咨询《2022年中国低代码行业研究报告》
摘要:近日,艾瑞咨询发布了<2022年中国低代码行业研究报告>,报告从企业数字化发展背景.低代码的发展路径.应用渗透.市场规模等方面进行深入研究分析,并洞察了发展趋势. 本文分享自华为云社 ...
- PPT 动画-制作一个倒酒
波浪往左上方,慢慢运动 数字 渐入 + 渐出 + 居中对齐 酒杯绘制 波浪绘制 上方的点全部设成[平滑顶点] https://getwaves.io/ 快速生成波浪[Office 2016 不支持插入 ...
- AIGC
博客目录 本地部署modelscope-agent python 使用 Google Gemini API MetaGPT MetaGPT day01: MetaGPT作者代码走读.软件公司初始示例