从 MySQL 客户端中执行 shell 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/929342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:22:33  来源:igfitidea点击:

Execute shell command from within a MySQL client?

mysqlshell

提问by Progress Programmer

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?

在oracle 数据库中,可以使用命令“host”从数据库命令窗口运行bash 命令。在 mySql 中是否有与“host”等效的命令?

回答by Ayman Hourieh

You can use the systemcommand.

您可以使用该system命令。

system command, \! command

Executes the given command using your default command interpreter.

The system command works only in Unix.

系统命令,\! 命令

使用您的默认命令解释器执行给定的命令。

system 命令仅适用于 Unix。

Example:

例子:

system ls -l

回答by Rory Larson

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.

我在 Linux 上运行 5.0.95-log 版本。以“系统”或“!”开头命令 适用于“pwd”和“ls -l”命令。如果我尝试使用例如更改目录

mysql> system cd /home/mydir

mysql> 系统 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.

该命令似乎已被接受。但这显然没有任何作用,因为以下“pwd”和“ls -l”命令表明我仍在同一目录中。因此,似乎为此内置了一个有限功能的存根,但我们实际上并没有完全访问系统外壳。

回答by ???u

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

通过使用MySQL 代理(向下滚动到“来自 MySQL 客户端的 Shell 命令”部分),实际上可以通过客户端连接(而不是在本地执行客户端计算机上的命令)在 mysqld 正在运行的服务器上执行 shell 命令。

回答by Kevin_Kinsey

As an additional tweak, on most "'nix" systems you can actually do something like this:

作为额外的调整,在大多数“nix”系统上,您实际上可以执行以下操作:

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.

并让整个终端供您使用。如果您在与 MySQL 接口的终端中进行代码工作,这将特别有效;在 shell 中,键入“exit”将带您返回 MySQL 监视器/客户端,因此您可以轻松地来回切换。

Obviously, other shells ("mysql> system tcsh") would work as well.

显然,其他 shell(“mysql> system tcsh”)也可以工作。

回答by chege

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

在 linux 机器中,您应该能够使用以下示例

  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
  1. !clear - 清除屏幕
  2. !ls - 列出当前工作目录中的文件
  3. 基本上你应该能够使用该语法运行任何命令或脚本

NB: Add a back slash before !

注意:在 ! 之前添加反斜杠!