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 --helpmysql_config_editor Ver 1.0 Distrib 5.7.30, for Linux on x86_64Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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 FALSEWhere command can be any one of the following :set [command options] Sets user name/password/host name/socket/portfor 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 specifiedlogin 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.cnf0000000 0000 0000 0e16 0c1f 1014 1915 0910 1b010000010 0b08 000e 0b11 1516 0010 0000 7c87 f22d0000020 b92c 751f 4750 d5bd 3db3 1558 0010 00000000030 7b3b 4bcf a986 0921 5ea2 197f 5ad7 9cd20000040 0020 0000 2b28 3ecc ffee 9d8e 70c5 c9ac0000050 8a40 2e89 74dd a9db f67f 34d1 f0b2 10b50000060 d7f0 2c17 0020 0000 30b5 8f7f 9f20 dc0d0000070 63a8 c83e 17a0 7792 997d 23e4 02ee c7880000080 de49 c2da cd06 9993 0010 0000 1e9f c9040000090 e3dd ea6c 8db5 d28a b17e dfc7000009c
打印/查看配置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
内容来源于网络如有侵权请私信删除