初衷:有时候我们想把另外一个数据库中的某张表同步到本地数据库中。1.mysqllogin-pathmysql如果在脚本中直接登录,会报shellwarning警告:在命令行界面使用密码可能不安全。这样做确实不安全。明文暴露mysql账号密码。因此,我们使用login-pathlogin-path是MySQL5.6支持的新特性。使用mysql_config_editor工具将登录MySQL服务的认证信息加密保存在.mylogin.cnf文件中(默认位于用户家目录下)。之后MySQL客户端工具可以读取加密文件连接MySQL,避免重复输入登录信息和敏感信息外泄。mysql_config_editor使用帮助:配置:mysql_config_editorset--login-path=test--user=test_user--host=127.0.0.1--port=3306--password例子:vagrant@homestead:~/code$mysql_config_editorset--login-path=xxx--host=127.0.0.1--port=3306--user=xxx--password输入密码:输入密码即可。它将被加密并保存在.mylogin.cnf文件中(默认位于用户的主目录中)。可配置项-h,--host=name在登录文件中添加host-G,--login-path=name在登录文件中添加登录路径name(默认为client)-p,--password在登录文件中添加密码登录文件(密码会被mysql_config_editor自动加密)-u,--user将用户名添加到登录文件中-S,--socket=name将sock文件路径添加到登录文件中-P,--port=name添加登录端口到登录文件显示配置:mysql_config_editorprint--login-path=test#显示执行的登录路径配置mysql_config_editorprint--all#显示所有登录路径信息删除配置:mysql_config_editorremove--login-path=test可以删除项目-h,--host=name添加主机到登录文件-G,--login-path=name添加登录文件中登录路径的名称(默认是client)-p,--password添加一个密码到登录文件(密码会被mysql_config_editor自动加密)-u,--user将用户名添加到登录文件-S,--socket=name将sock文件路径添加到登录文件-P,--port=name在登录文件中添加登录端口重置配置:mysql_config_editorreset--login-path=test使用login-path登录:shell>mysql--login-path=test登录其他主机和其他端口,或者添加其他附加参数,直接在上述命令后添加即可。shell>mysql--login-path=test-hhost1-Pport1#登录host1:poet1上的MySQLshell>mysql--login-path=test-hhost1-Pport1test_db#登录host1:poet1上的MySQL中的test_db库2、shell脚本#!/bin/bash#从源数据库同步到目标数据库.db_src=xxx//源数据库db_tar=xxx//目标数据库table_name=xxx//table##缓存目录configcach_dump=/home/vagrant/#缓存文件cache_file=$table_name.txt#loglog_dir=/var/log/syncSql/log_file=syncSql_$(date+"%Y-%m-%d").log#if[!-e$log_dir$log_file]然后如果[!-d$log_dir];然后sudomkdir$log_dir;elseexit&&echo-e"\n------------创建${log_dir}失败。------------\n"fiif[!-d$日志文件];然后cd$log_dir&&sudotouch$log_file;elseexit&&echo-e"\n------------创建${log_file}失败。------------\n"fisudochown-Rvagrant:vagrant$log_dirfi##定义执行sql函数sqlrun_src(){mysql--login-path=src<
