MySQL Shell - 一行查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9462416/
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 12:16:50  来源:igfitidea点击:

Shell - one line query

mysqlshellcommand-linefreebsd

提问by Cyclone

I need to execute a mysql query in one line using bash.

我需要使用 bash 在一行中执行一个 mysql 查询。

It should be something like this:

它应该是这样的:

mysql database --user='root' --password='my-password' < query.file

But instead of the < query.fileit would like to use a raw query like this:

但不是< query.file它想使用这样的原始查询:

mysql database --user='root' --password='my-password' < UPDATE `database` SET `field1` = '1' WHERE `id` = 1111;

Is that possible?

那可能吗?

回答by Nishant

Did you try

你试过了吗

 mysql -u root -pmy_password -D DATABASENAME -e "UPDATE `database` SET `field1` = '1' WHERE `id` = 1111;" > output.txt 

(the > output.txtpart can be ignored but, it will be useful to see what was returned by the statement executed by looking at the file.)

(该> output.txt部分可以忽略,但是通过查看文件来查看执行的语句返回的内容会很有用。)

回答by Cyclone

Use the -eoption:

使用-e选项:

$ mysql -e "UPDATE ..."

回答by Some programmer dude

Use echoand a pipe:

使用echo和管道:

echo "UPDATE `database` SET `field1` = '1' WHERE `id` = 1111;" | mysql database --user='root' --password='my-password'

回答by Ian Mackinnon

Writing your password in a command is generally a bad idea (people can read it over your shoulder, it probably gets stored in your shell history, etc.), but you can put your credentials in a file. Giving the file a name starting with .makes it hidden, which is also more secure.

在命令中写入密码通常是一个坏主意(人们可以偷偷阅读它,它可能会存储在您的 shell 历史记录中,等等),但您可以将您的凭据放在一个文件中。给文件起一个以 开头的名字.可以隐藏它,这也更安全。

# .db.conf
[client]
database=myDatabase
user=myUserName
password=myPassWord

Make sure only you can read the file:

确保只有您可以读取该文件:

chmod 600 .db.conf

Then call MySQL like so:

然后像这样调用 MySQL:

mysql --defaults-extra-file=.db.conf -e "UPDATE database SET field1 = '1' WHERE id = 1111;"

or:

或者:

echo "UPDATE database SET field1 = '1' WHERE id = 1111;" | mysql --defaults-extra-file=.db.conf

Note that --defaults-extra-fileneeds to be the first option supplied to mysqlotherwise it freaks out.

请注意,--defaults-extra-file需要是提供给的第一个选项,mysql否则它会吓坏了。

回答by zainengineer

I normally prefer Triple less then as its syntax and approach is similar to file redirect. Easy to go back in history and modify query

我通常更喜欢 Triple,因为它的语法和方法类似于文件重定向。轻松返回历史和修改查询

mysql database --user='root' --password='my-password' <<< "UPDATE `database` SET `field1` = '1' WHERE `id` = 1111"

It is called Here Strings in bash. You can find more about them here http://linux.die.net/abs-guide/x15683.html

它在 bash 中称为 Here Strings。你可以在这里找到更多关于它们的信息http://linux.die.net/abs-guide/x15683.html

It is useful when you want to pipe string to commands.

当您想通过管道将字符串传递给命令时,它很有用。

回答by HappyCoder

If you are running this on a production environment, it would be better to login to the mysql shell first so as not to expose your db details.

如果您在生产环境中运行它,最好先登录到 mysql shell,以免暴露您的数据库详细信息。

Once logged into shell, why not just use a prepared.sql file?

一旦登录到 shell,为什么不直接使用一个 Prepared.sql 文件?

mysql -u user -p

mysql -u 用户 -p

Next, enter your user password

接下来,输入您的用户密码

Now you are logged into shell and you can run commands securely from here:

现在您已登录到 shell,您可以从这里安全地运行命令:

mysql> use DBNAME mysql> SOURCE file.sql

mysql> 使用 DBNAME mysql> SOURCE file.sql

This is how I operate on the command line with my databases in order for my passwords not to appear in the logs.

这就是我在命令行上使用我的数据库操作的方式,以便我的密码不会出现在日志中。