Execute shell command from within a MySQL client?

2019-04-23 17:15发布

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?

标签: mysql shell
5条回答
聊天终结者
2楼-- · 2019-04-23 17:47

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.

查看更多
萌系小妹纸
3楼-- · 2019-04-23 17:55

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
查看更多
虎瘦雄心在
4楼-- · 2019-04-23 17:56

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.

查看更多
疯言疯语
5楼-- · 2019-04-23 18:02

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

查看更多
手持菜刀,她持情操
6楼-- · 2019-04-23 18:04

In a linux machine you should be able to use the following example

  1. ! clear - to clear the screen
  2. ! ls - to list files in the current working directory
  3. Basically you should be able to run any command or script using that syntax

NB: Add a back slash before !

查看更多
登录 后发表回答