假设redis安装在 IP 地址为 192.168.0.123 的linux服务器 .html
个人本机Win10操做系统 IP地址为 192.168.0.45 , 有一套java客户端代码可调用linux 上的redis服务.java
bind 192.168.0.123
bind 127.0.0.1
bind 192.168.0.123 127.0.0.1
奇葩的是,网上广为流传的是用bind 127.0.0.1 设置成只容许redis所在机器访问,然而在集群模式下,这个脑残设计有什么意义呢! 难道全部人都没有遇到A机器访问B机器redis的应用场景吗?linux
参考文章 http://blog.csdn.net/fxq8866/article/details/58238802 后,总结以下redis
方式一 | bind IP列表 | win10 java客户端问题 | linux 上redis-cli客户端问题 | 是否推荐 |
1 | bind 192.168.0.123 | 正常 | Connection refused | 不 |
2 | bind 127.0.0.1 | Connection refused | 正常 | 不 |
3 | bind 192.168.0.123 127.0.0.1 | 正常 | 正常 | 是 |
4 | bind 0.0.0.0 | 正常 | 正常 | 是 |
因此尽可能使用方式3或4来bind IP数据库
在网上四处摸索后以为惟一合理的作法,就是使用防火墙iptables,针对6379端口作IP拦截策略,没有其它.安全
首先安利三波 iptables详解 iptables用法简介 关于iptables添加规则不生效的问题服务器
因而配置如下三步:tcp
[root@localhost bin]#iptables -I INPUT -s 192.168.0.1/192.168.0.255 -p tcp --dport 6379 -j ACCEPT [root@localhost bin]#iptables -A INPUT -p tcp --dport 6379 -j REJECT [root@localhost bin]#service iptables save iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [肯定] [root@localhost bin]#service iptables status
部分参数解释 -I : 添加到头部 -A: 添加到尾部性能
由于规则是按从头至尾匹配优先级执行的,因此 REJECT要追加到尾部,从而让ACCEPT先匹配,否则会致使Connection 问题ui
redis设置访问密码有两种方式
requirepass mypassword
127.0.0.1:6379> CONFIG set requirepass "mypassword" 127.0.0.1:6379> CONFIG get requirepass
若是设置了主从模式,则须要在从数据库的配置文件中经过masterauth参数设置主数据库的密码
另外由于redis的强大性能,黑客能够每秒十几万的密码批量交互redis服务端以验证真实密码,因此,咱们的redis密码尽可能设置得又臭又长点吧.
禁用CONFIG, flushall ,flushdb 这三个重要命令
rename-command CONFIG "" rename-command flushall "" rename-command flushdb ""
su -m nobody -c xxx