环境说明
三台服务器 10.x.x.1 redis1,10.x.x.2 redis2, 10.x.x.3 redis3 ;
安装时设置redis1 服务器为主,redis2,redis3为从,配置好sentinel之后,主从关系将交给sentinel系统自动控制。
前置1:先安装gcc
yum install gcc-c++
前置2:安装jemalloc
- 下载jemalloc-5.0.1.tar.bz2, url地址https://github.com/jemalloc/jemalloc/releases
- 上传到家目录
- tar xvf jemalloc-5.0.1.tar.bz2
- cd jemalloc-5.0.1
- ./configure -prefix=/usr/local/jemalloc
- make -j8 && make install
- echo /usr/local/jemalloc/lib >> /etc/ld.so.conf
- ldconfig
前置3:增大系统核心参数somaxconn
vim /etc/sysctl.conf 增加一行
net.core.somaxconn=1024
然后执行命令
sysctl -p
正式开始安装redis
每台服务器上都安装如下步骤执行:
按照redis4.0.6
-
下载或上传 redis-4.0.6.tar.gz 到linux用户的家目录。
-
进入家目录,解压: tar xzf redis-4.0.6.tar.gz
-
运行命令: cd redis-4.0.6
-
运行命令: make MALLOC=jemalloc && make install,完成安装
此步骤里切记要添加参数MALLOC=jemalloc,安装jemalloc方式的内存管理,防止内存碎片比例过高。
make install安装完成后,会在/usr/local/bin目录下生成下面几个可执行文件,它们的作用分别是:
redis-server:Redis服务器端启动程序
redis-cli:Redis客户端操作工具。也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具
redis-check-aof:数据修复工具 -
(配置sentinel模式则跳过本步)安装到系统服务 ./utils/install_server.sh
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
- 编辑redis配置文件
mkdir /etc/redis
mkdir -p /data/redis/log
cp ~/redis-4.0.6/redis.conf /etc/redis/
vim /etc/redis/redis.conf
内存最大8G字节 maxmemory 8589934592
绑定所有网口 bind 0.0.0.0
关闭保护模式 protected-mode no
开启守护模式 daemonize yes
指定数据存储目录 dir /data/redis
每秒一次aof写 appendfsync everysec
打开aof持久化 appendonly yes
指定日志文件 logfile "/data/redis/log/redis.log"
在2台从服务器的redis.conf里最后添加下面一行
slaveof 10.x.x.1 6379
- 编辑哨兵配置文件,全部内容如下
vim /etc/redis/sentinel.conf
port 26379
dir "/tmp"
daemonize yes
protected-mode no
logfile "/data/redis/log/sentinel.log"
sentinel monitor mymaster 10.x.x.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 60000
-
分别启动redis:
redis-server /etc/redis/redis.conf & -
启动哨兵:
方式一:redis-sentinel /etc/redis/sentinel.conf &(推荐,这种方式启动和redis实例没有任何关系)
方式二:redis-server /etc/redis/sentinel.conf --sentinel &
如果发现哨兵模式不正常,master不能发现slave。则检查slave节点的/etc/redis/redis.conf,最后一行有没有 slaveof 6379
- 查看状态命令 redis-cli
127.0.0.1:6379> info replication
\# Replication
role:master
connected_slaves:2
slave0:ip=10.x.x.2,port=6379,state=online,offset=434,lag=1
slave1:ip=10.x.x.1,port=6379,state=online,offset=434,lag=1
master_replid:4219d672b92cb2ea4e9920eb36f3d20c55ee5cac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:434
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:434
-
停止redis命令: redis-cli shutdown
-
停止sentinel : ps -ef|grep “[sentinel]”|grep -v grep|cut -c 9-15|xargs kill
-
连接sentinel : redis-cli -p 26379 ,命令 info sentinel、 sentinel master mymaster。
-
使用时: 10.x.x.1:26379,10.x.x.2:26379,10.x.x.3:26379
-
重启redis:
redis-cli shutdown
redis-server /etc/redis/redis.conf &