目录
1、单机redis安装
下载
软件下载地址:Index of /releases/https://download.redis.io/releases/
调整系统参数
编辑/etc/rc.local,加入
echo never > /sys/kernel/mm/transparent_hugepage/enabled
重启或直接执行以使参数生效
编辑/etc/sysctl.conf,加入
vm.overcommit_memory = 1
net.core.somaxconn=4096
尽量减少内存交换
重启或执行sysctl -p以使参数生效
sudo sysctl -p
安装redis主程序
解压安装程序
tar -zxvf redis-6.2.8.tar.gz
进入解压后的目录
cd redis-6.2.8
编译源码
make
使用 管理员权限指定安装路径进行安装
sudo make PREFIX=/usr/local/redis install
创建所需要的目录
sudo mkdir /usr/local/redis/conf
sudo mkdir -p /data/redis/data/6379
sudo mkdir /data/redis/log
从源码copy redis.conf配置文件
将redis.conf复制到/usr/local/redis/conf目录
sudo cp redis.conf /usr/local/redis/conf/redis_6379.conf
编辑配置文件调整配置
使用管理员权限编辑/usr/local/redis/conf/redis_6379.conf,调整下列配置项
bind 0.0.0.0
daemonize yes
pidfile /var/run/redis_6379.pid
port 6379
logfile "/data/redis/log/redis_6379.log"
dir "/data/redis/data/6379"
maxmemory 1gb
maxmemory-policy volatile-ttl
appendonly yes
notify-keyspace-events "xeE"
设置reids认证口令
使用管理员权限编辑/usr/local/redis/conf/redis_6379.conf,调整下列配置项
加入:
requirepass xErWB6JX6NuS
添加启动脚本
添加启动脚本/etc/init.d/redis_6379
sudo vi /etc/init.d/redis_6379
#!/bin/sh
#Configurations injected by install_server below....
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/conf/redis_6379.conf"
REDISPORT="6379"
PASSWORD=$(cat $CONF|grep '^\s*requirepass'|awk '{print $2}'|sed 's/"//g')
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_9736 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_9736
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_9736
# Description: Redis daemon
### END INIT INFO
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
if [ -z $PASSWORD ]
then
$CLIEXEC -p $REDISPORT shutdown
else
$CLIEXEC -a $PASSWORD -p $REDISPORT shutdown 2>/dev/null
fi
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=
if [ -f $PIDFILE ]
then
PID=$(cat $PIDFILE)
fi
if [ "$PID" = "" ] || [ ! -x /proc/${PID} ]
then
echo 'Redis is not running'
else
echo "Redis is running ($PID)"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Please use start, stop, restart or status as first argument"
;;
esac
添加开机自启动项
sudo chmod +x /etc/init.d/redis_6379
sudo chkconfig --add redis_6379
sudo chkconfig redis_6379 on
启动
sudo /etc/init.d/redis_6379 start
查看状态
sudo /etc/init.d/redis_6379 status
2、安装(一主二从三哨兵)redis集群
主节点:192.168.1.10 从节点:192.168.1.11、192.168.1.12
按照a) 安装单机redis步骤分别在三台机器上安装一遍redis服务
配置redis集群
分别在:192.168.1.11、192.168.1.12从节点机器上编辑
sudo vi /usr/local/redis/conf/redis_6379.conf
中添加主节点IP和端口: slaveof 192.168.1.10 6379
分别在:192.168.1.11、192.168.1.12从节点机器上编辑
sudo vi /usr/local/redis/conf/redis_6379.conf
从节点添加主节点的口令
masterauth xErWB6JX6NuS
配置三个哨兵节点
分别配置192.168.1.10、192.168.1.11、192.168.1.12三台机器上的哨兵节点,操作如下:
创建目录
sudo mkdir -p /data/redis/data/26379
复制sentinel.conf,调整配置
sudo cp sentinel.conf /usr/local/redis/conf/sentinel_26379.conf
sudo vi /usr/local/redis/conf/sentinel_26379.conf
daemonize yes
pidfile /var/run/redis-sentinel-26379.pid
port 26379
logfile "/data/redis/log/sentinel_26379.log"
dir "/data/redis/data/26379"
sentinel monitor mymaster 192.168.1.10 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 60000
(注:
行sentinel monitor mymaster 192.168.1.10 6379 2
mymaster
192.168.1.10为主redis节点ip
6379为主redis端口
2为判定redis节点失效所需要的sentinel票数,设为2则需要有3个sentinel节点)
添加哨兵自启动脚本
sudo vi /etc/init.d/sentinel_26379
#!/bin/sh
#Configurations injected by install_server below....
EXEC=/usr/local/redis/bin/redis-sentinel
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis-sentinel-26379.pid
CONF="/usr/local/redis/conf/sentinel_26379.conf"
REDISPORT="26379"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_sentinel_26379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_sentinel_26379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_sentinel_26379
# Description: Redis Sentinel daemon
### END INIT INFO
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis Sentinel server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis Sentinel to shutdown ..."
sleep 1
done
echo "Redis Sentinel stopped"
fi
;;
status)
if [ ! -f $PIDFILE ]
then
echo 'Redis Sentinel is not running'
else
PID=$(cat $PIDFILE)
if [ ! -x /proc/${PID} ]
then
echo 'Redis Sentinel is not running'
else
echo "Redis Sentinel is running ($PID)"
fi
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Please use start, stop, restart or status as first argument"
;;
esac
添加自动启动项
sudo chmod +x /etc/init.d/sentinel_26379
sudo chkconfig --add sentinel_26379
sudo chkconfig sentinel_26379 on