在shell腳本中,通常要配置數據庫的明文密碼,這是不安全的。在MySQL 5.6版本以后,可以配置安全的加密密碼文件,之后可以使用加密后的密碼文件連接數據庫,而不需要輸入明文密碼。
為本地root用戶配置安全加密密碼
[root@localhost ~]# mysql_config_editor set --user=root --password Enter password:
嘗試連接數據庫 [root@localhost ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 179 Server version: 5.6.31-77.0-log Percona Server (GPL), Release 77.0, Revision 5c1061c
Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, 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> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)
嘗試連接數據庫 [root@localhost ~]# mysql --login-path=remote Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 181 Server version: 5.6.31-77.0-log Percona Server (GPL), Release 77.0, Revision 5c1061c
Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, 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> select user(); +--------------------+ | user() | +--------------------+ | ss7@192.168.56.101 | +--------------------+ 1 row in set (0.00 sec)
mysql> exit Bye
通過--login-path選項連接root本地用戶 [root@localhost ~]# mysql --login-path=client Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 182 Server version: 5.6.31-77.0-log Percona Server (GPL), Release 77.0, Revision 5c1061c
Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, 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> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)