下载redis,解压并安装:
# wget http://download.redis.io/releases/redis-4.0.2.tar.gz
# tar xzf redis-4.0.2.tar.gz
# cd redis-4.0.2
# make
# mv redis-4.0.2 /usr/local/redis
启动并测试:
# /usr/local/redis/src/redis-server //启动6379端口的redis实例并执行下面的命令测试(另开窗口)
# /usr/local/redis/src/redis-cli -p 6379
> keys *
> set k1 v1
> keys *
> get k1
配置环境变量:
# echo 'PATH=${PATH}:/usr/local/redis/src/' >> /etc/profile
# source /etc/profile #<== 重新加载配置文件
配置伪集群:
# mkdir -p /data/redis/{6379,6380}/{conf,db,log}
# cp /usr/local/redis/redis.conf /data/redis/6379/conf/
# cp /usr/local/redis/redis.conf /data/redis/6380/conf/
# tree //查看项目树形结构
将redis.conf修改为对应的实例参数:
# grep "6379\|daemonize" 6380/conf/redis.conf
# grep "6380\|daemonize" 6380/conf/redis.conf
启动redis实例:
# redis-server /data/redis/6379/conf/redis.conf
# redis-server /data/redis/6380/conf/redis.conf
确认是否启动成功
# netstat -ntlp | grep -E ":6379|:6380"
配置主从同步:
6379为主库,6380为从库
修改从库6380配置:
# vim /data/redis/6380/conf/redis.conf
在里面添加一行:
slaveof 127.0.0.1 6379
验证是否添加成功:
# redis-cli -p 6379 "info"
role:master <== 角色:master
connected_slaves:2 <== slave链接数 1
slave0:ip=127.0.0.1,port=6380,state=online,offset=141,lag=1 <== slave 的信息
master_repl_offset:141
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:140
# redis-cli -p 6380 "info"
....
# Replication
role:slave <==角色 slave
master_host:127.0.0.1 <==master主机
master_port:6379 <== master端口
master_link_status:up <== 链接状态 up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:673
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
搭建哨兵集群:
在对应的/data/redis/6379和/data/redis/6380分别创建sentinel.conf文件:
# cd /data/redis/6379
# vim sentinel-26379.conf //内容如下:
#port
port 26379
#master2 configuration
sentinel monitor mymaster 10.10.10.127 6379 1
daemonize yes
pidfile "/data/redis/6379/sentinel-26379.pid"
logfile "/data/redis/6379/sentinel-26379.log"
# Generated by CONFIG REWRITE
dir "/data/redis/6379"
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 3432
sentinel current-epoch 3432
# cd /data/redis/6380
# vim sentinel-26380.conf //内容如下:
#port
port 26380
#master2 configuration
sentinel monitor mymaster 10.10.10.127 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 900000
sentinel parallel-syncs mymaster 2
daemonize yes
pidfile "/data/redis/6380/sentinel-26380.pid"
logfile "/data/redis/6380/sentinel-26380.log"
# Generated by CONFIG REWRITE
dir "/data/redis/6380"
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 688
sentinel current-epoch 688
然后分别启动两个哨兵:
# redis-sentinel /data/redis/6379/sentinel-6379.conf --sentinel
# redis-sentinel /data/redis/6380/sentinel-6380.conf --sentinel
ps观察下进程如下:
# ps -ef | grep redis
shell重启关闭脚本编写(因为redis使用root起的,所以需要给777权限或用root执行该脚本):
因为单机集群和哨兵重启有点麻烦,所有我写了两个脚本(第一个是重启脚本,第二个是关闭脚本)方便重启、关闭如下:
echo "重启开始.........."
echo ".................."
# redis主从部署目录
redis_data_dir=/data/redis
# 遍历处理,先杀掉所有redis相关的进程
for application_dir in `ls $redis_data_dir`
do
# 应用的进程ID 包含sentinel、redis应用的PID
redis_pid=`ps -ef | grep redis-server | grep ${application_dir} | grep -v grep | awk '{print $2}'`
sentinel_pid=`ps -ef | grep redis-sentinel | grep ${application_dir} | grep -v grep | awk '{print $2}'`
# 判断进程是否存在,存在则杀掉
if [ -n "$redis_pid" ]; then
kill -9 $redis_pid
fi
if [ -n "$sentinel_pid" ]; then
kill -9 $sentinel_pid
fi
done
sleep 1
# 启动,遍历先启动redis实例,在启动sentinel哨兵
for application_dir in `ls $redis_data_dir`
do
# 启动redis实例
redis-server /data/redis/${application_dir}/conf/redis.conf
done
sleep 2
for application_dir in `ls $redis_data_dir`
do
# 启动sentinel
redis-sentinel /data/redis/${application_dir}/sentinel-2${application_dir}.conf --sentinel
done
echo "重启结束.........."
echo "关闭开始.........."
echo ".................."
# redis主从部署目录
redis_data_dir=/data/redis
# 遍历处理,先杀掉所有redis相关的进程
for application_dir in `ls $redis_data_dir`
do
# 应用的进程ID 包含sentinel、redis应用的PID
redis_pid=`ps -ef | grep redis-server | grep ${application_dir} | grep -v grep | awk '{print $2}'`
sentinel_pid=`ps -ef | grep redis-sentinel | grep ${application_dir} | grep -v grep | awk '{print $2}'`
# 判断进程是否存在,存在则杀掉
if [ -n "$redis_pid" ]; then
kill -9 $redis_pid
fi
if [ -n "$sentinel_pid" ]; then
kill -9 $sentinel_pid
fi
done
echo "关闭结束.........."
参考链接:
redis单机伪集群主从实例搭建:https://www.cnblogs.com/lgeng/p/6623336.html
redis哨兵集权搭建:https://blog.csdn.net/qq_37853817/article/details/78961462