- 系統版本:CentOS Linux release 7.6.1810
- Redis版本:redis-4.0.6
- 檔案下載目錄:/usr/local/src/
- Redis安裝目錄:/usr/local/redis/
準備工作
yum安裝gcc依賴
[root@localhost redis]# yum install gcc
獲取安裝檔案
[root@localhost redis]# cd /usr/local/src/
[root@localhost redis]# wget http://download.redis.io/releases/redis-4.0.6.tar.gz
[root@localhost redis]# tar -zxvf redis-4.0.6.tar.gz
安裝
[root@localhost redis]# mv /usr/local/src/redis-4.0.6 /usr/local/redis
[root@localhost redis]# cd /usr/local/redis-4.0.6/
[root@localhost redis]# make MALLOC=libc
[root@localhost redis]# make install
cd src && make install
make[1]: Entering directory `/usr/local/redis/src'
CC Makefile.dep
make[1]: Leaving directory `/usr/local/redis/src'
make[1]: Entering directory `/usr/local/redis/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/src'
[root@localhost redis]# redis-cli -v
redis-cli 4.0.6 # 安裝成功
啟動
守護程式(可選)
[root@localhost redis]# vim /usr/local/redis/redis.conf
#daemonize no
daemonize yes # 開啟守護程式模式
啟動服務
[root@localhost redis]# redis-server
開機啟動
新增配置檔案
[root@localhost redis]# mkdir /etc/redis # 建立目錄
[root@localhost redis]# cp /usr/local/redis/redis.conf /etc/redis/6379.conf # 複製配置檔案
[root@localhost redis]# cp /usr/local/redis/utils/redis_init_script /etc/init.d/redisd # 複製啟動檔案
修改啟動配置
- 增加註釋資訊
- 如不增加註釋,chkconfig可能會提示服務不支援
[root@localhost init.d]# vim /etc/init.d/redisd
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO # 新增資訊從此開始
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO # 新增資訊從此結束
REDISPORT=6379 # 如埠號修改,此處相應修改
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
開機啟動
[root@localhost redis]# chkconfig redisd on # 設定開機啟動
服務管理
[root@localhost redis]# service redisd start # 啟動服務
[root@localhost redis]# service redisd stop # 關閉服務
常見問題
/var/run/redis_6379.pid exists, process is already running or crashed
- 使用手動指定配置檔案的方式啟動後,再使用service redisd stop停止服務
- 然後再次service redisd start啟動
[root@localhost redis]# redis-server
[root@localhost redis]# service redisd stop
[root@localhost redis]# service redisd start
- 一臺機器模擬主從
- master使用6379埠
- slave使用6380埠
配置
拷貝slave配置檔案
[root@localhost redis]# cp /usr/local/redis/redis.conf /usr/local/redis/redis.6380.conf
master設定
[root@localhost redis]# vim /etc/redis/redis.conf
bind 0.0.0.0 # 繫結允許訪問的ip地址,如本機模擬主從可不改變值仍使用127.0.0.1
slave設定
[root@localhost redis]# vim /usr/local/redis/redis.6380.conf
port 6380 # 將埠更改為6380
slaveof 127.0.0.1 6379 # 指定master ip port
驗證
啟動服務
[root@localhost redis]# redis-server /usr/local/redis/redis.conf # 啟動master
[root@localhost redis]# redis-server /usr/local/redis/redis.6380.conf # 啟動slave
檢視Master狀態
[liubo@localhost ~]$ redis-cli -p 6379
127.0.0.1:6379> info
# Server
……
# Clients
……
# Memory
……
# Persistence
……
# Stats
……
# Replication # 關注此區域
role:master # 當前角色,master
connected_slaves:1 # 已連線slave:1個
slave0:ip=127.0.0.1,port=6380,state=online,offset=56,lag=1 # slave0資訊
master_replid:7e3b0e1accd31abaf58177160685d51952ea1e90
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56
# CPU
……
# Cluster
……
檢視Slave狀態
[liubo@localhost ~]$ redis-cli -p 6380
127.0.0.1:6379> info
# Server
……
# Clients
……
# Memory
……
# Persistence
……
# Stats
……
# Replication # 關注此區域
role:slave # 當前角色,slave
master_host:127.0.0.1 # master相關資訊
master_port:6379
master_link_status:up # 連線master狀態
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:84
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7e3b0e1accd31abaf58177160685d51952ea1e90
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:84
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84
# CPU
……
# Cluster
……
常見問題
slave info 顯示 master_link_status:down
- vim /usr/local/redis/redis.6380.conf
daemonize no # 關閉後臺執行,方便檢視報錯資訊
# daemonize yes