mysql密码特殊符号有哪些 mysql密码命令_Warning


这篇文章主要记录如何避免一个Warning地出现,标题与其称为“如何在命令行中更安全地使用密码”,不如改为“如何避免直接明文出现密码信息地输入”,或则如何避免出现“Using a password on the command line interface can be insecure.”的警告信息提示。

现象

当我们使用mysql -u用户名 -p密码的方式以避免使用直接密码输入的方式时,MySQL一般会报出如下的警告信息

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

警告的内容就是说在命令行中使用密码是不安全的。详细示例内容如下所示:

liumiaocn:~ liumiao$ mysql --version
mysql  Ver 8.0.11 for osx10.13 on x86_64 (Homebrew)
liumiaocn:~ liumiao$ mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 58
Server version: 8.0.11 Homebrew

Copyright (c) 2000, 2018, 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>

为什么会不安全呢?因为这个密码以明文的方式出现,明显是非常不安全的,所有人都可以看到,所以提示了这个信息。即使你的屏幕上已经没有显示了,如果在控制台仍然没有退出的话,可以通过ps命令确认,已经退出的话还可以通过history命令确认到明文的密码。
在macOS可以看到,很贴心地给改了一下

liumiaocn:~ liumiao$ ps -ef |grep mysql |grep -v grep |grep root
  501  2510  2142   0 10:10PM ttys002    0:00.01 mysql -uroot -px
liumiaocn:~ liumiao$

但是希望看到内容的话,有很多种方式,比如history里面还是可以看到明文的密码,方便但不安全,就是这种使用方式的最大缺点。

liumiaocn:~ liumiao$ history |grep mysql |grep -v grep |tail -n1
  505  mysql -uroot -proot
liumiaocn:~ liumiao$

对应方法

方法1: 交互式方式输入密码

通过用户的输入方式输入密码,这样密码避免以明文的方式使得所有人都可以看见,就不会出现这个警告信息了。比如

liumiaocn:~ liumiao$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 8.0.11 Homebrew

Copyright (c) 2000, 2018, 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>

在Enter password:的提示之后直接输入密码并回车,便不会出现类似的警告信息。

方式2: 使用配置文件存储密码

虽然有五十笑百步之嫌,将明文的密码保存到当前用户的.my.cnf文件中也是MySQL官方建议的方式之一。这种情况之下就可以实现不必将明文的密码在命令行中显示了。将.my.cnf中[client]段内容中添加password设定即可

liumiaocn:~ liumiao$ tail -n2 ~/.my.cnf
[client]
password=root
liumiaocn:~ liumiao$

这种情况之下,则可不必指定-p并传入密码,MySQL进行客户端连接时会从上述文件中确认密码内容

liumiaocn:~ liumiao$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.11 Homebrew

Copyright (c) 2000, 2018, 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>

如果不指定-u用户名,会缺省使用当前系统用户名称,如果不是MySQL中匹配的用户名则会报错

liumiaocn:~ liumiao$ mysql 
ERROR 1045 (28000): Access denied for user 'liumiao'@'localhost' (using password: YES)
liumiaocn:~ liumiao$

在设定文件中加上user信息,则可不必指定用户名称

liumiaocn:~ liumiao$ tail -n3 ~/.my.cnf
[client]
user=root
password=root
liumiaocn:~ liumiao$ 
liumiaocn:~ liumiao$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.11 Homebrew

Copyright (c) 2000, 2018, 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>

这样密码没有直接以明文方式出现,至少避免了通过确认运行台状态或者操作系统对于命令执行的记录等方式获取多处可确认的明文密码,避免了一部分问题。可以对于统一存放密码的.my.cnf文件进行重点保护,比如设定权限为400或者600,至少保证了一般用户权限无法确认此文件内容,可以保证一定程度的安全性。

liumiaocn:~ liumiao$ ls -l ~/.my.cnf
-rw-r--r--  1 liumiao  staff  107 Nov  6 7:26 /Users/liumiao/.my.cnf
liumiaocn:~ liumiao$ 
liumiaocn:~ liumiao$ 
liumiaocn:~ liumiao$ chmod 400 ~/.my.cnf
liumiaocn:~ liumiao$ ls -l ~/.my.cnf
-r--------  1 liumiao  staff  107 Nov  6 7:26 /Users/liumiao/.my.cnf
liumiaocn:~ liumiao$

其他方式

除此之外,还有其他方式,比如将输出重定向到/dev/null这种掩耳盗铃的方式,或者使用mysql_config_editor与方式2类似存储到文件之中的方式,相差无几,不再赘述。

参考内容

https://stackoverflow.com/questions/20751352/suppress-warning-messages-using-mysql-from-within-terminal-but-password-written https://dev.mysql.com/doc/refman/5.5/en/password-security-user.html