mysql: [Warning] Using a password on the command line interface can be insecure 解决方法

 

1.最近把MySQL从5.5升到5.6之后,mysqldump竟然很差用了,提示:html

 代码以下mysql

复制代码sql

[root@qttc ~]# /usr/local/mysql/bin/mysqldump  -uroot -proot db > bak.sql
Warning: Using a password on the command line interface can be insecure.vim

翻译过来是:在命令行界面上使用密码能够是不安全的;安全

这让人有点郁闷,5.5用的1直都很爽,到5.6竟然说命令行方式写密码不安全?那密码写哪呢?ide

在官网文档找到了原因,你们能够点击这里看看:ui

1.官方网址:http://dev.mysql.com/doc/refman/5.1/en/password-security-user.html命令行

MySQL users shoulduse the following guidelines to keep passwords secure.翻译

   When you run a client program to connect to the MySQL server, it is inadvisableto specify your password in a way that exposes it to discovery by other users.The methods you can use to specify your password when you run client programsare listed here, along with an assessment of the risks of each method. Inshort, the safest methods are to have the client program prompt for thepassword or to specify the password in a properly protected option file.server

翻译过来大意是在命令行下若是要使用密码能够在执行命令后的提示输入里输入密码,或者在指定的安全文件内指定密码;那安全文件时哪一个呢?文档对此给出了答案:

Store your passwordin an option file. For example, on Unix, you can list your password in the[client] section of the .my.cnf file in your home directory:

能够在my.cnf内指定,因而打开个人my.cnf,在[mysqldump]下增长:

 代码以下

复制代码

user=root
password=root

文中说的在[client]下面加也能够,但那样就全部块的操做都能共享了,因此生产环境上为了安全仍是尽可能分开;保存退出再dump就ok了;

 代码以下

复制代码

[root@qttc ~]# /usr/local/mysql/bin/mysqldump db > bak.sql
[root@qttc ~]#

MySQL 5.6 警告信息 command line interface can be insecure 修复

在命令行输入密码,就会提示这些安全警告信息;

Warning: Using apassword on the command line interface can be insecure.

注:mysql -u root -pPASSWORD 或 mysqldump -u root -pPASSWORD 都会输出这样的警告信息.

1.针对mysql:

mysql -u root -pPASSWORD改为mysql -u root -p 在输入密码便可.

2.mysqldump就比较麻烦了,一般都写在scripts脚本中;

解决方法:

对于mysqldump 要如何避免出现(Warning:Using a password on the command line interface can be insecure.) 警告信息呢?

vim /etc/mysql/my.cnf

[mysqldump]

user=your_backup_user_name

password=your_backup_password

修改完配置文件后, 只须要执行mysqldump 脚本就能够了;备份脚本中不须要涉及用户名密码相关信息;
 

 

问题:

echo "show global status where Variable_name='Questions';" |   mysql -uroot -proot -N

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

第一种方式: MYSQL常量;

这种方式其实就是利用常量来代替了明文输入密码的这一步骤, 
使用方法也很简单, 在脚本中加入以下命令便可
---------------------------------------------
export MYSQL_PWD=<password>
---------------------------------------------
以后在脚本中就不用带上 -p参数了, 直接在命令行试试看~

echo "show global status where Variable_name='Questions';" | MYSQL_PWD=root  mysql -uroot -N

 

第二种方式:在配置文件中填写用户名密码方式

经过HOME常量指定配置文件路径,在此路径下建立.my.cnf配置文件并在其中写入帐号密码

.my.cnf文件内容
[client]
user=zabbix
host=127.0.0.1
password=123456

 

echo "show global status where Variable_name='Questions';" | HOME=/usr/local/zabbix/etc mysql -N | awk '{print $2}'

mysql 会使用此配置文件中的帐号密码登陆,也不试试安全警告