MySQL本地密码加密及免密登录

在日常运维管理的过程中,经常有需要保存密码,但是又不希望明文密码泄露的情况
mysql_config_editor 是MySQL官方提供的一个保存用户密码的工具,把用户名及密码信息,以二进制的方式存储在用户家目录下的隐藏文件 .mylogin.cnf 中。
从而实现操作系统登陆之后的免密登陆。
查看官方文档,可以看到在5.6 中 已经提供了相关说明,说明该工具在5.6版本中就已经存在可用了。

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 参数

1. 使用  mysql_config_editor 保存本地登陆密码

[root@wl ~]# mysql_config_editor set --login-path=client --user=root --password
Enter password:      //输入root用户密码

2.使用默认密码登陆

[root@wl ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 

3.查看当前的连接信息

mysql> select session_user();
+----------------+
| session_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.02 sec)

其实在登陆系统的过程中,需要设置 --login-path 为client 或者remote ,不指定该参数的时候,使用默认的 client

4.设置远程登陆用户

[root@wl ~]# mysql_config_editor  set --login-path=remote  --user=root --host=192.168.1.130 --password
Enter password: 
WARNING : 'remote' path already exists and will be overwritten. 
 Continue? (Press y|Y for Yes, any other key for No) : yes

5.登陆远程系统

[root@wl ~]# mysql --login-path=remote
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 

6.查看已经保存的登陆信息

[root@wl ~]# mysql_config_editor print --all
[client]
user = root
password = *****
[remote]
user = root
password = *****
host = 192.168.1.130

7.删除登陆用户

##删除配置login path
[root@wl ~]# mysql_config_editor remove --login-path=remote
[root@wl ~]# mysql_config_editor print --all
也可以删除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 添加登陆端口到登陆文件中

##删除某个用户
[root@wl ~]# mysql_config_editor remove --login-path='remote' --user=cs
mysql_config_editor: [Warning] mysql_config_editor: ignoring option '--user' due to invalid value 'cs'

##清空配置login path
[root@wl ~]# mysql_config_editor reset
[root@wl ~]# mysql_config_editor print --all

 

posted @ 2022-09-29 14:00  聆听说书人  阅读(800)  评论(0编辑  收藏  举报