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


当前回答

如果你想在命令行中使用密码,我发现这可以过滤掉特定的错误消息:

mysqlcommand 2>&1 | grep -v "Warning: Using a password"

它基本上是将标准错误重定向到标准输出——并使用grep删除与“Warning: using a password”匹配的所有行。

通过这种方式,您可以看到任何其他输出,包括错误。我将此用于各种shell脚本等。

其他回答

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

所以只要这样做:

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

下面是Docker在/bin/sh脚本中的解决方案:

docker exec [MYSQL_CONTAINER_NAME] sh -c 'exec echo "[client]" > /root/mysql-credentials.cnf' docker exec [MYSQL_CONTAINER_NAME] sh -c 'exec echo "user=root" >> /root/mysql-credentials.cnf' docker exec [MYSQL_CONTAINER_NAME] sh -c 'exec echo "password=$MYSQL_ROOT_PASSWORD" >> /root/mysql-credentials.cnf' docker exec [MYSQL_CONTAINER_NAME] sh -c 'exec mysqldump——defaults-extra-file=/root/mysql-credentials.cnf——all-databases'

替换[MYSQL_CONTAINER_NAME],并确保在容器中设置了环境变量MYSQL_ROOT_PASSWORD。

希望它能像帮助我一样帮助你!

定义helper:

remove-warning () {
    grep -v 'mysql: [Warning] Using a password on the command line interface can be insecure.'
}

使用它:

mysql -u $user -p$password -e "statement" 2>&1 | remove-warning

Tachaan !您的代码很干净,易于阅读

(用bash测试)

shell> mysql_config_editor set --login-path=local
     --host=localhost --user=localuser --password
Enter password: enter password "localpass" here
shell> mysql_config_editor set --login-path=remote
     --host=remote.example.com --user=remoteuser --password
Enter password: enter password "remotepass" here

要查看mysql_config_editor对.mylogin.cnf文件写了什么,使用print命令:

shell> mysql_config_editor print --all
[local]
user = localuser
password = *****
host = localhost
[remote]
user = remoteuser
password = *****
host = remote.example.com

print命令将每个登录路径显示为一组行,以方括号开头的组标头表示登录路径名称,然后是登录路径的选项值。密码值被屏蔽,不会以明文形式显示。

如前面的示例所示,.mylogin.cnf文件可以包含多个登录路径。通过这种方式,mysql_config_editor可以很容易地为连接到不同的MySQL服务器设置多个“个性”。稍后在调用客户端程序时,可以使用——login-path选项按名称选择其中任何一个。例如,要连接到本地服务器,使用以下命令:

shell> mysql --login-path=local

要连接到远程服务器,使用这个命令:

shell> mysql --login-path=remote

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

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.