環境
【阿里雲centos7.6操作系統;yum方式;redis-3.2.12; firewalld防火牆默認關閉;需要在安全組開啓相關端口6379】
【用yum方式安裝的redis版本比較老都是3.2.12,如果需要高版本的需要用irpm源或者源碼安裝
】
查看redis是否正常啓動必須用命令systemctl status redis.server查看,如果啓動失敗用命令netstat -anpt|grep redis查看時也會顯示有redis正在運行。
注意:配置文件的註釋需要放在單獨的一行,不可以放在命令行後面
systemctl status firewalld.service查看防火牆狀態
# uname -a
Linux iZ8vb0ywcounjc3oh3rv2jZ 3.10.0-862.14.4.el7.x86_64
#1 SMP Wed Sep 26 15:12:11 UTC 2018
x86_64 x86_64 x86_64 GNU/Linux
Linux - 內核名
audit - 主機名
2.6.18-128.el5 - 內核版本
#1 SMP Wed Jan 21 10:41:14 ....2009 - 內核編譯日期
x86_64 - 操作系統版本
x86_64 - 處理器類型
x86_64 - 硬件平臺
(以上3 個可能順序不對,都是一樣的不要區別,x86_64表示64位的)
GNU/Linux - 操作系統
# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core
注意:
需要開放安全組端口6379
登陸方式:redis-cli -h 主機ip -p 端口
日誌路徑:/var/log/redis/redis.log
- 設置Redis的倉庫地址
# yum install epel-release -y
- 安裝redis
# yum -y install redis
默認安裝目錄:配置文件在/etc/redis.conf;服務器和客戶端在 /usr/bin/redis-server redis-cli
whereis redis //可以查看配置文件在哪
# whereis redis
redis: /etc/redis.conf
- 修改配置文件vim /etc/redis.conf
#監聽所有的IP地址(61行)
bind 127.0.0.1註釋掉爲#bind 127.0.0.1
#設置爲守護進程(128行)
daemonize no 改爲 daemonize yes
#設置密碼(480行)
requirepass 密碼也可以在命令界面設置密碼:
redis 127.0.0.1:6379> config set requirepass 密碼
#最大物理內存設置(537行)在真實環境必須部署,否則物理內存會被耗盡。一般配置200mb/500mb/1gb/2gb。配置了最大內存 maxmemory 之後記得配置過期刪除策略
maxmemory 200mb
#過期刪除策略volatile-lru -> (542行)根據LRU算法生成的過期時間來刪除。
# allkeys-lru -> (543行)根據LRU算法刪除任何key。
# volatile-random -> (544行)根據過期設置來隨機刪除key。
# allkeys->random ->(545行) 無差別隨機刪。
# volatile-ttl ->(546行) 根據最近過期時間來刪除(輔以TTL) # noeviction -> 誰也不刪,直接在寫操作時返回錯誤。
maxmemory-policy(560行)
volatile-lru
設置日誌的屬組屬主爲redis,啓動redis後纔會自動出現redis.log文件,所以需要先創建
#touch /var/log/redis/redis.log
#chown redis.redis /var/log/redis/redis.log
- 啓動redis
#service redis start (這個不好使,這是centos6裏的命令centos7裏不適用)
或者指定配置文件啓動redis-server /etc/redis.conf
本機redis啓動命令
#systemctl restart redis
關閉redis
#systemctl stop redis
- 設置開機自動啓動
設置爲開機自啓
# systemctl enable redis.service
查看redis的自啓動狀態
# systemctl list-unit-files |grep redis
redis-sentinel.service disabled
redis.service enabled
這條命令是redis集羣才用的單獨的redis是不需要的,在這個項目中,不需要只爲以後做準備。
# systemctl enable redis.service redis-sentinel.service
Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel.service to /usr/lib/systemd/system/redis-sentinel.service.
# systemctl list-unit-files |grep redis
redis-sentinel.service enabled
redis.service enabled
# vim /usr/lib/systemd/system/redis.service
[Service] Type=forking //不加這句回一直重啓【不好使,這條以後不用,廢棄掉,留在此處只爲提個醒】
- 查看版本
# redis-server -v
Redis server v=3.2.12 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=7897e7d0e13773f
- 測試啓動redis-cli
Auth 密碼
ping 返回PONG,啓動成功
- redis-cli 啓動redis客戶端
redis-cli -h 主機ip -p 端口
或者
redis-cli,之後輸入auth+空格+密碼