In oracle database, command "host" can be used to run bash command from database command window. Is there a equivalent command as "host" in mySql?
5 Answers
You can use the system
command.
system command, \! command
Executes the given command using your default command interpreter.
The system command works only in Unix.
Example:
system ls -l

- 132,184
- 23
- 144
- 116
-
3Of course this only works in the MySQL command-line client. Not from the database server itself. – Peter Stuifzand May 30 '09 at 11:06
-
This works from CLI client and also it runs on client instance and not on the MySQL server. Running shell script on MySQL server from withing MySQL is impossible, and for good reasons. – Rakmo Oct 05 '20 at 13:28
As an additional tweak, on most "'nix" systems you can actually do something like this:
mysql> system bash
And have the entire terminal at your disposal. This is particularly effective if you're doing code work in the terminal that interfaces with MySQL; from the shell, typing "exit" will take you back into the MySQL monitor/client, so you can go back and forth quite easily.
Obviously, other shells ("mysql> system tcsh") would work as well.

- 2,285
- 1
- 22
- 23
I'm running version 5.0.95-log on Linux. Prefacing the command either by "system" or by "!" works for the "pwd" and the "ls -l" commands. If I try to change directory using, e.g.
mysql> system cd /home/mydir
the command seems to be accepted. But this apparently does nothing, as following "pwd" and "ls -l" commands indicate that I am still in the same directory. So it appears that there is a stub of limited functionality built in for this, but that we do not actually have full access to the system shell.

- 21
- 1
-
1I think this isn't necessarily a stub, but there's a separate shell on each instance of `system` command. so if you could chain them somehow ... – Ciprian Tomoiagă Apr 25 '17 at 20:49
It's actually possible to execute shell commands on the server that mysqld is running though a client connection (rather than executing commands on the client machine locally) by using MySQL Proxy (scroll down to the "Shell commands from MySQL client" section ).

- 47,148
- 35
- 106
- 149
In a linux machine you should be able to use the following example
- ! clear - to clear the screen
- ! ls - to list files in the current working directory
- Basically you should be able to run any command or script using that syntax
NB: Add a back slash before !

- 323
- 5
- 16
-
1This doesn't answer the question, which is about how to invoke an SQL script from MySQL prompt, not Linux command line. – codeforester Jan 31 '18 at 19:12
-
1@codeforester Please read the question again, and while you are at it, check the "accepted" answer as well :) – Faisal Aug 03 '20 at 02:53