「MySQL」 – 使用「登录路径(login-paths)」存储帐号信息

在~/.mylogin.cnf中存储了访问服务器的连接及身份凭据信息。此文件不仅加密存储,而且MySQL Utilities访问服务器的任何连接信息都不会出现在日志中,因此日志中不会显示用户名,密码,端口等。这些都是出于安全考虑,不因该在日志中出现敏感信息。

MySQL Utilities支持在连接中使用“登录路径”,格式:login-path-name [:port] [:socket],其中port和socket是可选的,如果使用了,则会覆盖指定的“登录路径”文件中的相对应的选项。

当使用“登录路径”时,在指定套接字时,除了POSIX系统之外,没有默认值。在这种情况下,host选项在端口3306上默认为localhost。这表示将“登录路径”中指定的值至少要指定一个用户,一个主机名和一个端口,或者socket。

配置与查看.mylogin.cnf文件

使用mysql_config_editor命令添加连接信息,示例如下:

#!/bin/bash

mysql_config_editor set --login-path=instance_13001 --host=localhost --user=root --port=13001 --password

使用以下命令确认登录路径数据已正确添加到.mylogin.cnf(这是个加密文件无法直接查看)中:

#!/bin/bash

mysql_config_editor print --login-path=instance_13001

# 该命令会输入如下内容:
# [instance_13001]
# user = root
# password = *****
# host = localhost
# port = 13001

一旦配置了.mylogin.cnf文件,连接时只需要指定”服务器实例”。例如,在前面的例子中的’instance_13001’,因此我们可以使用–server=instance_13001。如下示例:

#!/bin/sh

mysqlserverinfo --server=instance_13001 --format=vertical

# 输出类似如下内容:
#
# # Source on localhost: ... connected.
# *******************  1. row  *******************
# server: localhost:13001
# config_file: /etc/my.cnf, /etc/mysql/my.cnf
# binary_log: clone-bin.000001
# binary_log_pos: 341
# relay_log:
# relay_log_pos:
# version: 5.6.17-log
# datadir: /Volumes/Source/source/temp_13001/
# basedir: /Volumes/Source/source/git/mysql-5.6
# plugin_dir: /Volumes/Source/source/git/mysql-5.6/lib/plugin/
# general_log: OFF
# general_log_file:
# general_log_file_size:
# log_error:
# log_error_file_size:
# slow_query_log: OFF
# slow_query_log_file:
# slow_query_log_file_size:
# 1 row.
# #...done.

注意事项

  • 如果MySQL Server版本较旧(5.6.25或5.7.8之前)而my_print_defaults较新,那么MySQL Utilities无法访问.login-path文件中的密码,因为较新版本的my_print_defaults会“掩盖”了密码,但旧版本没有。

参考文献