简体   繁体   English

来自bash脚本的SSH中MYSQL中的嵌套SQL语句

[英]Nested SQL Statement within MYSQL within SSH from bash script

I have the unfortunate situation, that I don't have the permission to access the MYSQL database from outside the server.我有一个不幸的情况,我没有从服务器外部访问 MYSQL 数据库的权限。 But SSH is possible.但是 SSH 是可能的。 Therefore I try to run a simple SQL statement from a bash file, that creates a SSH connection, connects to the MYSQL DB and run the SQL statement.因此,我尝试从 bash 文件运行一个简单的 SQL 语句,该语句创建一个 SSH 连接,连接到 MYSQL DB 并运行该 SQL 语句。

The syntax is pretty straight forward but I'm not able to use them combined in one bash file, but on the command line each individual is working语法非常简单,但我无法将它们组合在一个 bash 文件中使用,但在命令行上每个人都在工作

that the snippets I'm using:我正在使用的片段:

1) establish the SSH connection: 1)建立SSH连接:

$:sshpass -p my_password ssh my_user@my_server

2) connect to the MYSQL DB: 2)连接到MYSQL数据库:

my_server>mysql -h rdbms -u db_user -D db_name -p db_password

3) run the SQL statement 3)运行SQL语句

mysql>SELECT * FROM table

... as said. ……正如所说。 all good when running on command line.在命令行上运行时一切都很好。

But when I combine them into a bash file:但是当我将它们组合成一个 bash 文件时:

#!/usr/bin/
sshpass -p my_password ssh my_user@my_server
mysql -h rdbms -u db_user -D db_name -p db_password
SELECT * FROM table

It stops right after the first line (establishing the SSH connection).它在第一行(建立 SSH 连接)之后立即停止。 Any ideas how I can combine these?我有什么想法可以结合这些吗?

To run a command on a remote server via ssh, you need to list the command as arguments on the same command-line.要通过 ssh 在远程服务器上运行命令,您需要在同一命令行上将该命令作为参数列出。

For example:例如:

sshpass -p my_password ssh my_user@my_server date

That will run date on the remote server, and return the output of that command.这将在远程服务器上运行date ,并返回该命令的输出。

You can run the mysql client this way too.您也可以通过这种方式运行 mysql 客户端。 Just put the mysql command on the same command-line, as arguments to your ssh.只需将 mysql 命令放在同一命令行上,作为 ssh 的参数。

sshpass -p my_password ssh my_user@my_server mysql ...arguments...

You can use \\ at the end of a line to continue a long command on the following line.您可以在行尾使用\\以在下一行继续执行长命令。 It works as if you had written the full command on one very long line, but it's easier to post in Stack Overflow so readers don't have to scroll horizontally to read it.它的工作原理就像你在很长的一行上写了完整的命令一样,但在 Stack Overflow 中发布更容易,这样读者就不必水平滚动来阅读它。 :-) :-)

Also note that the remote command must be in quotes so it appears like a single argument to ssh.另请注意,远程命令必须用引号引起来,因此它看起来像 ssh 的单个参数。 When it runs on the remote server, it will be expanded to multiple arguments.当它在远程服务器上运行时,它会被扩展为多个参数。

sshpass -p my_password ssh my_user@my_server \
 "mysql ...arguments..."

The mysql client has an option -e that you can use to execute an SQL statement. mysql 客户端有一个选项-e ,您可以使用它来执行 SQL 语句。

sshpass -p my_password ssh my_user@my_server \
 "mysql -h rdbms -u db_user -D db_name -pdb_password -e 'SELECT * FROM table'"

A couple of tips about the password:关于密码的一些提示:

  1. There must be no space between -p and the password. -p和密码之间不能有空格。 If you use -p with a space after it, you will be prompted for the password interactively.如果您使用-p并在其后加一个空格,系统会以交互方式提示您输入密码。 The word following the space is not taken as the password unless you stick it against the -p .空格后面的单词不会被当作密码,除非你把它贴在-p

  2. I don't like to put passwords in plaintext on the command-line.我不喜欢在命令行上以明文形式输入密码。 It's not safe to do that, because anyone who can access your shell history can view the password.这样做是不安全的,因为任何可以访问您的 shell 历史记录的人都可以查看密码。 It's better to use an option file or a login file .最好使用选项文件登录文件 But you'll have to put these on the remote server where the mysql client runs.但是您必须将它们放在运行 mysql 客户端的远程服务器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM