MySQL5.6之后的版本在通过客户端连接或者备份时时都会有出现如下的一个警告:
Warning: Using a password on the command line interface can be insecure.
在一些脚本操作中可能会造成一些干扰,这里介绍下两种方式来避免这个输出。
1:修改配置文件
vim /etc/mysql/my.cnf
在[client]下添加
user=***
password=***
保存退出,重启mysql进程;
此时再次连接MySQL的时候,不要在命令行出现-u -p参数,直接mysql:
root@ubuntu:/etc/mysql/mysql.conf.d# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1637
Server version: 5.7.31-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1637
Server version: 5.7.31-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
可以看到之前的警告就没有了!
注意:如果你是使用mysqldump指令去连接,就修改配置文件中[mysqldump]的部分。
2:重定向警告
root@ubuntu:/etc/mysql/mysql.conf.d# mysql -usaier -plr@2018 2>/dev/null
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1640
Server version: 5.7.31-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1640
Server version: 5.7.31-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
通过这种方式,我们把告警输出重定向到/dev/null,告警就不会输出到交互式界面了,同时脚本里也会获得一个比较纯净的返回家过变量。
这两种方式,第一种需要改配置文件,而且任何人都可以不需要密码直接进入BD,存在一定的风险性;
第二种需要重定向错误和告警输出,这样会让我们忽略掉一些异常信息,因此这两种方式需要酌情选择。
以上。