Linux—编写shell脚本操作数据库执行sql
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linux—编写shell脚本操作数据库执行sql,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2261字,纯文字阅读大概需要4分钟。
内容图文
![Linux—编写shell脚本操作数据库执行sql](/upload/InfoBanner/zyjiaocheng/876/dd906de5a6e64e13bbb558334ef3f406.jpg)
修改数据库数据
??在升级应用时,我们常常会遇到升级数据库的问题,这就涉及到sql脚本的编写。
??一般我们会通过写sql脚本,然后将xxx.sql脚本放到数据库中进行source xxx.sql
执行。本篇文章,我们可以通过写shell脚本来执行数据库操作。
配置文件
创建 test_sql.properties 作为shell脚本的外部配置参数修改:
[andya@liunx01 sql_sh]$ vim test_sql.properties
-rwxr-x--- 1 bdoc bdoc 1238 Mar 19 14:00 test_sql.sh
# 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
内容总结
以上是互联网集市为您收集整理的Linux—编写shell脚本操作数据库执行sql全部内容,希望文章能够帮你解决Linux—编写shell脚本操作数据库执行sql所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。