最近公司從阿里云準(zhǔn)備遷移到亞馬遜云,為此需要在亞馬遜上搭建一些基礎(chǔ)設(shè)施.
這篇文檔僅是搭建redis服務(wù)器的步驟記錄.
前提條件
- 系統(tǒng)版本 ubuntu14.04
- 安全組開放訪問 6379端口
安裝步驟
- 安裝redis
sudo apt-get install redis-server
-
檢測(cè)是否啟動(dòng)
- 檢測(cè)進(jìn)程
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
- 檢測(cè)端口監(jiān)聽情況
netstat -nlt|grep 6379
結(jié)果如下:
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
- 還可以通過redis的命令行工具查看其狀態(tài)
sudo /etc/init.d/redis-server status
輸出結(jié)果為:
redis-server is running
修改配置
主要需要修改兩個(gè)地方,一是綁定ip由本機(jī)改為所有,而是設(shè)置密碼.為了步驟清晰,筆者將兩者分開設(shè)置,建議讀者全部閱讀一遍后在一起修改配置文件.
- 修改綁定地址
從上面可以看到,安裝后redis默認(rèn)啟動(dòng)后監(jiān)聽127.0.0.1,也就是只可以本機(jī)訪問.
現(xiàn)在來(lái)修改地址使其監(jiān)聽所有地址:-
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
注釋,即在這一句前面加個(gè)#
,修改后如下:# 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
-
再來(lái)看一下端口監(jiān)聽情況:
netstat -nlt|grep 6379
可以看到,已經(jīng)監(jiān)聽所有ip:
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN tcp6 0 0 :::6379 :::* LISTEN
-
- 設(shè)置密碼
-
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
就是密碼,您可以根據(jù)自自己的需要修改,修改后如下:################################## 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
-
使用默認(rèn)安裝的命令行客戶端進(jìn)行測(cè)試:
redis-cli
這會(huì)是您進(jìn)入redis 命令行,屏幕提示:
127.0.0.1:6379>
輸入
keys *
:127.0.0.1:6379> keys * (error) NOAUTH Authentication required.
可以看到,是需要密碼才能訪問的.
現(xiàn)在,我們退出然后使用密碼再連接一次:127.0.0.1:6379> exit
使用密碼:
redis-cli -a 你的密碼
輸入
keys *
:127.0.0.1:6379> keys * (empty list or set)
由于我還沒有存儲(chǔ)任何東西,所以顯示空集.但可以確定,使用密碼后就可以訪問了.
-