最近公司从阿里云准备迁移到亚马逊云,为此需要在亚马逊上搭建一些基础设施.
这篇文档仅是搭建redis服务器的步骤记录.
前提条件
- 系统版本 ubuntu14.04
- 安全组开放访问 6379端口
安装步骤
- 安装redis
sudo apt-get install redis-server
-
检测是否启动
- 检测进程
ps -ef | grep redis
可以看到:
redis 1977 1 0 05:51 ? 00:00:00 /usr/bin/redis-server 127.0.0.1:6379 ubuntu 1990 1364 0 05:53 pts/0 00:00:00 grep --color=auto redis
- 检测端口监听情况
netstat -nlt|grep 6379
结果如下:
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
- 还可以通过redis的命令行工具查看其状态
sudo /etc/init.d/redis-server status
输出结果为:
redis-server is running
修改配置
主要需要修改两个地方,一是绑定ip由本机改为所有,而是设置密码.为了步骤清晰,笔者将两者分开设置,建议读者全部阅读一遍后在一起修改配置文件.
- 修改绑定地址
从上面可以看到,安装后redis默认启动后监听127.0.0.1,也就是只可以本机访问.
现在来修改地址使其监听所有地址:-
vim 打开配置文件
sudo vim /etc/redis/redis.conf
-
找到以下位置:
# By default Redis listens for connections from all the network interfaces # available on the server. It is possible to listen to just one or multiple # interfaces using the "bind" configuration directive, followed by one or # more IP addresses. # # Examples: # # bind 192.168.1.100 10.0.0.1 bind 127.0.0.1
-
将
bind 127.0.0.1
注释,即在这一句前面加个#
,修改后如下:# By default Redis listens for connections from all the network interfaces # available on the server. It is possible to listen to just one or multiple # interfaces using the "bind" configuration directive, followed by one or # more IP addresses. # # Examples: # # bind 192.168.1.100 10.0.0.1 # bind 127.0.0.1
-
修改保存后,重启redis:
sudo /etc/init.d/redis-server restart
-
再来看一下端口监听情况:
netstat -nlt|grep 6379
可以看到,已经监听所有ip:
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN tcp6 0 0 :::6379 :::* LISTEN
-
- 设置密码
-
vim 打开配置文件
sudo vim /etc/redis/redis.conf
-
找到以下位置:
################################## SECURITY ################################### # Require clients to issue AUTH <PASSWORD> before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # # requirepass foobared # Command renaming.
-
将
# requirepass foobared
前的#去掉,requirepass foobared
里面的foobared
就是密码,您可以根据自自己的需要修改,修改后如下:################################## SECURITY ################################### # Require clients to issue AUTH <PASSWORD> before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # requirepass 你的密码 # Command renaming.
-
修改保存后,重启redis:
sudo /etc/init.d/redis-server restart
-
使用默认安装的命令行客户端进行测试:
redis-cli
这会是您进入redis 命令行,屏幕提示:
127.0.0.1:6379>
输入
keys *
:127.0.0.1:6379> keys * (error) NOAUTH Authentication required.
可以看到,是需要密码才能访问的.
现在,我们退出然后使用密码再连接一次:127.0.0.1:6379> exit
使用密码:
redis-cli -a 你的密码
输入
keys *
:127.0.0.1:6379> keys * (empty list or set)
由于我还没有存储任何东西,所以显示空集.但可以确定,使用密码后就可以访问了.
-