Bash 脚本 Mysql 警告:在命令行界面上使用密码可能不安全

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

Bash Script Mysql Warning: Using a password on the command line interface can be insecure

mysqllinuxbashpasswords

提问by andresg3

Hi I have a script to partition some mysql databases. We are upgrading from 5.5 to 5.6. While testing the scripts i noticed that with the new 5.6 version mysql returns Warning: Using a password on the command line interface can be insecure.what is the best way to fix this? I read a workaround would be 2>/dev/nullbut I wont be able to get the exit code or any errors if they happen. Is there any other way to do this. Here is the problematic line of code:

嗨,我有一个脚本来分区一些 mysql 数据库。我们正在从 5.5 升级到 5.6。在测试脚本时,我注意到使用新的 5.6 版本 mysql 返回Warning: Using a password on the command line interface can be insecure.解决此问题的最佳方法是什么?我读了一个解决方法,2>/dev/null但如果发生,我将无法获得退出代码或任何错误。有没有其他方法可以做到这一点。这是有问题的代码行:

MYSQL_RESULT=`echo "SET sql_log_bin=0;SET @pdb='$DB',@ptable='$table';CALL maintenance(@pdb,@ptable);SET sql_log_bin=1;"|mysql -uUSER -pPASSWORD database`

回答by pgl

One way to get around this is to set the appropriate variables in your ~/.my.cnffile. Something similar to this should help:

解决此问题的一种方法是在~/.my.cnf文件中设置适当的变量。类似的东西应该会有所帮助:

[mysql]                                                                                                                                                   
user=my_username                                                                                                                                          
password=my_password

This should live in the home directory of the user executing the command. And don't forget to set the right permissions on the file to avoid it being readable by other users: chmod 600 ~/.my.cnf.

这应该位于执行命令的用户的主目录中。并且不要忘记为文件设置正确的权限以避免其他用户读取它:chmod 600 ~/.my.cnf.

回答by álvaro González

If you are using MySQL/5.6.6 or greater you can use a bundled tool called mysql_config_editor:

如果您使用 MySQL/5.6.6 或更高版本,您可以使用名为mysql_config_editor的捆绑工具:

The mysql_config_editorutility [...] enables you to store authentication credentials in an encrypted login path file named .mylogin.cnf. The file location is the %APPDATA%\MySQLdirectory on Windows and the current user's home directory on non-Windows systems. The file can be read later by MySQL client programs to obtain authentication credentials for connecting to MySQL Server.

mysql_config_editor实用程序 [...] 使您能够将身份验证凭据存储在名为 .mylogin.cnf. 文件位置是%APPDATA%\MySQLWindows 上的目录和非 Windows 系统上当前用户的主目录。MySQL 客户端程序稍后可以读取该文件以获取用于连接到 MySQL 服务器的身份验证凭据。

With such tool, you can assign a number of named credentials ("login paths"):

使用此类工具,您可以分配多个命名凭据(“登录路径”):

$ mysql_config_editor set --login-path=backup-user --host=localhost --user=backup --password
Enter password:
$ mysql_config_editor print --all
[backup-user]
user = backup
password = *****
host = localhost

... which are can be used later by clients that support the feature (such as the official command-line client or mysqldump):

...稍后可以由支持该功能的客户端使用(例如官方命令行客户端或 mysqldump):

$ mysql --login-path=backup-user
Welcome to the MySQL monitor.  Commands end with ; or \g.

Of course, having 5.6.6+ is the main reason of getting«Warning: Using a password on the command line interface can be insecure» in the first place;-)

当然,有5.6.6+是获得的主要原因«警告:在命令行界面上使用密码可以是不安全»放在首位;-)