Mysql之三種免密登入方式
一、示例環境版本說明
作業系統版本centos7.6
[wuhs@test1 mysql]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
mysql資料庫版本5.7.32
[wuhs@test1 mysql]$ mysql -V
mysql Ver 14.14 Distrib 5.7.32, for el7 (x86_64) using EditLine wrapper
二、MySQL免密登入方式配置示例
1、通過設定client標籤,直接編輯/etc/my.cnf檔案
編輯/etc/my.cnf檔案,新增如下程式碼
[wuhs@test1 mysql]$ cat /etc/my.cnf
[client]
user = root
password = 123456
port = 3306
配置完成後可以使用mysql命令直接登入資料庫
[wuhs@test1 mysql]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.32-log MySQL Community Server (GPL)
<br>
Copyright © 2000, 2020, Oracle and/or its affiliates. All rights reserved.<br>
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
<br>
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
<br>
mysql>
此方式最大問題是明文儲存密碼,見配置檔案各使用者可見,非常的不安全。
2、我們通過my.cnf來配置,設定到~/.my.cnf來配置免密碼
編輯~/.my.cnf檔案,新增如下程式碼
[wuhs@test1 mysql]$ cat ~/.my.cnf
[client]
user = root
password = 123456
port = 3306
修改my.cnf屬性
#chmod 600 ~/.my.cnf
[wuhs@test1 mysql]$ ll ~/.my.cnf
-rw-------. 1 wuhs wuhs 51 Dec 29 22:56 /home/wuhs/.my.cnf
配置完成後可以使用mysql命令直接登入資料庫
[wuhs@test1 mysql]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.32-log MySQL Community Server (GPL)
<br>
Copyright © 2000, 2020, Oracle and/or its affiliates. All rights reserved.
<br>
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
<br>
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
<br>
mysql>
此種方式也是明文儲存,配置方式同第一種,檔案為隱藏檔案,設定檔案為改使用者可讀,與第一種方式相比安全性有所提高。經驗證測試,~/.my.cnf配置檔案優先於/etc/my.cnf。
3、通過mysql_config_editor命令
使用mysql_config_editor命令一個test標籤
[wuhs@test1 mysql]$ mysql_config_editor set -G test -S /tmp/mysql.sock -uroot -p
Enter password: [此處輸入root賬戶密碼]
執行如上步驟後生成了隱藏檔案.mylogin.cnf,檔案型別為data,是一個二進位制檔案
[wuhs@test1 mysql]$ file ~/.mylogin.cnf
/home/wuhs/.mylogin.cnf: data
檢視該檔案,密碼為加密儲存
[wuhs@test1 mysql]$ mysql_config_editor print --all
[test]
user = root
password = *****
socket = /tmp/mysql.sock
使用mysql --login-path="標籤"登入
[wuhs@test1 mysql]$ mysql --login-path=test
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.32-log MySQL Community Server (GPL)
<br>
Copyright © 2000, 2020, Oracle and/or its affiliates. All rights reserved.
<br>
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
<br>
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
<br>
mysql>
第三種方式登入檔案為隱藏的二進位制檔案,且密碼通過密文儲存,安全性最高。