如何在Bash中保持MySQL连接打开 [英] How to Keep a MySQL Connection Open in Bash

查看:59
本文介绍了如何在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"

我正在寻找openMySQLexecuteMySQL函数,在这些函数中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连接打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆