从mysql5.6开始,mysql推出了加密工具mysql_config_editor。在此之前我们通过将账号和密码明文放入my.cnf,从而使用mysql客户端登录时,无需指定账号密码就可以登录数据库。而有了mysql_config_editor工具之后,我们将加密后的账号密码放入二进制文件。在登录时,客户端通过解密该文件来登录数据库。由于加密解密都在内存中进行,所以无法明文的显示文件内容。只要我们将文件权限保存好,就可以防止不怀好意的人解密我们的数据库密码了.mysql_config_editor的使用过程如下:
mysql_config_editor set --login-path=client --host=localhost --user=localuser --password这样我们就配置了一个为本地的数据源信息:
login-path :指定通过mysql客户端登录时的标识host:我们要连接的数据库user: 通过本地连接数据库时,使用的账号password:指定通过本地连接时,使用的数据库密码(这里假设输入的密码为password1)当然,如果通过远程连接,我们可能还要加上特定的端口信息。这样,当我们登录数据库时,只需要如下命令就可以连接到该数据库了:mysql —login-path=client这样我们就连接到本地数据库了。
下面我们来看看mysql_config_editor的细节部分:由于该工具包含set/remove/print/reset/help,所以我们仅分析set功能的实现:set功能是通过set_command函数实现的,该函数主要用于配置账号密码等数据源信息,并将该信息存储到二进制文件:点击(此处)折叠或打开static int set_command(void){
DBUG_ENTER("set_command");DYNAMIC_STRING file_buf, path_buf;
init_dynamic_string(&path_buf, "", MY_LINE_MAX, MY_LINE_MAX);
init_dynamic_string(&file_buf, "", file_size, 3 * MY_LINE_MAX);if (tty_password)
opt_password= get_tty_password(NullS);
if (file_size)
{
if (read_and_decrypt_file(&file_buf) == -1) //如果文件存在,就读取文件,并将文件的密文解密后存放到file_buf中.
goto error;
}dynstr_append(&path_buf, "["); /* --login=path */
if (opt_login_path)
dynstr_append(&path_buf, opt_login_path);
else
dynstr_append(&path_buf, "client");
dynstr_append(&path_buf, "]");if (opt_user) /* --user */
{
dynstr_append(&path_buf, "
user = ");
dynstr_append(&path_buf, opt_user);
}if (opt_password) /* --password */
{
dynstr_append(&path_buf, "
password = ");
dynstr_append(&path_buf, opt_password);
}if (opt_host) /* --host */
{
dynstr_append(&path_buf, "
host = ");
dynstr_append(&path_buf, opt_host);
}if (opt_socket)
{
dynstr_append(&path_buf, "
socket = ");
dynstr_append(&path_buf, opt_socket);
}if (opt_port)
{
dynstr_append(&path_buf, "
port = ");
dynstr_append(&path_buf, opt_port);
}dynstr_append(&path_buf, "
");/* Warn if login path already exists */
if (opt_warn && ((locate_login_path (&file_buf, opt_login_path)) //判断该login-path是否已经存在
!= NULL))
{
int choice;
printf ("WARNING : "%s" path already exists and will be "
"overwritten.
Continue? (Press y|Y for Yes, any "
"other key for No) : ",
opt_login_path);
choice= getchar();if (choice != (int) "y" && choice != (int) "Y’) //如果login-path存在是否选择覆盖
goto done; /* skip */
}/* Remove the login path. */
remove_login_path(&file_buf, opt_login_path); //从原来文件中读取的内容中,删掉该login-path信息/* Append the new login path to the file buffer. */
dynstr_append(&file_buf, path_buf.str); //将该login-path的信息加到file_buf的末尾if (encrypt_and_write_file(&file_buf) == -1) //将包含新的log-path的所有信息和原来的信息加密写入文件
goto error;done:
dynstr_free(&file_buf);
dynstr_free(&path_buf);
DBUG_RETURN(0);error:
dynstr_free(&file_buf);
dynstr_free(&path_buf);
DBUG_RETURN(-1);
}代码的具体逻辑如下:在这里我们重点看看其中涉及的几个重要的函数:read_and_decrypt_file (读取文件内容,并解密后放到动态字符缓冲中)locate_login_path(判断该login-path是否已经存在)remove_login_path(如果login-path存在,则删除该login-path)dynstr_append(&file_buf, path_buf.str); 将新的login-path添加到file_buf 末尾encrypt_and_write_file(&file_buf) 将file_buf中的信息解码后写入到文件中首先我们来看看加密后的文件格式如下:这里我们假设之前已经存在加密的文件了.
更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2016-02/128363p2.htm
mysqldump注意问题一则MySQL新特性之mysql_config_editor 加密算法与解密实现相关资讯 MySQL新特性 mysql_config_editor
- MySQL新特性之mysql_config_editor (02月14日)
本文评论 查看全部评论 (0)