GlareVPN使用日本在地伺服器‧日本本地IP位址,述不能訪問日本國內網站的情形將通通消失。  隆重介紹 Evo 社群系列賽 PlayStation 4 賽事   Nintendo Switch版《國夫君的三國志全員集合!》發售日終於確定!   令人驚異的360Hz刷新率!MSI旗下新品牌的「Oculux NXG253R」電競螢幕即將在日本開賣!   從石卷市誕生的角色扮演遊戲「羈絆幻想曲~海濱之國的大聖典~」開放下載!   Pokémon Presents放送決定!會發表「Pokémon 鑽石/珍珠」和「Pokémon LEGENDS 阿爾宙斯」的最新情報   經營遊戲直播的「Mildom」與「TOPANGA」簽訂直播合同   日本2022年畢業生的「人氣就業企業排行」大公開!遊戲業界的第1名果然是…!   《Horizon Forbidden West》遊戲畫面搶先看,只在今天的 State of Play 

MySQL 使用 mysql_config_editor 避免 CLI 出現密碼

商業

MySQL 在 Bash Shell、Script 執行,若有密碼直接打在命令列裡面,都會出現下述的警告 (Warning):

Warning: Using a password on the command line interface can be insecure

為何會有這個警告呢?

主要是平常 CLI 的命令,用 ps 都可以查看到,若密碼打在上面,密碼很容易就因此洩漏出去(不過 MySQL 新版有避免這個問題,密碼直接輸入,於 ps 是看不到密碼的)。

當然除了 ps 外,還有很多方法可以查看,在此就不繼續探討。

MySQL 使用 mysql_config_editor 避免 CLI 出現密碼

上述講到的 Warning,MySQL 為了解決此問題,有做一個 mysql_config_editor,會將主機的 host、帳號、密碼 等資訊存入並加密(AES ECB),直接使用來登入,相對會安全很多,此篇主要來介紹 mysql_config_editor 的幾個用法。

  • MySQL 官方文件:4.6.7 mysql_config_editor — MySQL Configuration Utility
    • mysql_config_editor [program_options] command [command_options] # 參數說明
    • mysql_config_editor 要注意 --login-path 等同是 project name 類似的意思,設定檔的新增、刪除、使用都是靠這個

下述範例與資料整理自官方文件:

一般設定檔內容:(此篇會用此設定檔當範例來設定 mysql_config_editor)

[client]
user = localuser
password = localpass
host = localhost
[remote]
user = remoteuser
password = remotepass
host = remote.example.com

新增 mysql_config_editor 設定

將上述設定檔,設定入 mysql_config_editor

  1. $ mysql_config_editor set --login-path=client --host=localhost --user=localuser --password Enter password: 輸入 "localpass"
  2. $ mysql_config_editor set --login-path=remote --host=remote.example.com --user=remoteuser --password Enter password: 輸入"remotepass"
  3. $ mysql_config_editor set --login-path=127.0.0.1 --host=127.0.0.1 --user=root --port=3306 --password
  4. ls ~/.mylogin.cnf # 可以看到此檔案,less 看都會是 binary
  5. $ mysql_config_editor print --all # 查看所有設定,密碼不會直接呈現 [client] user = localuser password = host = localhost [remote] user = remoteuser password = host = remote.example.com

使用 mysql_config_editor 登入、操作 SQL 命令

  • $ mysql --login-path=client # 登入 [client]
  • $ mysql --login-path=remote # 登入 [remote]
  • $ mysql --login-path=remote --host=remote2.example.com # 若需要登入其它主機,但是帳號、密碼一樣
  • $ mysql --login-path=client dbname -e "SQL statement" # 直接登入執行 SQL 語法

移除 mysql_config_editor 設定檔

  • $ mysql_config_editor remove --login-path=remote
  • $ mysql_config_editor remove --login-path=remote --user
  • $ mysql_config_editor reset # 全部重設

MySQL 5.7 標準 Client 的 mysql_config_editor 位置

  • 於此篇 mysql-client-5.7 可以查到 mysql_config_editor 的位置:/usr/bin/mysql_config_editor

至於使用 Percona 的話,5.6 版有 mysql_config_editor,5.7 版卻沒有,要怎麼解決呢?

找 Percona 的 mysql_config_editor 套件在哪裡

  1. dpkg -S mysql_config_editor # 找是哪個 package percona-server-client-5.6: /usr/share/man/man1/mysql_config_editor.1.gz percona-server-client-5.6: /usr/bin/mysql_config_editor
  2. sudo apt install percona-server-client # 若是 Percona 5.6 可以這樣查詢、安裝

Percona client 5.7 卻沒有 mysql_config_editor 的解法

  • apt-get install libperconaserverclient20-dev # 最後找到於此套件有 mysql_config_editor

相關網頁

Tsung

隨機商業新聞

NordVPN