本文主要介绍如何避开MySQL数据库登录时,在终端中输入密码提示的warning。
在MySQL中, 如果显示的输入密码去登录的话, 就会有一个Warning显示出来, 提醒这种使用方式会不安全。在写脚本时,通过mysql命令行去执行SQL语句,获取执行的结果,这个提示会带来灾难。
1 | mysql: [Warning] Using a password on the command line interface can be insecure. |
解决办法如下:
- 修改数据库配置文件
在my.cnf配置文件中,加入以下几行:
1 | [client] |
- 利用mysql_config_editor安全登录工具
生成加密文件:
1 | # mysql_config_editor set --login-path=local --host=192.168.1.190 --user=root --password |
使用加密文件登录:
1 | # mysql --login-path=local |
查看当前主机上的加密文件:
1 | # mysql_config_editor print --all |
删除某个加密登陆:
1 | # mysql_config_editor remove --login-path=remote |
重置所有:
1 | # mysql_config_editor reset |
- 利用环境变量MYSQL_PWD
1 | # export MYSQL_PWD=666666 密码 |
- 将警告输出到null
1 | # mysql -uroot -p123456 -e 'show grants;' 2>/dev/nul 2是标准出错 |