1. 程式人生 > 其它 >關於Oracle和MySQL中的無密碼登入 (r5筆記第39天)

關於Oracle和MySQL中的無密碼登入 (r5筆記第39天)

無密碼登入在一定程度上能夠簡化流程,對於密碼敏感,但是又需要提供訪問許可權的情況下是一個不錯的選擇。尤其是在乙方在做一些操作的時候,要密碼和給密碼是一個糾結的問題。不給沒法工作,給了又對資訊保安又影響。 在Oracle和MySQL中都有相應的解決方案,大道至簡,這個功能的目的都是類似的。 在Oracle中可以通過設定wallet來實現,在10g版本開始支援。而在MySQL中自5.6版本開始可以使用--login-path來實現。 先來看看Oracle中的wallet實現無密碼登入,可以通過mkstore來配置,我們可以使用--help得到命令使用的幫助。 [ora11g@oel1 admin]$ mkstore --help Oracle Secret Store Tool : Version 11.2.0.1.0 - Production Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved. No wallet location specified. mkstore [-wrl wrl] [-create] [-createSSO] [-createLSSO] [-createALO] [-delete] [-deleteSSO] [-list] [-createEntry alias secret] [-viewEntry alias] [-modifyEntry alias secret] [-deleteEntry alias] [-createCredential connect_string username password] [-listCredential] [-modifyCredential connect_string username password] [-deleteCredential connect_string] [-help] [-nologo]

我們首先來建立錢包,指定錢包路徑為/u02/ora11g/wallet,對於密碼還是有一定的要求,太簡單也不行。 $ mkstore -wrl /u02/ora11g/wallet -create Oracle Secret Store Tool : Version 11.2.0.1.0 - Production Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved. Enter password: Enter password again: 生成錢包後,會在指定的路徑下生成兩個檔案。 $ ll total 8 -rw------- 1 ora11g dba 3589 May 17 21:37 cwallet.sso -rw------- 1 ora11g dba 3512 May 17 21:37 ewallet.p12
我們可以指定臨時的連線串來配置到錢包裡面,比如我們認為test11g是一個臨時連線串,可以使用tnsping來測試,確保連線串是可訪問的。 $tnsping test11g Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = oel1.oracle.com)(PORT = 1511))) (CONNECT_DATA = (SERVICE_NAME = TEST11G))) OK (0 msec) 配置完成之後,我們需要在登入之前在sqlnet.ora中配置錢包的路徑。sqlnet.ora中需要配置的內容如下: $ cat sqlnet.ora WALLET_LOCATION = (SOURCE = (METHOD = FILE) (METHOD_DATA = (DIRECTORY = /u02/ora11g/wallet) ) ) SQLNET.WALLET_OVERRIDE=true
這些配置都搞定以後我們就可以指定對應的連線串,對應的使用者名稱密碼。 $ mkstore -wrl /u02/ora11g/wallet -createCredential test11g n1 n1 Oracle Secret Store Tool : Version 11.2.0.1.0 - Production Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved. Enter wallet password: l 1 Create credential oracle.security.client.connect_string1 配置完成之後工作就完成了,我們可以簡單驗證一下。 $ sqlplus /@test11g SQL*Plus: Release 11.2.0.1.0 Production on Sun May 17 21:45:59 2015 With the Partitioning, OLAP, Data Mining and Real Application Testing options n1@TEST11G> 而如果使用MySQL來實現,則需要通過mysql_config_editor來配置。 mysql_config_editor的命令提示如下,可以看出可使用的選項還是相對比較簡單的。 [mysql@oel1 ~]$ mysql_config_editor set --help mysql_config_editor Ver 1.0 Distrib 5.6.23, for linux-glibc2.5 on i686 Copyright (c) 2012, 2015, 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. MySQL Configuration Utility. Description: Write a login path to the login file. Usage: mysql_config_editor [program options] [set [command options]] -?, --help Display this help and exit. -h, --host=name Host name to be entered into the login file. -G, --login-path=name Name of the login path to use in the login file. (Default : client) -p, --password Prompt for password to be entered into the login file. -u, --user=name User name to be entered into the login file. -S, --socket=name Socket path to be entered into login file. -P, --port=name Port number to be entered into login file. -w, --warn Warn and ask for confirmation if set command attempts to overwrite an existing login path (enabled by default). (Defaults to on; use --skip-warn to disable.) 我們直接可以通過一個命令來完成配置,制定這個無密碼登入的別名為fastlogin

[mysql@oel1 ~]$ mysql_config_editor set --login-path=fastlogin --user=root --host=localhost --password --socket=/u02/mysql/mysqld_mst.sock Enter password: 配置完成之後,會在當前路徑下生成一個隱藏檔案.mylogin.cnf [mysql@oel1 ~]$ ll -la .mylogin* -rw------- 1 mysql dba 480 May 17 22:10 .mylogin.cnf [mysql@oel1 ~]$ 大功告成,這個時候直接登入即可。 [mysql@oel1 ~]$ mysql --login-path=fastlogin Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 3 Server version: 5.6.23-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial) Copyright (c) 2000, 2015, 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>