前言
- MySQL版本:5.7.10
- 本文介绍的免密登录方式的原理就是在配置文件中设置mysql客户端命令行工具的默认参数,实现登录时可以直接使用设置参数进行登录,相当于设置了默认登录用户,并且只在修改了配置文件的主机上使用MySQL客户端登录时才有效
查看配置配置文件生效顺序
使用mysql --help
命令可以输出mysql
命令行工具的使用帮助手册,其中Default options are read from the following files in the given order:
字样的后续输出即为配置文件的读取和生效顺序,后者文件中的参数设置会覆盖前者
Liunx上可以使用以下命令查看:
mysql --help | tail -n +$(mysql --help | grep -n 'given order' | cut -d ':' -f 1) | head -n2
[tomandersen@localhost ~]$ mysql --help | tail -n +$(mysql --help | grep -n 'given order' | cut -d ':' -f 1) | head -n2
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf /usr/local/mysql/my.cnf ~/.my.cnf
在配置文件中设置相应工具的默认参数
以~/.my.cnf配置文件为例,设置mysql、mysqldump工具的默认参数:
[mysql]
user=root
password=root
[mysqldump]
user=root
password=root
于是在使用mysql客户端、mysqldump工具时能够使用默认用户免密登录:
[tomandersen@localhost ~]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 82
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.
mysql>