How to change directory in mysql command line tool?
Asked Answered
F

4

10

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.

Funeral answered 13/7, 2016 at 7:30 Comment(6)
unclear what I am asking?? Really?Funeral
Possible duplicate of mysql system commandNumbat
@AniMenon that question and answers are more about the system command than changing the directory...Funeral
read that question again.. OP wanted to cd and he tried it with system.Numbat
@AniMenon look at the question title and the answers... because he limited the solutions to using system, they are just answering how to run the system command instead and it obviously never works with system. This is not good canonical question for how to cd in mysql. There will never be good answer.Funeral
The answer is you can't. There is no hopping around. There is pathing around. Unless you want to code a convoluted UDF.Radiate
S
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.)

Section answered 18/9, 2017 at 22:7 Comment(2)
well, that's kind of obvious. That's not changing the directory. That's starting in a different directory.Funeral
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.Section
G
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
Gelsemium answered 13/7, 2016 at 7:49 Comment(5)
yes, and I want to change "where I am standing" from within the mysql command client (without restarting it).Funeral
You don need to change you standing directory here. you just need to use absolute path to import file/dump. system cd /home/mfyounusGelsemium
@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.Baseboard
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.Funeral
system cd [path] accomplishes exactly nothing. It starts a sub-process, changes its working directory, and then terminates.Malignant
E
0

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://mcmap.net/q/1167842/-mysql-system-command and https://dba.stackexchange.com/questions/237254/change-current-working-directory-from-mysql-client-prompt.

Eddra answered 8/3, 2023 at 15:14 Comment(0)
W
-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"
Williamswilliamsburg answered 3/3, 2020 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.