使用 mysql_config_editor 工具 MySQL 去明文密码

mysql_config_editor 是一个 MySQL 自带的一款用于安全加密登录的工具,使用这个工具,可以将登录 MySQL 的 username、password、port、socket 文件等信息存入一个名为 .mylogin.cnf 的隐藏文件中(注意:该文件进行了模糊处理,不能明文读取,可以防止密码泄露。

mysql_config_editor 是一个 MySQL 自带的一款用于安全加密登录的工具,使用这个工具,可以将登录 MySQL 的 username、password、port、socket 文件等信息存入一个名为 .mylogin.cnf 的隐藏文件中(注意:该文件进行了模糊处理,不能明文读取,可以防止密码泄露。但是,mysql_config_editor 的 print 命令可以用于显示该文件的内容,密码除外)。

在 Windows 上 .mylogin.cnf 位于 %APPDATA%\MySQL 目录;在 Linux 系统上,.mylogin.cnf 文件位于用户的主目录,如 root 用户在 /root 目录。

.mylogin.cnf 文件可以被 MySQL 客户端程序读取,以便成功连接到 MySQL 服务器。

使用 mysql_config_editor 可以管理一台或多台 MySQL 实例。它解决了在命令行、脚本里直接输入明文密码,带来一系列的安全隐患了。不仅方便,而且安全。

mysql_config_editor 实践

添加登录配置

C:\Users\Administrator> mysql_config_editor set --login-path=db_root --host=127.0.0.1 --user=root -p
Enter password: *********

注,笔者执行成功后,.mylogin.cnf 文件位于 C:\Users\Administrator\AppData\Roaming\MySQL 目录。

显示指定名称的登陆信息

C:\Users\Administrator> mysql_config_editor print --login-path=db_root
[db_root]
user = root
password = *****
host = 127.0.0.1

显示所有登陆信息

C:\Users\Administrator> mysql_config_editor print --all
[db_root]
user = root
password = *****
host = 127.0.0.1
[db_hxstrive]
user = root
password = *****
host = 127.0.0.1

删除登录配置

C:\Users\Administrator> mysql_config_editor remove --login-path=db_hxstrive

注意:删除名为 db_hxstrive 的登录配置项

登录 MySQL 服务

使用上面创建的 db_root 登录配置项去登录 MySQL 服务,适用范围:mysql、mysqladmin、mysqldump

C:\Users\Administrator> mysql --login-path=db_root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

总结

通过上面的实践,使用 mysql_config_editor 管理 MySQL 服务的登录配置信息确实非常方便,避免每次都重复输入登录信息,并且也避免了在控制台中输入明文,如果明文是随机生成的长字符串,直接输入简直就是折磨人。不过,将登录信息直接保存到文件中,如果该文件丢失,或者被别人盗取了,是不是问题就大了(笔者试过,将 .mylogin.cnf 拷贝到其他机器上面可以使用)。

游手好闲地学习,并不比学习游手好闲好。 —— 约翰·贝勒斯
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号