当前位置: 代码网 > it编程>数据库>Mysql > MySQL实现免密登录的三种配置方式

MySQL实现免密登录的三种配置方式

2024年05月15日 Mysql 我要评论
一、示例环境版本说明操作系统版本centos7.6[wuhs@test1 mysql]$ cat /etc/redhat-releasecentos linux release 7.6.1810 (c

一、示例环境版本说明

操作系统版本centos7.6

[wuhs@test1 mysql]$ cat /etc/redhat-release
centos linux release 7.6.1810 (core)

mysql数据库版本5.7.32

[wuhs@test1 mysql]$ mysql -v
mysql ver 14.14 distrib 5.7.32, for el7 (x86_64) using editline wrapper

二、mysql免密登录方式配置示例

1、通过设置client标签,直接编辑/etc/my.cnf文件

编辑/etc/my.cnf文件,添加如下代码

[wuhs@test1 mysql]$ cat /etc/my.cnf
[client]
user = root
password = 123456
port = 3306

配置完成后可以使用mysql命令直接登录数据库

[wuhs@test1 mysql]$ mysql
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 6
server version: 5.7.32-log mysql community server (gpl)
<br>
copyright © 2000, 2020, oracle and/or its affiliates. all rights reserved.
<br>
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
<br>
type ‘help;' or ‘\h' for help. type ‘\c' to clear the current input statement.
<br>
mysql>

此方式最大问题是明文存储密码,见配置文件各用户可见,非常的不安全。

2、我们通过my.cnf来配置,设置到~/.my.cnf来配置免密码

编辑~/.my.cnf文件,添加如下代码

[wuhs@test1 mysql]$ cat ~/.my.cnf
[client]
user = root
password = 123456
port = 3306

修改my.cnf属性

#chmod 600 ~/.my.cnf
[wuhs@test1 mysql]$ ll ~/.my.cnf
-rw-------. 1 wuhs wuhs 51 dec 29 22:56 /home/wuhs/.my.cnf

配置完成后可以使用mysql命令直接登录数据库

[wuhs@test1 mysql]$ mysql
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 6
server version: 5.7.32-log mysql community server (gpl)
<br>
copyright © 2000, 2020, oracle and/or its affiliates. all rights reserved.
<br>
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
<br>
type ‘help;' or ‘\h' for help. type ‘\c' to clear the current input statement.
<br>
mysql>

此种方式也是明文存储,配置方式同第一种,文件为隐藏文件,设置文件为改用户可读,与第一种方式相比安全性有所提高。经验证测试,~/.my.cnf配置文件优先于/etc/my.cnf。

3、通过mysql_config_editor命令

使用mysql_config_editor命令一个test标签

[wuhs@test1 mysql]$ mysql_config_editor set -g test -s /tmp/mysql.sock -uroot -p
enter password: [此处输入root账户密码]

执行如上步骤后生成了隐藏文件.mylogin.cnf,文件类型为data,是一个二进制文件

[wuhs@test1 mysql]$ file ~/.mylogin.cnf
/home/wuhs/.mylogin.cnf: data

查看该文件,密码为加密存储

[wuhs@test1 mysql]$ mysql_config_editor print --all
[test]
user = root
password = *****
socket = /tmp/mysql.sock

使用mysql --login-path="标签"登录

[wuhs@test1 mysql]$ mysql --login-path=test
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 18
server version: 5.7.32-log mysql community server (gpl)
<br>
copyright © 2000, 2020, oracle and/or its affiliates. all rights reserved.
<br>
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
<br>
type ‘help;' or ‘\h' for help. type ‘\c' to clear the current input statement.
<br>
mysql>

第三种方式登录文件为隐藏的二进制文件,且密码通过密文存储,安全性最高。

以上就是mysql免密登录的三种方式的详细内容,更多关于mysql免密登录的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com