在撰寫管理mysql的相關shell script時,苦於進DB撈資料時,不得不把密碼直接寫在腳本裡
除了每次執行script都會show 出錯誤提示,明碼寫在script裡也不安全
Warning: Using a password on the command line interface can be insecure.
當然可以在script執行時使用資料導向解決 > /dev/null 2>&1
但是後來發現了個不錯的內建工具mysql_config_editor,可解決這個問題,以下是使用的心得
mysql_config_editor簡單說就是一個建立有關mysql登入profile的工具
利用此工具建立profile後,相關支援–login-path參數的client指令就能利用profile登入
先help一下…..,或看官方documentation會有更清楚的說明
[root@localhost ~]# mysql_config_editor --help ..略.. Variables (--variable-name=value) and boolean options {FALSE|TRUE} Value (after reading options) --------------------------------- ---------------------------------------- verbose FALSE Where command can be any one of the following : set [command options] Sets user name/password/host name/socket/port for a given login path (section). remove [command options] Remove a login path from the login file. print [command options] Print all the options for a specified login path. reset [command options] Deletes the contents of the login file. help Display this usage/help information.
先用set建立一組profile試看看
[root@localhost ~]# mysql_config_editor set \
--login-path='andyroot' \
--user='root' \
--host='127.0.0.1' \
--socket='/var/lib/mysql/mysql.sock' \
--port=3306 \
--password
Enter password:
用print查看已建立的profile,密碼是遮蔽的
[root@localhost ~]# mysql_config_editor print --all [andyroot] user = root password = ***** host = 127.0.0.1 socket = /var/lib/mysql/mysql.sock port = 3306
接下來在shell上使用這個profile登入,成功! 運用在shell script上也不會報錯了
[root@localhost ~]# mysql --login-path='andyroot' Welcome to the MySQL monitor. Commands end with ; or \g. mysql>
除了不用再輸入帳密,還能在profile定義host,port,socket等資訊,也就是如果常常在這台主機管理多個mysql實例的,使用login-path來登入會方便許多
也增加了點安全性,因為建立的profile會在家目錄建立一個加密的.mylogin.cnf檔案,如此一來其他帳戶無法用此profile登入,也看不懂此檔案內的資訊
[root@localhost ~]# cat ~/.mylogin.cnf p،▒▒▒▒j▒▒Y[dC▒h▒▒ 3▒▒▒▒▒▒h/▒<▒v▒ƚ▒uh▒6IOդD▒(2▒▒}▒▒▒$#▒▒▒▒^)s]܊▒▒▒▒▒p▒d▒ݤi▒o=▒b▒Ԧ@▒▒^_(<g▒ٔ{u~▒0x▒>g▒5▒▒▒j▒4▒▒▒4 ^▒▒▒g▒▒▒ز%▒R▒V▒/▒0▒p+▒▒ f▒▒2▒mF▒▒▒kN▒▒▒{`▒▒▒]▒$U0▒'▒f*s9y▒▒▒ ▒▒▒>D]MbÑ▒R]4▒2▒▒h▒" w9▒v▒1▒L▒[▒ޭQ▒.▒I▒?z▒▒▒[
不過如果該OS帳戶或root被攻破,有心者只要下mysql_config_editor print –all就能查到有甚麼profile可以運用,就能藉此登入資料庫,也算是個兩面刃
頁面: 1 2
對「Mysql_config_editor使用心得」的一則回應