mysql_config_editor介紹
mysql_config_editor是MySQL自帶的一款用於安全加密登錄的工具,可以在一些場合避免使用密碼明文,例如,寫shell腳本時,不用在為在腳本里面寫入明文密碼糾結了;也可以用於管理多台MySQL實例。另外,像如果使用mysql命令登錄數據庫,可以避免每次都要輸入一堆參數。簡單方便。
官方文檔介紹如下:
The mysql_config_editor utility enables you to store authentication credentials in an obfuscated login path file named .mylogin.cnf. The file location is the %APPDATA%\MySQL directory on Windows and the current user's home directory on non-Windows systems. The file can be read later by MySQL client programs to obtain authentication credentials for connecting to MySQL Server.
mysql_config_editor使用
幫助信息查看
# mysql_config_editor --help
mysql_config_editor Ver 1.0 Distrib 5.7.30, for Linux on x86_64
Copyright (c) 2012, 2020, 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.
Usage: mysql_config_editor [program options] [command [command options]]
-#, --debug[=#] This is a non-debug version. Catch this and exit.
-?, --help Display this help and exit.
-v, --verbose Write more information.
-V, --version Output version information and exit.
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.
新增配置login path
mysql_config_editor對應的參數信息如下:
· help 顯示幫助
· --login-path=name,-G name
· --host=host_name,-h host_name 主機名
· --password,-p 密碼,注意這個地方不能使用“=”直接寫入密碼
· --port=port_num,-P port_num 端口號
· --socket=file_names,-S file_name 文件名
· --user=user_name,-u user_name 用戶名
· --warn,-w 默認開啟,提示警告信息,如果要忽略警告,使用--skip-warn 參數
#mysql_config_editor set --login-path=dbadmin --user=root --host=localhost --port=3306 --password
你會發現新增login path后,就會在當前用戶的根目錄生成隱藏文件.mylogin.cnf(如果是Windows的話,此文件位於%APPDATA%\MySQL目錄下面)
# ls -lrt ~/.mylogin.cnf
-rw-------. 1 root root 156 Aug 14 15:32 /root/.mylogin.cnf
# hexdump ~/.mylogin.cnf
0000000 0000 0000 0e16 0c1f 1014 1915 0910 1b01
0000010 0b08 000e 0b11 1516 0010 0000 7c87 f22d
0000020 b92c 751f 4750 d5bd 3db3 1558 0010 0000
0000030 7b3b 4bcf a986 0921 5ea2 197f 5ad7 9cd2
0000040 0020 0000 2b28 3ecc ffee 9d8e 70c5 c9ac
0000050 8a40 2e89 74dd a9db f67f 34d1 f0b2 10b5
0000060 d7f0 2c17 0020 0000 30b5 8f7f 9f20 dc0d
0000070 63a8 c83e 17a0 7792 997d 23e4 02ee c788
0000080 de49 c2da cd06 9993 0010 0000 1e9f c904
0000090 e3dd ea6c 8db5 d28a b17e dfc7
000009c
打印/查看配置login path
# mysql_config_editor print --login-path=dbadmin
[dbadmin]
user = root
password = *****
host = localhost
port = 3306
#查看所有的login path信息
# mysql_config_editor print --all
清空配置login path
# mysql_config_editor reset
# mysql_config_editor print --all
刪除配置login path
# mysql_config_editor remove --login-path=dbadmin
也可以刪除login path中的某一個項。
-h,–host=name 添加host到登陸文件中
-G,–login-path=name 在登錄文件中為login path添加名字(默認為client)
-p,–password 在登陸文件中添加密碼(該密碼會被mysql_config_editor自動加密)
-u,–user 添加用戶名到登陸文件中
-S,–socket=name 添加sock文件路徑到登陸文件中
-P,–port=name 添加登陸端口到登陸文件中
mysql_config_editor的“Bug”
1:mysql_config_editor can not deal password with "#"
使用mysql_config_editor創建了login path后,使用是遇到這個錯誤。
# mysql --login-path=admin
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
出錯的原因是賬號密碼中包含特殊字符#,在創建login path的時候必須用雙引號將密碼包裹,才可以避免遇到這種錯誤。其實這個也不完全算一個Bug,因為解析.mylogin.cnf文件時,將#當成了注釋符號。
The problem is that the .mylogin.cnf file interprets the # as the beginning of a comment.
參考資料:
https://bugs.mysql.com/bug.php?id=95597
https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html