1

I came across what I thought was a relatively simple problem, but haven't been able to find a solution. I wrote an extremely complex query in the MySQL shell and I'd like to save the query to a text file to use later and across systems (to use across redundant copies of the DB, and I unfortunately cannot copy paste).

Is there a simple way to do this, or will I have to manually retype the query into a text file?

5
  • Why can't you just copy/paste the sql text to a file like notepad? which shell you are using BTW? every shell has copy/paste feature. Commented Sep 11, 2015 at 19:53
  • Select. Copy. Paste. Commented Sep 11, 2015 at 19:54
  • I am using ubuntu server on a different macine than I'm on now. I'm logged into the mysql shell using mysql -u root -p Commented Sep 11, 2015 at 19:54
  • That's no issue. Which OS you are on (client machine)? Commented Sep 11, 2015 at 19:55
  • @Rahul I have a screen plugged into the server. So the host machine Commented Sep 11, 2015 at 19:56

1 Answer 1

2

Typing \e will open your default $EDITOR (usually vi or vim) with the last query you ran. You can then type : to input commands and save the file using w /path/to/myfile.sql.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this is exactly what I was looking for
You can also use a client on your mac or pc. they can connect via ssh tunnel if the database not direct reach. So its easyer to work. But the answer from Mureinik is also OK
@BerndBuffen I know you can, but I wanted a way to save it on the shell specifically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.