1、下载redis源文件,解压并安装
安装gcc tcl依赖
yum install -y gcc tcl
安装并验证
tar zxvf redis-4.0.6.tar.gz
cd redis-4.0.6/
make PREFIX=/usr/local/redis install
make test
出现"\o/ All tests passed without errors!"说明安装成功
程序做软连接到bin目录,方便直接执行
ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli
ln -s /usr/local/redis/bin/redis-sentinel /usr/local/bin/redis-sentinel
ln -s /usr/local/redis/bin/redis-server /usr/local/bin/redis-server
2、新增相关目录
注意要以非root用户运行redis服务,所以对应目录也要修改相应的拥有者
新增目录
mkdir /var/lib/redis/
mkdir /var/log/redis/
mkdir /var/run/redis/
mkdir /etc/redis/
新增redis用户并设置密码
groupadd redis
useradd -g redis redis
passwd redis
修改对应目录的拥有者
chown -R redis.redis /var/lib/redis/
chown -R redis.redis /var/log/redis/
chown -R redis.redis /var/run/redis/
chown -R redis.redis /etc/redis/
3、启动redis服务
su redis
redis-server /etc/redis/redis.conf
redis-sentinel /etc/redis/sentinel.conf &
查看服务状态
redis-cli -p 6481 -a password
redis-cli -p 26481 -a password
Redis信息
127.0.0.1:6481> info
# Server
redis_version:4.0.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:2cb183298997fd87
redis_mode:standalone
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:15961
run_id:e1f64670ddb1c4dc330a722413642eff2089f04f
tcp_port:6481
uptime_in_seconds:427
uptime_in_days:0
hz:10
lru_clock:2392597
executable:/root/redis-server
config_file:/etc/redis/redis.conf
# Clients
connected_clients:8
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:34253264
used_memory_human:32.67M
used_memory_rss:9961472
used_memory_rss_human:9.50M
used_memory_peak:34355272
used_memory_peak_human:32.76M
used_memory_peak_perc:99.70%
used_memory_overhead:34209156
used_memory_startup:487088
used_memory_dataset:44108
used_memory_dataset_perc:0.13%
total_system_memory:16655790080
total_system_memory_human:15.51G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:0.29
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1562673258
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
# Stats
total_connections_received:7
total_commands_processed:2672
instantaneous_ops_per_sec:8
total_net_input_bytes:196135
total_net_output_bytes:901206
instantaneous_input_kbps:0.52
instantaneous_output_kbps:9.20
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:1
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
# Replication
role:slave
master_host:*.*.*.*
master_port:6481
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:106644
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9225f32503bde30c92f1245d356d6c33257910f0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:106644
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:33554432
repl_backlog_first_byte_offset:14574
repl_backlog_histlen:92071
# CPU
used_cpu_sys:0.28
used_cpu_user:0.16
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
# Cluster
cluster_enabled:0
# Keyspace
127.0.0.1:6481>
Sentinel信息
127.0.0.1:26481> info
# Server
redis_version:4.0.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:2cb183298997fd87
redis_mode:sentinel
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:15965
run_id:030eeb1df4074566839acf333be8acc9136fcbf5
tcp_port:26481
uptime_in_seconds:336
uptime_in_days:0
hz:13
lru_clock:2392510
executable:/root/redis-sentinel
config_file:/etc/redis/sentinel.conf
# Clients
connected_clients:3
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# CPU
used_cpu_sys:0.30
used_cpu_user:0.20
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
# Stats
total_connections_received:3
total_commands_processed:929
instantaneous_ops_per_sec:3
total_net_input_bytes:52722
total_net_output_bytes:5685
instantaneous_input_kbps:0.26
instantaneous_output_kbps:0.02
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6481,slaves=2,sentinels=3
4、附录:配置文件
Master的redis.conf
protected-mode yes
port 6481
tcp-backlog 511
unixsocket "/var/run/redis/redis_6481.sock"
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis/redis_6481.pid
loglevel notice
logfile "/var/log/redis/redis_6481.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir "/var/lib/redis/"
masterauth password
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-ping-slave-period 5
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 32mb
repl-backlog-ttl 3600
slave-priority 100
requirepass password
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
rename-command FLUSHALL ""
rename-command CONFIG ""
rename-command EVAL ""
slave的redis.conf
bind 0.0.0.0
protected-mode yes
port 6481
tcp-backlog 511
unixsocket "/var/run/redis/redis_6481.sock"
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis/redis_6481.pid
loglevel notice
logfile "/var/log/redis/redis_6481.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir "/var/lib/redis/"
slaveof *.*.*.* 6481
masterauth password
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-ping-slave-period 5
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 32mb
repl-backlog-ttl 3600
slave-priority 100
requirepass password
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
rename-command FLUSHALL ""
rename-command CONFIG ""
rename-command EVAL ""
sentinel.conf
protected-mode no
port 26481
dir "/var/lib/redis"
sentinel myid 8a77d457a1474f32be15540baf059fc81b5bda8f
sentinel monitor mymaster *.*.*.* 6481 2
sentinel auth-pass mymaster password
sentinel config-epoch mymaster 0
logfile "/var/log/redis/sentinel_26481.log"
sentinel leader-epoch mymaster 176
maxclients 4064
# 当存在内外网时,如果想要从外网访问,最好加个ip声明
# 否则sentinel返回给的IP可能是内网的,导致外网主机无法访问
sentinel known-slave mymaster *.*.*.1 6481
sentinel known-slave mymaster *.*.*.2 6481
sentinel announce-ip "*.*.*.*.*"