mysql5.7免密登录

转载: https://www.cnblogs.com/hanglinux/p/11773255.html

方法一:通过设置client标签,直接编辑/etc/my.cnf文件

[client]
user=root
password=123456
port = 3306
我们直接输入mysql就可以进行登录mysql数据库,这种不好的地方就是我们的密码是不加密的,可见的,是不安全的。
 
复制代码
[root@tz-ftp-yum ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 17
Server version: 5.7.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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.
root@localhost [(none)]>
复制代码

  

方法二:通过mysql_config_editor命令。

[root@tz-ftp-yum ~]# mysql_config_editor set -G vml -S /tmp/mysql.sock -u root -p
Enter password:
[root@tz-ftp-yum ~]#
我们配置完成之后就会在这个用户目录下面生成一个.mylogin.cnf的二进制文件
 
[root@tz-ftp-yum ~]# file /root/.mylogin.cnf
/root/.mylogin.cnf: data

我们print之后就可以发现,我们创建了一个标签,标签的名字就是vml,密码是加密的,我们这样就看不到密码了,并且这种存储方式是二进制的,相对比较安全

 
[root@tz-ftp-yum ~]# mysql_config_editor print --all
[vml]
user = root
password = *****
socket = /tmp/mysql.sock

我们可以通过--login-path=vml这种方式进行免密码登录数据库 

复制代码
[root@tz-ftp-yum ~]# mysql --login-path=vml
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 20
Server version: 5.7.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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.
root@localhost [(none)]>
复制代码
方法三:我们通过my.cnf来配置,设置到~/.my.cnf来配置免密码
[root@rsync-test03 ~]# cat .my.cnf
[client]
user=root
password=123456
port = 3306

故障排查 

再使用MySQL免密码登录的过程中,提示如下报错

权限全局可写,任何一个用户都可以写。mysql担心这种文件被其他用户恶意修改,所以忽略掉这个配置文件。这样mysql无法重启。

[root@rsync-test03 ~]# mysql
mysql: [Warning] World-writable config file '/root/.my.cnf' is ignored.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

查看.my.cnf文件发现该文件的权限不对。重新授权

[root@rsync-test03 ~]# chmod 600 .my.cnf
[root@rsync-test03 ~]# ll .my.cnf
-rw------- 1 root root 47 Oct 31 19:06 .my.cnf
原文地址:https://www.cnblogs.com/coolruo/p/14278031.html