[技巧]如何防止Linux命令行下MySQL登录密码泄露?

命令行登录防止MySQL密码泄露的几个小妙招。

明知山有虎偏向虎山行的方案:

1、可以通过如下环境变量强制Linux不记录敏感历史命令

在命令行执行HISTCONTROL=ignorespace后,再输入带密码的命令的前面加一个空格登录,登录命令不会被记录到历史记录里。

[root@yunweipai~]# HISTCONTROL=ignorespace

#<==这里是临时生效,要想永久生效,请放入/etc/bashrc。

[root@yunweipai~]#  mysql -uroot -p'yunweipai123'

#<==命令的开头要多一个空格。

2、操作完敏感的命令后可以及时删除命令行记录

执行“history -d 历史命令序号” 清除指定历史记录命令

[root@yunweipai~]# history|tail -4

#<==显示历史记录。

252  mysql -uroot -p'yunweipai123'

#<==此条带密码,敏感,待删除。

253 pwd

254 history

255 history|tail -4

[root@yunweipai~]# history -d 252

#<==删除序号为252的历史记录。

[root@yunweipai~]# history|tail -5

252 pwd

#<==序号252对应的带密码登录的命令已经消失。

253 history

254 history|tail -4

255 history -d 252

256 history|tail -5

执行“history -c”清除所有所有记录

[root@yunweipai~]# history -c [root@yunweipai~]# history

1 history

执行“>~/.bash_history”清除历史记录文件

3、给带密码的启动脚本以及备份脚本等加700权限,用户和组改为root。

chmod700 /data/3306/mysql

#<==可以采用kill信号的关闭方式数据库,从而防止密码泄露。

chmod700 /server/scripts/bak.sh

#<==将密码写入my.cnf配置文件,使得执行备份命令不需要加密码。

4、把密码写入my.cnf配置文件并加700权限,用户和组改为mysql。

[root@yunweipai~]# cp /application/mysql/my.cnf /etc/ [root@yunweipai~]# grep -A 2 client /etc/my.cnf

#<==配置文件开头添加如下三行,无需重启系统。

[client]

#<==客户端模块标签。

user=root

#<==用户参数及密码。

password=yunweipai123

#<==密码参数及密码。

[root@yunweipai~]# mysql

#<==此时登录数据库就不用输入密码了。

Welcometo the MySQL monitor.  Commands end with; or \g.

YourMySQL connection id is 8

Serverversion: 5.6.34 Source distribution

...省略若干行...

Type'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

知道山上有老虎,就不去的的方法:

[root@yunweipai~]# mysql -uroot -p

#<==这里标准dba命令行登陆命令,交互式输入密码可有效防止密码泄露。

Enter password:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值