当我尝试在终端内运行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。


当前回答

对于PowerShell (pwsh,而不是bash),这是一个相当土豪的解决方案……我的第一次尝试是在try/catch函数中包装对mysql的调用,但由于PowerShell错误处理中的一些奇怪行为,这是不可行的。

解决方案是重写$ErrorActionPreference,以足够长的时间组合和捕获STDERR和STDOUT,并解析单词ERROR并根据需要重新抛出。我们不能捕获和释放“^mysql.*警告。因为PowerShell将错误作为一个流来处理和引发,所以您必须捕获它以便过滤和重新抛出。:/

Function CallMySQL() {
    # Cache the error action preference
    $_temp = $ErrorActionPreference
    $ErrorActionPreference = "Continue"

    # Capture all output from mysql
    $output = (&mysql --user=foo --password=bar 2>&1)

    # Restore the error action preference
    $ErrorActionPreference = $_temp

    if ($output -match "ERROR") {
        throw $output
    } elseif($output) {
        "   Swallowing $output"
    } else {
        "   No output"
    }
}

注意:PowerShell可用于Unix,因此此解决方案是跨平台的。它可以通过一些小的语法修改来适应bash。

警告:有许多边缘情况下,这将不起作用,如非英语错误消息或语句在输出的任何地方返回单词error,但它足以吞下对mysql的基本调用的警告,而不破坏整个脚本。希望其他人觉得这有用。

如果mysql简单地添加一个选项来抑制这个警告就好了。

其他回答

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

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

这只返回最后一行

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

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

如果您的MySQL客户端/服务器版本是5.6。使用mysql_config_editor工具来避免警告消息:

mysql_config_editor set --login-path=local --host=localhost --user=username --password

然后你可以在你的shell脚本中使用:

mysql --login-path=local  -e "statement"

而不是:

mysql -u username -p pass -e "statement"

我使用的是:

mysql --defaults-extra-file=/path/to/config.cnf

or

mysqldump --defaults-extra-file=/path/to/config.cnf 

其中config.cnf包含:

[client]
user = "whatever"
password = "whatever"
host = "whatever"

这允许您拥有多个配置文件-针对不同的服务器/角色/数据库。使用~/.my.cnf将只允许您拥有一组配置(尽管它可能是一组有用的默认值)。

如果你是基于Debian的发行版,并且以root用户运行,你可以跳过上面的步骤,使用/etc/mysql/debian.cnf进入…:

mysql——defaults-extra-file = / etc / mysql / debian.cnf

最简单的方法是

mysql -u root -pMYPASSWORD -e "show databases" 2>/dev/null

下面是我如何让每日mysqldump数据库备份的bash脚本更安全地工作。这是克里斯蒂安·波特的伟大答案的扩展。

First use mysql_config_editor (comes with mysql 5.6+) to set up the encrypted password file. Suppose your username is "db_user". Running from the shell prompt: mysql_config_editor set --login-path=local --host=localhost --user=db_user --password It prompts for the password. Once you enter it, the user/pass are saved encrypted in your home/system_username/.mylogin.cnf Of course, change "system_username" to your username on the server. Change your bash script from this: mysqldump -u db_user -pInsecurePassword my_database | gzip > db_backup.tar.gz to this: mysqldump --login-path=local my_database | gzip > db_backup.tar.gz

不再暴露密码。