问题描述
当我尝试从终端内在 MySQL 上运行以下命令时:
When I tried running the following command on MySQL from within Terminal:
mysql -u $user -p$password -e "statement"
执行按预期工作,但总是发出警告:
The execution works as expected, but it always issues a warning:
警告:在命令行界面上使用密码可能不安全.
但是,我必须使用存储密码的环境变量 ($password
) 执行上述语句,因为我想在终端内的 bash 脚本中迭代运行该命令,而且我绝对不喜欢等待提示出现并强迫我在单个脚本中输入密码 50 或 100 次的想法.所以这是我的问题:
However, I have to conduct the statement above using an environment variable ($password
) that stores my password, because I want to run the command iteratively in bash script from within Terminal, and I definitely don't like the idea of waiting a prompt showing up and forcing me to input my password 50 or 100 times in a single script. So here's my question:
是否可以抑制警告?该命令按照我的说明正常工作,但是当我循环并运行该命令 50 或 100 次时,窗口变得非常混乱.
Is it feasible to suppress the warning? The command works properly as I stated, but the window becomes pretty messy when I loop over and run the command 50 or 100 times.
我是否应该遵守警告信息并且不要在脚本中写入我的密码?如果是这种情况,那么每次提示强迫我输入密码时我都必须输入密码吗?
Should I obey the warning message and do NOT write my password in my script? If that's the case, then do I have to type in my password every time the prompt forces me to do so?
运行 man mysql
没有帮助,只说
Running man mysql
doesn't help, saying only
--show-warnings
导致在每个语句之后显示警告(如果有的话).此选项适用于交互和批处理模式.
--show-warnings
Cause warnings to be shown after each statement if there are any. This option applies to interactive and batch mode.
如果我没有遗漏任何东西,并没有提到如何关闭该功能.
and mentions nothing about how to turn off the functionality, if I'm not missing something.
我使用的是 OS X 10.9.1 Mavericks 并使用自制软件的 MySQL 5.6.
I'm on OS X 10.9.1 Mavericks and use MySQL 5.6 from homebrew.
推荐答案
如果你的 MySQL 客户端/服务器版本是 5.6.xa 避免出现警告消息的方式正在使用 mysql_config_editor 工具:
If your MySQL client/server version is a 5.6.x a way to avoid the WARNING message are using the mysql_config_editor tools:
mysql_config_editor set --login-path=local --host=localhost --user=username --password
然后你可以在你的shell脚本中使用:
Then you can use in your shell script:
mysql --login-path=local -e "statement"
代替:
mysql -u username -p pass -e "statement"
这篇关于在终端内使用 mysql 禁止警告消息,但密码是用 bash 脚本编写的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!