MySQL--避开登录时的[Warning]

本文主要介绍如何避开MySQL数据库登录时,在终端中输入密码提示的warning。

在MySQL中, 如果显示的输入密码去登录的话, 就会有一个Warning显示出来, 提醒这种使用方式会不安全。在写脚本时,通过mysql命令行去执行SQL语句,获取执行的结果,这个提示会带来灾难。

1
mysql: [Warning] Using a password on the command line interface can be insecure.

解决办法如下:

  1. 修改数据库配置文件

在my.cnf配置文件中,加入以下几行:

1
2
3
4
5
6
7
[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8mb4
host=localhost
user=数据库用户
password='数据库密码'
  1. 利用mysql_config_editor安全登录工具

生成加密文件:

1
2
3
4
5
6
7
# mysql_config_editor set --login-path=local --host=192.168.1.190 --user=root --password
参数说明:
--login-path 标识
--host 登录数据库的主机ip
--user 登录数据库的用户名
--password 要设置的密码
# ll ~/.mylogin.cnf

使用加密文件登录:

1
# mysql --login-path=local

查看当前主机上的加密文件:

1
# mysql_config_editor print --all

删除某个加密登陆:

1
2
# mysql_config_editor remove --login-path=remote
# mysql_config_editor print --all

重置所有:

1
# mysql_config_editor reset
  1. 利用环境变量MYSQL_PWD
1
2
# export MYSQL_PWD=666666					    				 密码
# mysql -uroot -e 'select count(*) from mysql.user;' 不需要-p参数
  1. 将警告输出到null
1
# mysql -uroot -p123456 -e 'show grants;' 2>/dev/nul			   2是标准出错
---------------- The End ----------------