当我尝试在终端内运行MySQL上的以下命令时:

mysql -u $user -p$password -e "statement"

执行按预期工作,但它总是发出一个警告:

警告:在命令行界面上使用密码可能不安全。

但是,我必须使用存储密码的环境变量($password)来执行上面的语句,因为我想从Terminal中在bash脚本中迭代地运行命令,而且我绝对不喜欢等待提示显示并强迫我在一个脚本中输入密码50或100次的想法。我的问题是:

压制警告可行吗?如我所述,该命令可以正常工作,但是当我循环并运行该命令50或100次时,窗口变得非常混乱。 我应该遵守警告信息,不写我的密码在我的脚本?如果是这样的话,那么每次提示时我都必须输入密码吗?

Running man mysql没有帮助,说的只是

——显示警告 在每条语句后显示警告(如果有的话)。此选项适用于交互和批处理模式。

也没有提到如何关闭功能,如果我没有错过什么。

我使用的是OS X 10.9.1 Mavericks,使用的是自制的MySQL 5.6。


当前回答

最简单的方法:

mysql -u root -p YOUR_DATABASE

输入这个,你需要输入你的密码。

注意:是的,没有分号。

其他回答

我遇到的问题是在bash脚本中使用条件输出。

这并不优雅,但在docker环境中,这应该真的无关紧要。 基本上,所有这些操作都忽略了不在最后一行上的输出。 你可以用awk做类似的事情,并更改为返回除第一行以外的所有内容。

这只返回最后一行

mysql -u db_user -pInsecurePassword my_database ... | sed -e '$!d'

它不会抑制错误,但它将确保您可以在bash脚本中使用查询的输出。

一种方便(但同样不安全)的方法是使用:

MYSQL_PWD=xxxxxxxx mysql -u root -e "statement"

请注意,官方文件不建议这样做。 请参见6.1.2.1密码安全终端用户指南(Mysql手册for Version 5.6):

Storing your password in the MYSQL_PWD environment variable This method of specifying your MySQL password must be considered extremely insecure and should not be used. Some versions of ps include an option to display the environment of running processes. On some systems, if you set MYSQL_PWD, your password is exposed to any other user who runs ps. Even on systems without such a version of ps, it is unwise to assume that there are no other methods by which users can examine process environments.

这对我很管用 只要在$(mysql_command)之后添加2> null,它将只抑制错误和警告消息。

还可以将标准错误STDERR输出重定向到/dev/null

所以只要这样做:

Mysql -u $user -p$password -e "statement" 2> /dev/null

最好的解决方案是使用别名:

alias [yourapp]-mysql="mysql -u root -psomepassword -P3306 -h 127.0.0.1"

例如,把这个放在你的脚本中:

alias drupal-mysql="mysql -u root -psomepassword -P3306 -h 127.0.0.1"

然后在脚本中加载数据库:

drupal-mysql database_name < database_dump.sql

运行语句:

drupal-mysql -e "EXEC SOMESTATEMENT;"