一、mysql_config_editor工具介绍
- mysql_config_editor能够将身份认证信息存储在一个名为.mylogin.cnf文件里,该文件的位置在windows下是在%APPDATA%\MySQL目录下,linux下是在用户的家目录下/root/.mylogin.cnf。该文件可在以后通过MySQL客户端程序可以读取,以获得身份验证凭据用于连接到MySQL服务器。
- 该工具至少在mysql5.6.6以上的版本才可用。
- 该工具仅仅存储: host, user, password, port and socket. 信息。
二、创建login-path
[root@mysql mysql]# mysql_config_editor set --login-path=login --user=root --password --host=localhost --socket= /opt/mysql/3306/mysql.sock1
也可以简写:
[root@mysql mysql]# mysql_config_editor set -G login -u root -p -h localhost -S /opt/mysql/3306/mysql.sock1
也可以不使用--login-path参数:
[root@mysql mysql]# mysql_config_editor set -u root -p -h localhost -S /opt/mysql/3306/mysql.sock1
三、登录
[root@mysql mysql]# mysql_config_editor set -G login -u root -p -h localhost -S /opt/mysql/3306/mysql.sock1
[root@mysql mysql]# mysql --login-path=login
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
- 不带--login-path参数,直接使用mysql登录:
[root@mysql mysql]# mysql_config_editor set -u root -p -h localhost -S /opt/mysql/3306/mysql.sock1
[root@mysql mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
四、查看.mylogin.cnf
[root@mysql mysql]# mysql_config_editor print --all
[client]
user = root
password = *****
host = localhost
socket = /opt/mysql/3306/mysql.sock1
[login]
user = root
password = *****
host = localhost
socket = /opt/mysql/3306/mysql.sock1
[root@mysql mysql]# mysql_config_editor print --login-path=login
[login]
user = root
password = *****
host = localhost
socket = /opt/mysql/3306/mysql.sock1
五、移除login-path内容
remove需要指定 --host, --password, --port, --socket, and --user options选项值,如果一个值都没有指定,则会remove整个login-path
[root@mysql mysql]# mysql_config_editor remove
WARNING : No login path specified, so options from the default login path will be removed.
Continue? (Press y|Y for Yes, any other key for No) :
[root@mysql mysql]# mysql_config_editor remove -G login
[root@mysql mysql]# mysql_config_editor print --all
[client]
user = root
password = *****
host = localhost
socket = /opt/mysql/3306/mysql.sock1
[root@mysql mysql]# mysql_config_editor remove -G client -u
[root@mysql mysql]# mysql_config_editor print --all
[client]
password = *****
host = localhost
socket = /opt/mysql/3306/mysql.sock1
六、清空login-path内容
[root@mysql mysql]# mysql_config_editor reset
[root@mysql mysql]# mysql_config_editor print --all