大连比较好的建站公司,有什么网站做任务换q币吗,深圳的网站建设的公司,小程序推广平台有哪些修改数据库数据在升级应用时#xff0c;我们常常会遇到升级数据库的问题#xff0c;这就涉及到sql脚本的编写。一般我们会通过写sql脚本#xff0c;然后将xxx.sql脚本放到数据库中进行source xxx.sql执行。本篇文章#xff0c;我们可以通过写shell脚本来执行数据库操作。配…修改数据库数据在升级应用时我们常常会遇到升级数据库的问题这就涉及到sql脚本的编写。一般我们会通过写sql脚本然后将xxx.sql脚本放到数据库中进行source xxx.sql执行。本篇文章我们可以通过写shell脚本来执行数据库操作。配置文件创建 test_sql.properties 作为shell脚本的外部配置参数修改[andyaliunx01 sql_sh]$ vim test_sql.properties# set parameters start# 1 db namedbNamedb01# 2 the valueof net speeds and requestsnetMaxSpeeds500netRequeststest.t1# 3 database info## mysql addressMYSQL_ADDRESS10.127.0.1## database nameMYSQL_DATABASE_NAMEdb_test## 5.3 bdoc connect mysql user nameMYSQL_USERuser01## 5.4 bdoc connect mysql user passwordMYSQL_PASSWD123456## 5.5 mysql engineDATABASE_ENGINEmysqlshell脚本创建shell脚本test_sql.sh[andyaliunx01 sql_sh]$ vim test_sql.sh#!/bin/bashstarttime$(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.logsource ./test_sql.propertiesecho Parameters: cat test_sql.properties test_sql_sh.logwhile read linedoecho $line test_sql_sh.log ;done test_sql.propertiesecho ------ test_sql.properties end------ test_sql_sh.log# # 2 update database# testSqlSET 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.logid$(${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.logendtimedate %Y-%m-%d %H:%M:%Secho 【Execute the script end】, end time is: ${endtime} test_sql_sh.logecho -e \n test_sql_sh.logexit 0脚本执行./test_sql.sh并且可以查看到输出日志test_sql_sh.log另一种连接方式(待研究)#!/bin/shmysql_enginewhich mysql${mysql_engine} -uroot -p123456 test.loguse db01;select * from tb01 where id 4;EOFexit 0其中1)1test.log是重定向标准输出到test.log中当然也尝试去掉1也是可以输出。2)我们也可以使用2 /dev/null重定向来屏蔽错误信息2即为标准错误输出对于linux来说/dev/null即为空设备输入进去的数据即为丢弃。3)EOF表示后续输入作为shell的输入直到下一个EOF出现再返回主进程shell中。