如何在Bash中保持MySQL连接打开

编程入门 行业动态 更新时间:2024-10-26 20:22:56
本文介绍了如何在Bash中保持MySQL连接打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个bash脚本,可以多次调用MySQL.不必重新连接到MySQL,有没有办法使连接保持打开状态?理想情况下,如果脚本提前退出,则连接将关闭.我认为命名管道可以工作,但它们会保持打开状态.

I have a bash script that calls MySQL several times. Instead of having to reconnect to MySQL, is there a way to keep the connection open? Ideally, the connection would close if the script exits early. I'm thinking named pipes would work but they would stay open.

这是我希望找到的一个简短的伪示例:

Here's a quick pseudo-example of what I hope to find:

openMySQL executeMySQL "SELECT 1" exit 1 executeMySQL "SELECT 2"

我正在寻找openMySQL和executeMySQL函数,在这些函数中MySQL连接实际上会在exit 1期间关闭.

I'm looking for the openMySQL and executeMySQL functions where the MySQL connection will actually close during the exit 1.

推荐答案

我已经找到了我想要的一部分.

I have part of what I was looking for.

使用fd = 3保持mysql连接的打开状态:

Keep the mysql connection open using fd=3 for writing:

exec 3> >(mysql) echo "SELECT 1;" >&3 echo "SELECT 2;" >&3 exec 3>&-

使用fd = 3读取以保持mysql连接打开:

Keep the mysql connection open using fd=3 for reading:

exec 3< <(echo "SELECT 1;SELECT 2;"|mysql|sed '1d') while read <&3 do echo $REPLY done

注意:sed'1d'删除了标头.

Note: sed '1d' removes the header.

有什么方法可以合并这些文件,以便您可以写入一个fd并从另一个fd读取?

Is there any way to merge these so you can write to one fd and read from another?

更多推荐

如何在Bash中保持MySQL连接打开

本文发布于:2023-11-06 17:46:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Bash   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!