MySQL Utilities 支援三種指定連接參數(使用者、主機、密碼、埠、Socket)的方法:
<login-path>[:<port>][:<socket>]<config-file-path>[:<section>]<user>[:<passwd>]@<host>[:<port>][:<socket>]透過 mysql_config_editor 建立加密登入檔,不會在程序列表或日誌中洩露密碼。
# cd /usr/local/mysql5.6/
./bin/mysql_config_editor set --login-path=instance_3366 --host=localhost --user=root --port=3366 --password
Enter password: ********
./bin/mysql_config_editor print --login-path=instance_3366
[instance_3366]
user = root
password = *****
host = localhost
port = 3366
# 指定登入路徑
mysqlserverinfo --server=instance_3366 --format=vertical
# 或直接指定參數覆蓋
mysqlserverinfo --server=root@localhost:3366:/tmp/mysql3366.sock --format=vertical
在 my.cnf 中定義 [client] 區段:
[client]
port = 3366
socket = /tmp/mysql3366.sock
user = root
連接時指定配置檔路徑與區段:
mysqlserverinfo --server=/usr/local/mysql5.6/my.cnf[client] --format=vertical
優點: 比命令列安全;缺點: 明文儲存,檔案權限需嚴格控制(chmod 600)。
mysqlserverinfo --server=root:password@localhost:3306 --format=vertical
密碼會出現在程序列表(ps -ef)與 Shell 歷史記錄中,僅適合測試環境。
若自行開發工具,可使用下列三種方式傳遞連接參數:
dest_values = {
"user": "root",
"passwd": "secret",
"host": "localhost",
"port": 3308,
"unix_socket": None
}
<user>[:<passwd>]@<host>[:<port>][:<socket>]字串解析可使用 options.parse_connection() 函數。