mysql免密登录的几种方式

 

1、配置文件直接写出密码

 1  1 [root@localhost bin]# cat /etc/my.cnf
 2  2 [client]
 3  3 user=root
 4  4 password=123456
 5  5 
 6  6 [mysqld]
 7  7 ########basic settings########
 8  8 server-id = 11 
 9  9 port = 3306
10 10 user = mysql
11 11 socket=/var/lib/mysql/mysql.sock
12 12 #bind_address = 10.166.224.32
13 13 #autocommit = 0
14 14 character_set_server=utf8
15 15 skip_name_resolve = 1
16 16 max_connections = 800
17 17 max_connect_errors = 1000
18 18 #datadir = /data/mysql_data
19 19 datadir = /usr/local/mysql/data
20 20 basedir = /usr/local/mysql
21 21 transaction_isolation = READ-COMMITTED
22 22 explicit_defaults_for_timestamp = 1
23 23 join_buffer_size = 134217728
24 24 tmp_table_size = 67108864
25 25 tmpdir = /tmp
26 26 max_allowed_packet = 16777216
27 27 #sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
28 28 interactive_timeout = 1800
29 29 .......
 1 [root@localhost bin]# mysql
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 3
 4 Server version: 5.7.9-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
 7 
 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 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> 

此时在命令行里输入mysql能够直接登录mysql数据库

2、设置到~/.my.cnf来配置免密码

[root@rsync-test03 ~]# cat .my.cnf
[client]
user=root
password=123456
port = 3306
.......
与1类似不做赘述

3、通过login-path来登录,这个好像知道的人比较少,相对来说比较安全,但是在生产环境中如果有人入侵你的环境了,破解一个二进制文件也不会有什么难度。所以目前生产环境中还未见到,在生产环境中,更改弱口令的时候,是通过读取加密文件获取密码的!

现列出login-path的登录实现方法

 1 [root@localhost bin]# mysql_config_editor set -G vml -S /tmp/mysql.sock -u root -p
 2 Enter password: 
 3 [root@localhost bin]# mysql_config_editor set -G vml  -u root -p
 4 Enter password: 
 5 WARNING : 'vml' path already exists and will be overwritten. 
 6  Continue? (Press y|Y for Yes, any other key for No) : y
 7 [root@localhost bin]# mysql_config_editor set -G vml -S /tmp/mysql.sock -u root -p
 8 Enter password: 
 9 WARNING : 'vml' path already exists and will be overwritten. 
10  Continue? (Press y|Y for Yes, any other key for No) : y

加不加mysql.sock都可以正常生成vm1文件

我们配置完成之后就会在这个用户目录下面生成一个.mylogin.cnf的二进制文件

[root@localhost bin]# file /root/.mylogin.cnf
/root/.mylogin.cnf: data

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

扫描二维码关注公众号,回复: 9266755 查看本文章
1 [root@localhost bin]# mysql_config_editor print --all
2 [vml]
3 user = root
4 password = *****
5 socket = /tmp/mysql.sock

下面通过login-path方式登录数据库

 1 [root@localhost bin]# mysql --login-path=vm1
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 4
 4 Server version: 5.7.9-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
 7 
 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 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> 

下边内容是可能出现的报错,我没有试,在网上的

故障排查 
再使用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

猜你喜欢

转载自www.cnblogs.com/shy-1208/p/12332158.html