mysql安全之loginpath


声明:本文转载自https://my.oschina.net/qixiaobo025/blog/1486064,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

用了许久的mysql,最近发现了一个新玩具。

对于兼运维的开发同学来说,需要备份数据库。我们一般通过crontab来实现。

执行

crontab -e
30 1 * * * /data/shell/backupdb.sh;

我们在凌晨1:30执行备份数据库操作

脚本如下

/usr/local/mysql/bin/mysqldump -uroot -pXXXX --skip-lock-tables --databases f6dms_trial $(mysql -uroot -pXXXX -Df6dms_trial -Bse "show tables like 'tm_monitor_avg_price_%'"|awk '{print "--ignore-table=f6dms_trial."$1}'|xargs)| gzip > /data/backup/f6dms-trial_`date '+%Y-%m-%d-%H:%M:%S'`.sql.gz; /usr/local/mysql/bin/mysqldump -uroot -pXXXX --skip-lock-tables --databases f6db_trial f6report_new_trial | gzip > /data/backup/f6db-trial_`date '+%Y-%m-%d-%H:%M:%S'`.sql.gz; 

这个关于忽略指定数据表(不定个数)的可以参考mysqldump之跳过指定表

我们这边的显式写上了db的用户名密码,如果该脚本我们是通过svn管理的,那么很容易出现db的用户名密码泄露,导致出现一些安全隐患。

mysql在5.6之后提供了loginpath功能。

loginpath的官方介绍如下

The best way to specify server connection information is with your .mylogin.cnf file. Not only is this file encrypted, but any logging of the utility execution does not expose the connection information. Thus, no user names, passwords, ports, etc. are visible in the log. This is the preferred method for using MySQL Utilities to connect to servers.

Utilities support the use of login-paths in the connection string provided they use the following format login-path-name[:port][:socket] where the port and socket parameters are optional. If used, these optional parameters override the respective options from the specified login-path file.

When using login-paths, there are no default values except on Posix systems when specifying a socket. In this case, the host option defaults to localhost on port 3306. This means that combining the values specified in the login-path with the two optional values port and socket, one needs to specify at least a user, a hostname and a port or socket.

Use the mysql_config_editor tool (http://dev.mysql.com/doc/en/mysql-config-editor.html) to add the connection information as follows.

shell> mysql_config_editor set --login-path=instance_13001 --host=localhost --user=root --port=13001 --password Enter password: <Password is prompted to be inserted in a more secure way>

我们可以如下执行

mysql_config_editor set --login-path=test --user=root --host=localhost --password Enter password: (输入密码) mysql --login-path=test

这样就可以登录成功。

对应会生成.mylogin.cnf 文件

打开后,基本是编码过后的内容,较为安全

这样我们也不需要再显示的写用户名密码了,系统的安全进一步得到提升。

新的代码如下

/usr/local/mysql/bin/mysqldump --login-path=test --skip-lock-tables --databases f6dms_trial $(mysql --login-path=test -Df6dms_trial -Bse "show tables like 'tm_monitor_avg_price_%'"|awk '{print "--ignore-table=f6dms_trial."$1}'|xargs)| gzip > /data/backup/f6dms-trial_`date '+%Y-%m-%d-%H:%M:%S'`.sql.gz; /usr/local/mysql/bin/mysqldump --login-path=test --skip-lock-tables --databases f6db_trial f6report_new_trial | gzip > /data/backup/f6db-trial_`date '+%Y-%m-%d-%H:%M:%S'`.sql.gz; 

 

本文发表于2017年07月25日 02:36
(c)注:本文转载自https://my.oschina.net/qixiaobo025/blog/1486064,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 3609 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1