mysql免密登錄的幾種方式


 

 

1、配置文件直接寫出密碼

 1  1 [root@localhost bin]# cat /etc/my.cnf
 2  2 [client]
 3  3 user=root
 4  4 password=123456
 5  5 
 6  6 [mysqld]
 7  7 ########basic settings########
 8  8 server-id = 11 
 9  9 port = 3306
10 10 user = mysql
11 11 socket=/var/lib/mysql/mysql.sock
12 12 #bind_address = 10.166.224.32
13 13 #autocommit = 0
14 14 character_set_server=utf8
15 15 skip_name_resolve = 1
16 16 max_connections = 800
17 17 max_connect_errors = 1000
18 18 #datadir = /data/mysql_data
19 19 datadir = /usr/local/mysql/data
20 20 basedir = /usr/local/mysql
21 21 transaction_isolation = READ-COMMITTED
22 22 explicit_defaults_for_timestamp = 1
23 23 join_buffer_size = 134217728
24 24 tmp_table_size = 67108864
25 25 tmpdir = /tmp
26 26 max_allowed_packet = 16777216
27 27 #sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
28 28 interactive_timeout = 1800
29 29 .......
 1 [root@localhost bin]# mysql
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 3
 4 Server version: 5.7.9-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> 

 

 

此時在命令行里輸入mysql能夠直接登錄mysql數據庫

2、設置到~/.my.cnf來配置免密碼

[root@rsync-test03 ~]# cat .my.cnf
[client]
user=root
password=123456
port = 3306
.......
與1類似不做贅述

3、通過login-path來登錄,這個好像知道的人比較少,相對來說比較安全,但是在生產環境中如果有人入侵你的環境了,破解一個二進制文件也不會有什么難度。所以目前生產環境中還未見到,在生產環境中,更改弱口令的時候,是通過讀取加密文件獲取密碼的!

現列出login-path的登錄實現方法

 1 [root@localhost bin]# mysql_config_editor set -G vml -S /tmp/mysql.sock -u root -p
 2 Enter password: 
 3 [root@localhost bin]# mysql_config_editor set -G vml  -u root -p
 4 Enter password: 
 5 WARNING : 'vml' path already exists and will be overwritten. 
 6  Continue? (Press y|Y for Yes, any other key for No) : y
 7 [root@localhost bin]# mysql_config_editor set -G vml -S /tmp/mysql.sock -u root -p
 8 Enter password: 
 9 WARNING : 'vml' path already exists and will be overwritten. 
10  Continue? (Press y|Y for Yes, any other key for No) : y

加不加mysql.sock都可以正常生成vm1文件

我們配置完成之后就會在這個用戶目錄下面生成一個.mylogin.cnf的二進制文件

[root@localhost bin]# file /root/.mylogin.cnf
/root/.mylogin.cnf: data

我們print之后就可以發現,我們創建了一個標簽,標簽的名字就是vml,密碼是加密的,我們這樣就看不到密碼了,並且這種存儲方式是二進制的,相對比較安全

1 [root@localhost bin]# mysql_config_editor print --all
2 [vml]
3 user = root
4 password = *****
5 socket = /tmp/mysql.sock

下面通過login-path方式登錄數據庫

 1 [root@localhost bin]# mysql --login-path=vm1
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 4
 4 Server version: 5.7.9-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> 

下邊內容是可能出現的報錯,我沒有試,在網上的

故障排查 
再使用MySQL免密碼登錄的過程中,提示如下報錯
權限全局可寫,任何一個用戶都可以寫。mysql擔心這種文件被其他用戶惡意修改,所以忽略掉這個配置文件。這樣mysql無法重啟。
[root@rsync-test03 ~]# mysql
mysql: [Warning] World-writable config file '/root/.my.cnf' is ignored.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
 
查看.my.cnf文件發現該文件的權限不對。重新授權
[root@rsync-test03 ~]# chmod 600 .my.cnf
[root@rsync-test03 ~]# ll .my.cnf
-rw------- 1 root root 47 Oct 31 19:06 .my.cnf

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM