mysql安全之loginpath

2017-12-12  本文已影响12人  3c69b7c624d9

用了许久的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;

上一篇下一篇

猜你喜欢

热点阅读