简体   繁体   English

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

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

Hi I have a script to partition some mysql databases.嗨,我有一个脚本来分区一些 mysql 数据库。 We are upgrading from 5.5 to 5.6.我们正在从 5.5 升级到 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.在测试脚本时,我注意到使用新的 5.6 版本 mysql 返回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/null but I wont be able to get the exit code or any errors if they happen.我读到一个解决方法是2>/dev/null但如果它们发生,我将无法获得退出代码或任何错误。 Is there any other way to do this.有没有其他方法可以做到这一点。 Here is the problematic line of code:这是有问题的代码行:

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`

One way to get around this is to set the appropriate variables in your ~/.my.cnf file.解决此问题的一种方法是在~/.my.cnf文件中设置适当的变量。 Something similar to this should help:类似的东西应该会有所帮助:

[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

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_editor utility [...] enables you to store authentication credentials in an encrypted login path file named .mylogin.cnf . mysql_config_editor实用程序 [...] 使您能够将身份验证凭据存储在名为.mylogin.cnf的加密登录路径文件中。 The file location is the %APPDATA%\\MySQL directory on Windows and the current user's home directory on non-Windows systems.文件位置在 Windows 上为%APPDATA%\\MySQL目录,在非 Windows 系统上为当前用户的主目录。 The file can be read later by MySQL client programs to obtain authentication credentials for connecting to MySQL Server. 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.

Please note that this doesn't really encrypt passwords (credentials at .mylogin.cnf are obfuscated only), it just moves them away from your scripts.请注意,这并没有真正加密密码( .mylogin.cnf中的凭据进行了混淆处理),它只是将它们从您的脚本中移开。

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+是获得的主要原因«警告:在命令行界面上使用密码可以是不安全»放在首位;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM