一、安裝
1.檢查gcc環境
執行命令,如果Linux系統沒有安裝gcc編譯器,會提示“Command not found”
# gcc -v
安裝gcc
# yum -y install gcc
以上是make需要的,不裝會報錯!
2.下載Redis
# cd /usr/local
# wget http://download.redis.io/releases/redis-4.0.12.tar.gz
3.解壓
# tar xzf redis-4.0.12.tar.gz
4.make編譯
# cd redis-4.0.12
# make
5.make install初始化
# make install
6.安裝Redis
運行make test測試
# cd src #進入src目錄
# make test #執行測試

# make install

安裝成功
7.啟動
# ./src/redis-server
該啟動方式在控制台退出的時候就關閉了,不建議使用。
建議修改好配置的daemonize為yes后,使用命令redis-server redis.conf應用改配 置文件啟動redis
二、配置
修改redis.conf配置文件
# vi redis.conf
1.設置都可訪問
#bind 127.0.0.1
如果設置為127.0.0.1,則表示僅本機可訪問,注銷這句表示全部可以訪問,這里我們注釋掉。想配置內網訪問,或者限定IP,一般使用雲服務器安全組策略來配置實現。
2.解除外網訪問的限制
protected-mode no
protected-mode參數是為了禁止外網訪問redis,如果啟用了,則只能夠通過lookback ip(127.0.0.1)訪問Redis
3.以后端模式啟動
daemonize yes
4.配置log日志路徑
logfile "/usr/local/redis/redis-4.0.12/logs/redis.log" # 建議先創建好redis.log文件,再來配置
5.禁用持久化(如果僅僅需要緩存,禁掉持久化配置)
#save 900 1
#save 300 10
#save 60 10000
全部注釋
6.配置最大內存
maxmemory 2147483648 # 后面跟的是Bytes,這里表示2G
7.內存淘汰策略
maxmemory-policy volatile-lru
redis提供了下面幾種淘汰策略供用戶選擇,其中默認的策略為noeviction策略:
noeviction:當內存使用達到閾值的時候,所有引起申請內存的命令會報錯。
allkeys-lru:在主鍵空間中,優先移除最近未使用的key。
volatile-lru:在設置了過期時間的鍵空間中,優先移除最近未使用的key。
allkeys-random:在主鍵空間中,隨機移除某個key。
volatile-random:在設置了過期時間的鍵空間中,隨機移除某個key。
volatile-ttl:在設置了過期時間的鍵空間中,具有更早過期時間的key優先移除。
8.設置密碼
requirepass foobared
三、應用配置啟動redis
1.啟動redis
redis-server redis.conf
ps -ef|grep redis # 查看redis進程
2.關閉redis
kill - 9 pid # pid通過查看而來
3.遠程連接redis
redis-cli -h ip地址 -p 端口號 -a 密碼
四、解決啟動后日志提醒的三個警告
第一個警告:The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
第二個警告:overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to/etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
第三個警告:you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix thisissue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain thesetting after a reboot. Redis must be restarted after THP is disabled.
解決方案:
第一個警告:
將net.core.somaxconn = 1024添加到/etc/sysctl.conf中,然后執行sysctl -p 生效配置。
第二個警告:
將vm.overcommit_memory = 1添加到/etc/sysctl.conf中,然后執行sysctl -p生效配置。
第三個警告:
將echo never > /sys/kernel/mm/transparent_hugepage/enabled添加到/etc/rc.local中,然后執行source /etc/rc.local生效配置。