翼度科技»论坛 编程开发 mysql 查看内容

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

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
一、示例环境版本说明

操作系统版本centos7.6
  1. [wuhs@test1 mysql]$ cat /etc/redhat-release
  2. CentOS Linux release 7.6.1810 (Core)
复制代码
mysql数据库版本5.7.32
  1. [wuhs@test1 mysql]$ mysql -V
  2. mysql Ver 14.14 Distrib 5.7.32, for el7 (x86_64) using EditLine wrapper
复制代码
二、MySQL免密登录方式配置示例


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

编辑/etc/my.cnf文件,添加如下代码
  1. [wuhs@test1 mysql]$ cat /etc/my.cnf
  2. [client]
  3. user = root
  4. password = 123456
  5. port = 3306
复制代码
配置完成后可以使用mysql命令直接登录数据库
  1. [wuhs@test1 mysql]$ mysql
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 6
  4. Server version: 5.7.32-log MySQL Community Server (GPL)
  5. <br>
  6. Copyright © 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  7. <br>
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. <br>
  12. Type ‘help;' or ‘\h' for help. Type ‘\c' to clear the current input statement.
  13. <br>
  14. mysql>
复制代码
此方式最大问题是明文存储密码,见配置文件各用户可见,非常的不安全。

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

编辑~/.my.cnf文件,添加如下代码
  1. [wuhs@test1 mysql]$ cat ~/.my.cnf
  2. [client]
  3. user = root
  4. password = 123456
  5. port = 3306
复制代码
修改my.cnf属性
  1. #chmod 600 ~/.my.cnf
  2. [wuhs@test1 mysql]$ ll ~/.my.cnf
  3. -rw-------. 1 wuhs wuhs 51 Dec 29 22:56 /home/wuhs/.my.cnf
复制代码
配置完成后可以使用mysql命令直接登录数据库
  1. [wuhs@test1 mysql]$ mysql
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 6
  4. Server version: 5.7.32-log MySQL Community Server (GPL)
  5. <br>
  6. Copyright © 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  7. <br>
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. <br>
  12. Type ‘help;' or ‘\h' for help. Type ‘\c' to clear the current input statement.
  13. <br>
  14. mysql>
复制代码
此种方式也是明文存储,配置方式同第一种,文件为隐藏文件,设置文件为改用户可读,与第一种方式相比安全性有所提高。经验证测试,~/.my.cnf配置文件优先于/etc/my.cnf。

3、通过mysql_config_editor命令

使用mysql_config_editor命令一个test标签
  1. [wuhs@test1 mysql]$ mysql_config_editor set -G test -S /tmp/mysql.sock -uroot -p
  2. Enter password: [此处输入root账户密码]
复制代码
执行如上步骤后生成了隐藏文件.mylogin.cnf,文件类型为data,是一个二进制文件
  1. [wuhs@test1 mysql]$ file ~/.mylogin.cnf
  2. /home/wuhs/.mylogin.cnf: data
复制代码
查看该文件,密码为加密存储
  1. [wuhs@test1 mysql]$ mysql_config_editor print --all
  2. [test]
  3. user = root
  4. password = *****
  5. socket = /tmp/mysql.sock
复制代码
使用mysql --login-path="标签"登录
  1. [wuhs@test1 mysql]$ mysql --login-path=test
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 18
  4. Server version: 5.7.32-log MySQL Community Server (GPL)
  5. <br>
  6. Copyright © 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  7. <br>
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. <br>
  12. Type ‘help;' or ‘\h' for help. Type ‘\c' to clear the current input statement.
  13. <br>
  14. mysql>
复制代码
第三种方式登录文件为隐藏的二进制文件,且密码通过密文存储,安全性最高。
以上就是MySQL免密登录的三种方式的详细内容,更多关于MySQL免密登录的资料请关注脚本之家其它相关文章!

来源:https://www.jb51.net/database/318805ucg.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具