Redis 安裝手冊
1 解壓
壓縮包放在/root/download目錄下
# tar -zxf redis-5.0.5.tar.gz
2 編譯
2.1編譯
# cd redis-5.0.5
# make MALLOC=jemalloc V=1
上面make命令執行的時候,存在CenterOS7中出現瞭如下異常,使用另一條命令執行(# make MALLOC=libc V=1)即可:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/data0/src/redis-2.6.2/src'
make: *** [all] Error 2
如果有提示gcc command not found,請安裝gcc依賴
# yum install gcc
2.2驗證(生產必須)
# make test
如果不能執行,且提示要安裝tcl(包已在運維目錄下),請執行下面命令後重試
# sudo tar -xzvf tcl8.6.1-src.tar.gz -C /usr/local/
# cd /usr/local/tcl8.6.1/unix/
# sudo ./configure
# sudo make
# sudo make install
安裝完畢後,再次回到redis的解壓目錄中,執行make test命令,等待執行結果,截圖爲驗證成功示例
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-wtqAKLnl-1580975214518)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\image-20191229103836280.png)]
注:構建時選擇默認的內存分配器
# make MALLOC=libc // 默認的
# make MALLOC=jemalloc // Linux中使用,因爲它比默認的少很多內存碎片
注:默認情況下,Redis將構建一個用戶友好的彩色輸出。如果希望看到更詳細的輸出,請使用以下命令
# make V=1
注:反編譯(完全刪除編譯內容)
# make distclean
3 安裝
# make PREFIX=/usr/local/redis install
# vi /etc/profile
在文件底部加入:
// 注:如果服務器已經安裝了其他組件,那麼PATH之間用英文的冒號”:”隔開:
// 例:export PATH=$PATH:$JAVA_HOME/bin:$REDIS_HOME/bin
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin
保存退出:wq,啓用環境變量
# source /etc/profile
// 驗證
# echo $PATH
4 構建
# cd utils // redis解壓目錄下
# ./install_server.sh // 該命令可以執行多次,會安裝多個實例,但生產只裝一次
設定目錄:
內容 | 文件位置及文件名 |
---|---|
配置 | /etc/redis/6379.conf |
日誌 | /var/redis/log/redis_6379.log |
數據 | /var/redis/data/6379 |
執行文件 | /usr/local/redis/bin/redis-server |
5 服務操作命令
# service redis_6379 stop
# service redis_6379 start
# service redis_6379 restart
# service redis_6379 status
6 開機自啓
# chkconfig redis_6379 on
# chkconfig --list //查看所有註冊的腳本文件
驗證,重啓服務器
# reboot
重新連接服務器後輸入以下命令:
# service redis_6379 status
看到下面的內容表示成功,如果有其他多餘的警告或錯誤提示,請重新安裝
Redis is running (1637)
7開放端口
所有redis服務器都要確保開放了6379端口:
/etc/rc.d/init.d/iptables status
如果沒有iptables,就安裝一下,一般是CenterOS7中默認開的是firewall
# 停止firewall
systemctl stop firewalld.service
# 禁止firewall開機啓動
systemctl disable firewalld.service
# 安裝iptables
yum install iptables-services
# 開機啓動
systemctl enable iptables.service
沒有開放端口就配置
CenterOS6中:
# /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
# /etc/rc.d/init.d/iptables save
# /etc/rc.d/init.d/iptables restart
CenterOS7中:
# vi /etc/sysconfig/iptables
添加以下內容:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
重啓應用配置
# systemctl restart iptables.service
7 修改配置
# vi /etc/redis/6379.conf
找到下列對應配置的位置,並進行修改:
1 關閉保護模式,本服務僅對內網服務,不需要該配置
bind 0.0.0.0
protected-mode no
2 RDB配置
save 900 1 // 15分鐘有1條key變更就記錄
save 300 10 // 5分鐘有10條key變更就記錄
save 60 60 // 1分鐘有60條key變更就記錄
3 設置訪問密碼
requirepass [訪問密碼] // 需要同步修改APP配置文件中的redis密碼
4 開啓AOF
appendonly yes
5 設置AOF爲每秒記錄
# appendfsync always
appendfsync everysec
# appendfsync no