How to change directory in mysql command line tool?

Tomas picture Tomas · Jul 13, 2016 · Viewed 14k times · Source

I am using mysql command line client under Linux to work with MySQL database. How can I change current working directory? I did not find it in the documentation.


EDIT: This is not duplicate of the mentioned question, because OP limited the solutions to using system, they are just answering how to run the system command instead and it obviously never works with system. It is not a good canonical question for how to cd in mysql. There will never be a good answer.

Answer

BuvinJ picture BuvinJ · Sep 19, 2017

If you start the MySQL command line tool from the terminal, your working directory will be wherever you executed the binary from. For example in Windows, start the command prompt and then run the following:

cd "C:\my scripts path\"    
"C:\Program Files (x86)\MySQL\MySQL Server 5.7\bin\mysql" -u root -p rootpassword

Now, you can execute source commands and just pass the script name rather than the entire absolute path. This is super useful for nesting scripts while using relative paths.

(I'm pretty certain this works in Linux too, but I only tested in Windows.)