11

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.

6
  • 2
    unclear what I am asking?? Really? Commented Jul 13, 2016 at 7:34
  • Possible duplicate of mysql system command Commented Jul 13, 2016 at 7:34
  • @AniMenon that question and answers are more about the system command than changing the directory... Commented Jul 13, 2016 at 7:36
  • read that question again.. OP wanted to cd and he tried it with system. Commented Jul 13, 2016 at 7:38
  • 1
    The answer is you can't. There is no hopping around. There is pathing around. Unless you want to code a convoluted UDF. Commented Jul 14, 2016 at 2:48

4 Answers 4

6

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

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

2 Comments

well, that's kind of obvious. That's not changing the directory. That's starting in a different directory.
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.
1

Short answer: You Can't.

I tried all the solutions in those answer, and none of those solutions works, including:

  1. \! cd ...
  2. system cd ...
  3. \system cd ...

(Of course the "quit mysql and restart from another locatioin" works, but that's not what the OP is asking.)

For confirmation and explanation, see https://stackoverflow.com/a/7457085/3776039 and https://dba.stackexchange.com/questions/237254/change-current-working-directory-from-mysql-client-prompt.

Comments

0

The MySQL data directory can only be changed using the my.cnf (MySQL configuration) file. Changing that requires MySQL to be restarted.

If you want to import/load files to DB, the working directory will be the directory you were in when you started MySQL.

If you want to change directory after connecting to MySQL, use following command:

system cd /home/mfyounus

5 Comments

yes, and I want to change "where I am standing" from within the mysql command client (without restarting it).
You don need to change you standing directory here. you just need to use absolute path to import file/dump. system cd /home/mfyounus
@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.
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.
system cd [path] accomplishes exactly nothing. It starts a sub-process, changes its working directory, and then terminates.
-1

I think the solution you are looking for is to change the directory to access and run some script.

\system cd "C:\Users\Admin\Folder"

Comments

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.