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"
I'm looking for the openMySQL
and executeMySQL
functions where the MySQL connection will actually close during the exit 1
.
To the best of my understanding your question: coproc's available in zsh/ksh and also bash v4+ might be similar to what you have in mind, e.g.
The command is kept running in the background, its stdin/stdout can accessed, it will finish (as a result its standard input closing/EOFing) as soon as the current shell exists...
I have part of what I was looking for.
Keep the mysql connection open using fd=3 for writing:
Keep the mysql connection open using fd=3 for reading:
Note: sed '1d' removes the header.
Is there any way to merge these so you can write to one fd and read from another?
I know this thread is old, but I was also looking for a comfortable bash mysql session implementation and didn't found something good enough for my needs, so I wrote my own one which I'd like to share with the world.
NOTES:
Example how to loop the returned data of a two column query
eg. "SELECT dt_id, dt_name FROM ..."