How to change directory in mysql command line tool?

16,266

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.)

Share:
16,266
Tomas
Author by

Tomas

Updated on July 20, 2022

Comments

  • Tomas
    Tomas almost 2 years

    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.

  • Tomas
    Tomas almost 8 years
    yes, and I want to change "where I am standing" from within the mysql command client (without restarting it).
  • Faizan Younus
    Faizan Younus almost 8 years
    You don need to change you standing directory here. you just need to use absolute path to import file/dump. system cd /home/mfyounus
  • tadman
    tadman almost 8 years
    @TMS I don't think the mysql tool allows you to do this. It expects you to have it right before you launch it, or as Faizan says, to specify the full path if that's matter. The full path is the best way to get it right for sure.
  • Tomas
    Tomas almost 8 years
    in other words, are you both trying to explain me why I shouldn't want what I asked for? This always bothers me. This is not an answer then. The only useful answer is the first sentence then.
  • Christopher Schultz
    Christopher Schultz about 5 years
    system cd [path] accomplishes exactly nothing. It starts a sub-process, changes its working directory, and then terminates.
  • Tomas
    Tomas over 4 years
    well, that's kind of obvious. That's not changing the directory. That's starting in a different directory.
  • BuvinJ
    BuvinJ almost 4 years
    Technically, you're correct. But it's not entirely "obvious" when you don't know the black box inner workings of how a given program handles a working directory / path resolution exactly. More to the point, this is an easy means to solve the problem for many (most?) use cases.