Redis标准安装
1. 先决条件
[root@redis ~]# yum -y install gcc gcc-c++ automake autoconf make tcl zlib-devel
[root@redis ~]# groupadd -g 500 redis
[root@redis ~]# useradd -u 500 -g redis redis
[root@redis ~]# echo "08pYzTpwftJVnY2O" | passwd --stdin redis
Changing password for user redis.
passwd: all authentication tokens updated successfully.
2. 修改系统参数
# 修改连接回收、内存权重、最大文件打开数等参数
[root@redis ~]# cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_max_syn_backlog = 65535
net.core.somaxconn = 65535
net.ipv4.tcp_max_tw_buckets = 262144
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_time = 120
vm.overcommit_memory = 1
vm.swappiness = 1
fs.file-max = 1024000
EOF
[root@redis ~]# sysctl -p /etc/sysctl.conf
# 临时关闭透明大页内存
[root@redis ~]# echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
[root@redis ~]# echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
[root@redis ~]# cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]
[root@redis ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
# 永久关闭透明大页内存
[root@redis ~]# cat >> /etc/rc.d/rc.local << EOF
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
fi
EOF
[root@redis ~]# chmod +x /etc/rc.d/rc.local
# 修改ulimit限制
[root@redis ~]# cat >> /etc/security/limits.conf << EOF
redis soft nofile 1024000
redis hard nofile 1024000
EOF
[root@redis ~]# cat >> /etc/security/limits.d/redis.conf <<EOF
redis soft nofile 1024000
redis hard nofile 1024000
EOF
[root@redis ~]# tail -n2 /etc/security/limits.conf
redis soft nofile 1024000
redis hard nofile 1024000
[root@redis ~]# tail -n2 /etc/security/limits.d/redis.conf
redis soft nofile 1024000
redis hard nofile 1024000
3. 安装Redis软件
[root@redis ~]# mkdir -p /data/redis/base /data/redis/data /data/redis/conf /data/redis/software
[root@redis ~]# cd /data/redis/software
[root@redis software]# wget http://download.redis.io/releases/redis-5.0.8.tar.gz
[root@redis software]# tar xf redis-5.0.8.tar.gz -C /data/redis/base/
[root@redis software]# mv /data/redis/base/redis-5.0.8 /data/redis/base/5.0.8
[root@redis software]# cd /data/redis/base/5.0.8/deps
[root@redis deps]# make hiredis lua linenoise jemalloc && echo $?
[root@redis deps]# cd /data/redis/base/5.0.8
[root@redis 5.0.8]# make && make install PREFIX=/data/redis/base/5.0.8
[root@redis 5.0.8]# chown -R redis:redis /data/redis
4. 创建Redis配置文件
- 注意配置文件中redis实例的密码及主从复制时的密码设置.
- requirepass : 注意 Redis 连接密码
- masterauth : 主从复制,从库连接主库的密码
[root@redis ~]# mkdir /data/redis/conf/6379
[root@redis ~]# vim /data/redis/conf/6379/redis_6379.conf
### Server ###
pidfile /data/redis/data/6379/redis_6379.pid
port 6379
bind 0.0.0.0
logfile "/data/redis/data/6379/redis_6379.log"
dbfilename dump_6379.rdb
dir /data/redis/data/6379
appendfilename "redis_6379.aof"
daemonize yes
loglevel notice
databases 16
activerehashing yes
hz 10
### Auth ###
requirepass "AY*s23y7Qt5w+J#rz^FoEBBB*Rc$u^qg"
masterauth "AY*s23y7Qt5w+J#rz^FoEBBB*Rc$u^qg"
### Memory ###
maxmemory 10gb
maxmemory-policy volatile-lru
maxmemory-samples 5
### Persistence ###
save ""
#save "900 1"
#save "300 10"
#save "60 10000"
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum yes
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 2gb
aof-load-truncated yes
### Clients ###
maxclients 10000
tcp-backlog 65535
timeout 0
tcp-keepalive 60
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
### Replication ###
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-ping-slave-period 10
repl-disable-tcp-nodelay no
repl-backlog-size 2gb
repl-backlog-ttl 3600
slave-priority 100
### Command ###
rename-command flushall ""
rename-command flushdb ""
### Slowlog ###
slowlog-log-slower-than 1000
slowlog-max-len 1024
### Redis 4.0 New ###
lazyfree-lazy-eviction no
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
slave-lazy-flush yes
### Redis Cluster ###
# cluster-config-file nodes-6379.conf
# cluster-enabled yes
# cluster-node-timeout 15000
# cluster-slave-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage no
5. 初始化6379实例
# 创建 6379 实例的数据目录
[root@redis ~]# mkdir /data/redis/data/6379
[root@redis ~]# chown -R redis:redis /data/redis
6. 添加systemd管理
[root@redis ~]# vim /etc/systemd/system/redis_6379.service
[Unit]
Description=Redis
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/data/redis/data/6379/redis_6379.pid
ExecStart=/data/redis/base/5.0.8/bin/redis-server /data/redis/conf/6379/redis_6379.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
User=redis
Group=redis
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@redis ~]# systemctl daemon-reload
[root@redis ~]# systemctl start redis_6379.service
[root@redis ~]# systemctl status redis_6379.service