系統:CentOS7
Redis:5.0.5
獲取最新版本redis:https://redis.io/download ,下載Stable版本,當前安裝版本5.0.5
一、安裝Redis
1、下載redis安裝包(安裝目錄/usr/local/)
[root@iZbp12y6fwj9mup08bgko6Z ~]# cd /usr/local/ [root@iZbp12y6fwj9mup08bgko6Z local]# wget http://download.redis.io/releases/redis-5.0.5.tar.gz
2、解壓redis-5.0.5.tar.gz
[root@iZbp12y6fwj9mup08bgko6Z local]# tar -xzf redis-5.0.5.tar.gz
3、進入解壓的redis目錄,通過make命令進行編譯
[root@iZbp12y6fwj9mup08bgko6Z local]# cd redis-5.0.5 [root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# make
4、執行make test驗證編譯是否成功(可能會報錯,但是不要怕一步一步解決錯誤,直至成功)
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# make test cd src && make test make[1]: Entering directory `/usr/local/redis-5.0.5/src' CC Makefile.dep make[1]: Leaving directory `/usr/local/redis-5.0.5/src' make[1]: Entering directory `/usr/local/redis-5.0.5/src' You need tcl 8.5 or newer in order to run the Redis test make[1]: *** [test] Error 1 make[1]: Leaving directory `/usr/local/redis-5.0.5/src' make: *** [test] Error 2
###### 很不巧,我的編譯錯誤了,根據錯誤提示是tcl的問題,所有我要安裝一下tcl,你需要根據你的結果信息來處理
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# yum install -y tcl ...... Install 1 Package Total download size: 1.9 M Installed size: 4.4 M Downloading packages: tcl-8.5.13-8.el7.x86_64.rpm | 1.9 MB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 1:tcl-8.5.13-8.el7.x86_64 1/1 Verifying : 1:tcl-8.5.13-8.el7.x86_64 1/1 Installed: tcl.x86_64 1:8.5.13-8.el7 Complete! [root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]#
###### tcl安裝完成后重新運行make test
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# make test ...... !!! WARNING The following tests failed: *** [err]: Active defrag big keys in tests/unit/memefficiency.tcl Expected condition '$max_latency <= 120' to be true (258 <= 120) Cleanup: may take some time... OK make[1]: *** [test] Error 1 make[1]: Leaving directory `/usr/local/redis-5.0.5/src' make: *** [test] Error 2
######此錯誤是由於所測試的阿里雲ECS服務器配置過低造成的延遲太大,可以忽略。
忽略警告,繼續安裝
6、運行make install命令,將命令安裝到/usr/local/bin目錄
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# make install cd src && make install make[1]: Entering directory `/usr/local/redis-5.0.5/src' Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: Leaving directory `/usr/local/redis-5.0.5/src' [root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]#
7、啟動服務器
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# redis-server
8、另開一個命令窗口,進行測試,可以看到通過redis-cli命令連接redis之后,輸入ping,redis會為我們返回PONG
[root@iZbp12y6fwj9mup08bgko6Z ~]# redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
二、設置redis
1、復制redis配置文件,將redis目錄的redis.conf復制到/etc/redis目錄下
root@iZbp12y6fwj9mup08bgko6Z ~]# find / -name redis.conf /usr/local/redis-5.0.5/redis.conf [root@iZbp12y6fwj9mup08bgko6Z ~]# mkdir /etc/redis [root@iZbp12y6fwj9mup08bgko6Z ~]# cp /usr/local/redis-5.0.5/redis.conf /etc/redis/redis.conf
2、設置可以遠程登錄,編輯/etc/redis/redis.conf配置文件,注釋掉bind 127.0.0.1,如下圖:
3、修改默認端口6379,編輯/etc/redis/redis.conf配置文件,如下圖:
同時修改pidfile,將其改為pidfile /var/run/redis_6379.pid,修改目的是為了文件名上的端口和實際端口保持一致,方便通過服務方式啟動、停止,如后面設置開機啟動
4、設置redis服務后台運行,編輯/etc/redis/redis.conf配置文件,將daemonize設置為yes,如下圖
5、設置訪問密碼,編輯/etc/redis/redis.conf配置文件,去掉requirepass行的注釋或添加一行,如下圖
6、啟動redis並指定配置文件為我們剛才修改的/etc/redis/redis.conf配置文件,服務啟動后就不像前面那樣輸出redis圖形信息的內容了。
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# redis-server /etc/redis/redis.conf 21262:C 27 Jun 2020 17:21:47.055 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 21262:C 27 Jun 2020 17:21:47.055 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=21262, just started 21262:C 27 Jun 2020 17:21:47.055 # Configuration loaded [root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]#
7、新開一窗口測試鏈接
[root@iZbp12y6fwj9mup08bgko6Z ~]# redis-cli 127.0.0.1:6379> ping (error) NOAUTH Authentication required. 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
也可以通過-p指定端口訪問
[root@iZbp12y6fwj9mup08bgko6Z ~]# redis-cli -p 6379 127.0.0.1:6379> ping (error) NOAUTH Authentication required. 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
由於開啟了遠程訪問,所以在其他電腦上也可以訪問,通過-h指定ip,-p指定端口:
[iypocket@smac src] ./redis-cli -p 6380 -h 192.168.16.125 192.168.16.125:6379> ping (error) NOAUTH Authentication required. 192.168.16.125:6379> auth 123456 OK 192.168.16.125:6379> ping PONG 192.168.16.125:6379>
8、停止服務
三、設置redis開機啟動
1、復制開機啟動腳本,在redis目錄的utils包下,有一個redis_init_script文件,我們復制到/etc/init.d/目錄下,並將文件名改為redis。
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# find / -name redis_init_script /usr/local/redis-5.0.5/utils/redis_init_script [root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# cp /usr/local/redis-5.0.5/utils/redis_init_script /etc/init.d/redis
2、(非必須)修改/etc/redis/redis.conf文件名為/etc/redis/6379.conf,此修改非必須,不修改的話可以在步驟3指定配置文件路徑
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# mv /etc/redis/redis.conf /etc/redis/6379.conf
3、編輯/etc/init.d/redis啟動腳本,腳本說明如下:
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# vim /etc/init.d/redis
說明
4、測試啟動腳本,啟動服務systemctl start redis,停止服務systemctl stop redis
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# systemctl start redis [[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# systemctl stop redis
5、設置開機啟動
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# chkconfig --add redis
6、重啟系統,redis服務即隨系統啟動。
[root@iZbp12y6fwj9mup08bgko6Z redis-5.0.5]# reboot